CppCon 2018: Arno Schoedl “Range-Based Text Formatting For a Future Range-Based Standard Library”

preview_player
Показать описание


Text formatting has been a favorite problem of C++ library authors for a long time. The standard C++ iostreams have been criticized for being difficult to use due to their statefulness and slow due to runtime polymorphism. Despite its age, printf is still popular because of simplicity and speed. The Boost library offers two more alternatives, Boost.Format and Boost.LexicalCast. And finally, the P0645 standard proposal sponsored by Facebook is currently finding its way through the C++ committee.

All these approaches are still firmly based on standard containers and iterators. But the Standard Library is changing radically with the advent of ranges, range adaptors and functional style programming in C++. Generating optimized code with metaprogramming is becoming standard fare.

In this talk, I want to convince you that the combination of ranges with a bit of metaprogramming makes for a very elegant solution to the text formatting problem. We introduce a form of ranges with internal iteration, which are generating their elements one by one rather than exposing external iterators. We can use these generator ranges to represent the values to be formatted, conceptually turning them into lazily evaluated strings. These can be used just like regular strings are used today: in function returns; as standard algorithm input; embedded into other, equally lazily evaluated strings; and so on, before they are finally expanded for display. By choosing the right interfaces, we can optimize this expansion at compile-time, making it no less pretty to write, but more efficient to expand than any text formatting approaches that rely on format strings that must be parsed at runtime.

I believe that this approach is the natural extension of a range-based future standard library to text formatting.


Arno Schoedl, think-cell
CTO

Arno is the CTO of think-cell Software GmbH. He is responsible for the design, architecture and development of all their software products, and the evolution of the company's open source C++ library. Before founding think-cell, Arno worked at Microsoft Research and McKinsey. Arno studied computer science and management and holds a Ph.D. from the Georgia Institute of Technology with a specialization in Computer Graphics.


*-----*
*-----*
Рекомендации по теме
Комментарии
Автор

What's missing is a benchmark on compile times.

bernadettetreual
Автор

At 16:32 `std::string s();` defines a function that returns a string rather than constructing an empty string.

echosystemd
Автор

Code-based concatenation is the worst way to do user-facing text formatting (though it can work well for data-driven formatting, such as XML or JSON). It is completely incompatible with translation, which often needs to re-order the components arbitrarily in a manner defined by the translator, which is often read at runtime rather than compile time. Different plural forms are also incredibly complicated. This is one of the reasons why most libraries intended for user text settle on using format strings. You mentioned this in passing at the start but then glossed over it.

Mirality
Автор

At 32:40, a 'Range const&' is forwarded. But the correct code would be to simply pass the range reference, or to forward it to maybe store it by value, similarly to what happens et 21:55 ?
It would be pretty cool if the moved range moves then it's elements into the 'appender' consumer (which are forwarded here).

Hichigo
Автор

At 21:55 you make a distinction between l-value and r-value parameters and store them differently. Does that mean there are 2 member variables, e.g. `T &value_ref` and `T value`? How would `value_ref` be initialized then? If not, how does this work? I'd like to know, since I'm running into a problem for which I think this might be a solution.

krytharn
Автор

Ah so this is the guy that according to Glassdoor abuses his subordinates...

Voy