【问题标题】:Is output of char* strings through std::wostream defined?是否定义了通过 std::wostream 输出的 char* 字符串?
【发布时间】:2014-03-01 22:56:35
【问题描述】:

我刚刚注意到,通过std::wcout 987654324 @Works on MSVC(用VS2005和VS 2010测试)。

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

确实,调试到我们得到&lt;ostream&gt;标头的输出:

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

但是,MSDNcppref (ostream members) 上似乎都没有记录此版本。

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


请注意,相反的情况确实会调用void*output:

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

【问题讨论】:

    标签: c++ visual-c++ iostream


    【解决方案1】:

    至少按照cppref (non-member operator&lt;&lt;)是这样定义的(见从上往下第六个)

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

    另外,看看 cmets:

    在插入之前,首先所有字符都用os.widen()加宽, 那么……

    这似乎在 C++ 标准中得到了很好的定义。

    【讨论】:

    • @MartinBa 他们应该一起定义,奇怪的是他们在不同的页面上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多