The character encoding of std::string
is not defined by the C++ standard, a std::string
can hold any encoding that can be represented using 1-byte char
elements, which includes UTF-7/8, ISO-8859-x, Windows-125x, etc.
Also, the size of wchar_t
is implementation-defined, not defined by the standard, so even the encoding of std::wstring
can vary, too. On Windows, wchar_t
is 2 bytes, so std::wstring
uses UCS-2/UTF-16 encoding. Whereas on other platforms, wchar_t
is 4 bytes, so std::wstring
uses UCS-4/UTF-32.
So, there is no single conversion that satisfies all possible combinations of std::string <-> std::wstring
conversions across all platforms and use-cases. So, you need to know the encoding of the source string, and the intended encoding of the target string, in order to perform a conversion.
And yes, the C++ standard did provide std::codecvt
and std::wstring_convert
/std::wbuffer_convert
for this task, but they have been deprecated, as you have noted. There is no standard replacement provided (yet?).
So, you are best off using 3rd party Unicode API/libraries to handle character conversions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…