-
Notifications
You must be signed in to change notification settings - Fork 4
Description
Make locale-dependent formats for std::format() congruent with default formatting
The design of format() prefers "locale-independent" formatting options for performance reasons. It provides very limited support for locale-dependent formatting via the 'n' specifier.
It's particularly problematic that the 'n' specifier for floating point numbers is specifically limited to the chars_format::general presentation. It would be very useful to have access to chars_format::scientific and chars_format::fixed formatting with locale-dependent presentation.
Adding these features to std::format() at this stage would require significant wording changes that are too large to contain in a comment. However, one approach that could be taken in the future would be to make 'n' be an additional suffix that could be added to format specifiers, rather than being a lone format specifier. This would enable locale-dependent formatting of any of the conversions of any of the arithmetic types.
In order to keep the design space open for making this change in a future version of the standard, it would be ideal for 'n' conversions to always be congruent with the default conversion. It provides an intuitive semantic: 'n' is the same as "no specifier", but with locale-dependent presentation.
The integer and charT presentation types currently specify 'n' conversions that are congruent with the default conversion.
The bool and floating-point presentation types have 'n' conversions that are not congruent with the default conversion.
For C++20:
- Remove the 'n' conversion for bool.
auto s format("{:n}", 1); // Committee Draft: s contains "1" // Proposed: ill-formed format string
Making the 'n' conversion for floating-point match the default conversion, i.e. dependent on whether a precision is specified.
auto s format("{:n} {:2n}", 12.345678, 12.345678); // Committee Draft: s contains "12,3456 12,34" // Proposed: s contains "12,345678 12,34"
These changes are the minimum necessary to allow enhanced support for locale-dependent formatting in the standard library to be added in a backwards-compatible way in a future edition of C++.
Proposed change:
In [tab:format.type.bool]: Remove n.
In [tab:format.type.float]: Replace the 'Meaning' of the n specifier with:
If precision is specified, equivalent to to_chars(first, last, value, chars_format::general, precision), where precision is the specified formatting precision; equivalent to to_chars(first, last, value) otherwise. The context's locale is used to insert the appropriate digit group and decimal radix separator characters.