简体   繁体   English

为什么 STL “官方”不支持 std::string 到 std::wstring 的转换?

[英]Why doesn't STL “officially” support std::string to std::wstring conversions?

I am aware that std::string and std::wstring come from the same base type std::basic_string<> .我知道std::stringstd::wstring来自相同的基本类型std::basic_string<> But there isn't an "official" way to convert std::string data to std::wstring using the C++ STL?但是没有使用 C++ STL 将std::string数据转换为std::wstring的“官方”方式? I mean Windows provide MultiByteToWideChar() to convert but why cant the STL provide one?我的意思是 Windows 提供MultiByteToWideChar()进行转换,但为什么 STL 不能提供一个?

I used std::codecvt before to get it done but now it says that it is deprecated.我之前使用std::codecvt来完成它,但现在它说它已被弃用。 Why does the STL remove this support the first place?为什么STL先去掉这个支持?

Thanks in advance.提前致谢。

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. std::string字符编码不是由 C++ 标准定义的, std::string可以保存可以使用 1 字节char元素表示的任何编码,包括 UTF-7/8、ISO-8859-x、 Windows-125x 等

Also, the size of wchar_t is implementation-defined , not defined by the standard, so even the encoding of std::wstring can vary, too.此外, wchar_t的大小是implementation-defined ,而不是标准定义的,因此即使std::wstring编码也可能会有所不同。 On Windows, wchar_t is 2 bytes, so std::wstring uses UCS-2/UTF-16 encoding.在 Windows 上, wchar_t为 2 个字节,因此std::wstring使用 UCS-2/UTF-16 编码。 Whereas on other platforms, wchar_t is 4 bytes, so std::wstring uses UCS-4/UTF-32.而在其他平台上, wchar_t为 4 个字节,因此std::wstring使用 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.因此,没有单一的转换可以满足所有平台和用例中std::string <-> std::wstring转换的所有可能组合。 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.是的,C++ 标准确实为此任务提供了std::codecvtstd::wstring_convert / std::wbuffer_convert ,但正如您所指出的,它们已被弃用。 There is no standard replacement provided (yet?).没有提供标准替代品(还没有?)。

So, you are best off using 3rd party Unicode API/libraries to handle character conversions.因此,您最好使用第 3 方 Unicode API/库来处理字符转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM