Search code examples
c++stringwchar-tc++23

std::from_chars overload for wchar_t?


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?


Solution

  • 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.