Mr. Boy
Mr. Boy

Reputation: 63710

Why/how is it valid to pass a non-wide string to wstringstream::operator<<?

wstring ss;
ss << L"Some wide-char text" << " and some non-wide-char text";

This appears to work just fine but why, when char_type is not char?

Upvotes: 1

Views: 92

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385088

Because it is.

Exactly std::char_traits<char>::length(s) characters are inserted.
Before insertion, first, all characters are widened using os.widen().

(source)

Upvotes: 0

user657267
user657267

Reputation: 20990

All streams have overloads for char, regardless of the CharT of the template.

http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2

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

Upvotes: 1

Related Questions