4

Is there any reason why std::from_chars doesn't have an overload for wchar_t?

Currently, there are only four overloads one of them being:

constexpr std::from_chars_result from_chars( const char* first, const char* last,
                                             /*see below*/& value, int base = 10 );

So what is the reason that there doesn't exist any overload for wchar_t or other character types? Is there any chance that they will be added in C++26? If not then are there equivalents for wchar_t?

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • `wchar_t` is a bit of a mess. Its size isn't fixed, and Crom only knows what encoding the data in it uses. With that much unknown I wouldn't want to specify or try to implement a `wchar_t` overload either. That leads to Snark answer: Because then it would be `std::from_wchars`? – user4581301 Sep 15 '22 at 18:30
  • @user4581301 `std::from_wchars` is a better name. I guess it would be handy if we had it in the std library. – digito_evo Sep 15 '22 at 18:36
  • I knew I shouldn't have gotten snarky. It distracts from the real point: `wchar_t` is the Wild West. There are too many unknowns to be able to specify how `from_chars` would have to behave. – user4581301 Sep 15 '22 at 18:40

1 Answers1

4

The from/to_chars series of functions are for elementary string conversions. These are the most elemental of numeric conversions. As such, they only support the most basic encoding: your system's native narrow character set (usually Unicode codepoints less than 128 encoded as UTF-8 or ASCII).

If you have text in some other encoding, it is on yourself to convert that encoding from/to the system narrow encoding in order to use these functions.

The expected use cases for these strings are for things like writing to/reading from textual formats like JSON. Such files are almost universally UTF-8 encoded (or ASCII), and when they call for numbers, their numbers are both locale-independent (always using . for the radix mark, for example) and encodes numbers using only the ASCII-part of Unicode.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982