Text Formatting at the ISO C++ standards meeting in Jacksonville

This week I attended the ISO C++ standards committee meeting to present the second revision of my paper P0645R1 Text Formatting, based on the {fmt} formatting library. The first revision was reviewed by Library Evolution Working Group (LEWG) in June 2017 in Toronto and there was a lot of feedback, particularly I’ve been asked to

• investigate compile-time format string processing,
• look at using or explain why not to use an output iterator,
• use string_view,
• allow pre-computation of output size,

It took me a while to address all the feedback, especially the first two items which required substantial API and implementation changes, so I had to skip the meeting in Albuquerque. However, this ultimately resulted in a much better API so I’m grateful to LEWG for the helpful guidance. Despite all the negative press covfefe “design by committee” sometimes works surprisingly well.

Results

P0645R1 was reviewed on Wednesday and the main features of the proposal received strong support from LEWG. Here are the results of the polls:

where “this” is the proposed Python-like syntax with ‘{‘ and ‘}’ used as replacement field delimiters in format strings. “SF”, “F”, “N”, “A” and “SA” stand for “Strongly in Favor”, “in Favor”, “Neutral”, “Against” and “Strongly Against” respectively.

Apart from the obvious consistency argument, a very strong argument in favor of the second feature is that it can be beneficial for localization. For example, it’s easy to change date formatting even if you use some non-standard date type:

and somewhere in a translation database

There was a long discussion on how to make the iterator-based API safer and avoid the pitfalls of sprintf. The outcome of this discussion and a subsequent poll was to add a new API function, format_to_n, that takes an iterator and a size which should be a relatively minor change.

Thanks to Eric Niebler for helping to defend the iterator-based API, the idea of which he in fact proposed in Toronto. Iterators are a standard and well-understood abstraction and they work fairly well both in the common case of efficiently writing to contiguous memory (there was no performance regression in {fmt} after adding iterator support) as well as more complex targets such as std::ostream or std::streambuf.

In the future we could have even more convenient API with ranges.

There was a question of whether it is possible to check format strings for user-defined types at compile-time. The design of the extension API allows this and here’s an example (godbolt):

This gives an intentional compile error due to an invalid format specifier:

Changing the specifier from ‘x’ to one of the valid two (‘d’ or ‘s’) fixes the error (godbolt):

Compile-time strings

In the current standard to provide an API that takes a compile-time string, i.e. a string that can be processed in the constexpr context, you need to use hacks such as constexpr lambdas nicely described in Michael Park’s post constexpr function parameters. For example, {fmt} provides the fmt macro (sigh) for constructing a compile-time format string:

where fmt(s) is roughly defined as follows:

Obviously this is far from ideal. Fortunately, Louis Dionne proposed the paper P0424 Reconsidering literal operator templates for strings which addressed exactly this use case and added literal operator template for string literals, so that you could do something like

P0424 was approved by Evolution Working Group in Albuquerque, but in Jacksonville Jeff Snyder submitted P0732 Class Types in Non-Type Template Parameters which provided a more general way of addressing the same use cases. As the name suggests with P0732 you can use strings (and more) as template parameters:

This is a great feature and according to Bjarne Stroustrup the intent was to have this functionality from the very beginning but it was not clear how to implement it at the time.

After a few delays P0732 was finally reviewed by EWG on Thursday. It was unanimously approved and sent to Core Working Group to review the wording. In an interesting twist, P0424 was pulled out of Core, but EWG voted for the user-defined literal part of it, which will now have to be based on top of P0732.

Once the two papers are accepted it will be possible to provide different APIs taking compile-time string:

I hope that there will be an even more intuitive way to express constexpr parameters, but P0732 and P0424 make a huge step in the right direction.

What’s next?

I plan to address the LEWG’s feedback, improve the wording, and present a new revision of P0645 Text Formatting at one of the forthcoming meetings. Help in reviewing and/or improving the wording of the paper will be greatly appreciated and acknowledged.