c++ - 为什么 w/cout 不支持字符串 U/u 前缀?

标签 c++ string iostream cout

我有以下受 SO 问题之一启发的代码:

int main()
{
    wcout << "\n\n 1- Test normal string with wcout \n";
    wcout << u8"\n 2- Test (u8) string with wcout  \n";
    wcout << u"\n 3- Test (u) string with wcout \n";
    wcout << U"\n 4- Test (U) string with wcout \n";
    wcout << L"\n 5- Test (L) string with wcout \n\n"; 
}

结果是:

Output

cout具有相似的输出。

我从 this post 的回答中了解到w/cout 不支持字符串 U/u 前缀,或者完全如前所述:

Meanwhile std::cout's class has no special << overload for const char16_t*, const char32_t* and const wchar_t*, hence it will match <<'s overload for printing pointers.

我的问题是:

1- 为什么它不支持这些类型?一种语言不支持自己的类型,这不是很奇怪吗? 2- 是否有任何已知计划在可见的 future 添加此类支持?

我知道 iostream 是一个库,但实际上,它仍然是语言的一部分。

最佳答案

标准库遗漏的通常答案适用:没有人提议为 char16_t const*char32_t 添加重载。添加字符类型 ( N2249 ) 的提议不包含相关的库部分。特别是,它没有为

添加重载
template <typename charT, typename Traits>
std::basic_ostream<charT, Traits>& std::operator<< (std::basic_ostream<charT, Traits>&, char16_t const*);
template <typename charT, typename Traits>
std::basic_ostream<charT, Traits>& std::operator<< (std::basic_ostream<charT, Traits>&, char32_t const*);

由于这些运算符重载了 void const*,因此打印了指针的地址。由于新的字符类型有点毫无意义(至少,从我的角度来看:只需使用 UTF8)我怀疑是否有人有兴趣提出建议。

关于c++ - 为什么 w/cout 不支持字符串 U/u 前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44078202/

相关文章:

c++ - 使用 incrimenter 计算空格

c++ - QML 与 C++ 通信

c++ - 访问在 C 中保存 C++ 对象的空指针的内容

string - Ada 中的多行字符串文字

c++ - 使用流读取复杂结构

c++ - 使用 istream_iterator 从键盘读取结构 vector ?

c++ - std::make_pair 与 c++ 11

python - 如何在Python中为类设置字符串渲染方法

c# - 是否可以在 Stream 参数中限制/要求某些功能?

php - 查找给定的字符串是否匹配,尽管有输入错误