c++ - 是否定义了通过 std::wostream 输出 char* 字符串?

标签 c++ visual-c++ iostream

我刚刚注意到 char* 的输出通过std::wcout在 MSVC 上运行完全正常(使用 VS2005 和 VS 2010 进行测试)。

std::wcout << L"Works as expected";
std::wcout << "This ALSO DOES print the string";

事实上,调试输出后我们得到 <ostream>标题:

template<class _Elem, class _Traits> 
inline basic_ostream<_Elem, _Traits>& 
operator<<(basic_ostream<_Elem, _Traits>& _Ostr, const char *_Val) { ...

但是,这个版本似乎没有记录在 MSDN 上。也确实在cppref (ostream members)上.

这是标准吗?这是一个未记录的 MS 扩展吗?有什么好处吗?


请注意,相反的操作确实会调用 void*输出:

std::cout << L"Will just print address of string";

最佳答案

至少根据cppref (non-member operator<< )它已被定义(参见从上往下第六个)

template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os, 
                                     const char* s );

另外,看看评论:

Before insertion, first, all characters are widened using os.widen(), then ...

这似乎是由 C++ 标准明确定义的。

关于c++ - 是否定义了通过 std::wostream 输出 char* 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21578468/

相关文章:

c++ - 如何从外部函数更改局部静态变量值

c++ - C++ 中带线程的时钟函数

c++ - 检测应用程序是否在笔记本电脑上运行

c# - 如何使用使用它的 .NET 应用程序进程来调试 ActiveX 控件?

c++ - Visual Studio 2010 - 导出(项目)模板菜单选项变灰

c++ - 为什么std::getline()在格式化提取后会跳过输入?

c++ - 为什么我们需要两个定义: integral constant expression and converted constant expression?

c++ - Visual Studio - 启用优化导致崩溃

c++ - C++ IO 流简介

C++ 缩进重载 ostream 运算符