C++03 定义了两种字符类型:char
和 wchar_t
。 (让我们忽略 signed char
和 unsigned char
疯狂)。
然后将这两个字符应用于 std::basic_string
、std::basic_ostream
等作为 std::string/std::wstring
和 std::ostream/std::wostream
.
标准库还从流中定义了全局变量 std::cout
和 std::wcout
。
新的 c++0x 标准定义了另外两种字符类型 char16_t
和 char32_t
。然而,唯一的新类型定义是 std::u16string
和 std::u32string
。
为什么标准不提供 std::u16ostream
?或者 std::u32cout
怎么样?
最佳答案
我们认为实现 Unicode iostream 的工作量太大,不值得: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2238.html
来自论文:
The rationale for leaving out stream specializations of the two new types was that streams of non-char types have not attracted wide usage, so it is not clear that there is a real need for doubling the number of specalizations of this very complicated machinery.
据我了解,标准委员会意识到序列化为宽字符(2 或 4 字节格式)并不常见,在需要 UTF-16 或 UTF-32 的地方,您始终可以使用相同的旧的基于字符的字节流,但具有 codecvt 方面会将您的输入转换为 UTF-16/UTF-32,它可以将其视为另一种多字节格式。
关于c++ - 为什么 std::u16cout 不见了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020314/