Question

I just noticed that output of a char* through std::wcoutworks totally fine on MSVC (tested with VS2005 and VS 2010).

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

Indeed, debugging into the output we get to the <ostream> header:

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

However, it would appear that this version is not documented on MSDN nor indeed on cppref (ostream members).

Is this standard? Is this an undocumented MS extension? Does any good come of it?


Do note that the opposite will indeed invokde the void*output:

std::cout << L"Will just print address of string";
Was it helpful?

Solution

At least according to cppref (non-member operator<<) it is defined (see the sixth one down from the top)

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

Also, looking at the comments:

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

this seems well defined by the C++ standard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top