Mixing cout and wcout in same program
I have no idea.
Barring threads, you can't run any two statements "at the same time". You can certainly use cout
and wcout
at different points in your program, though. They both map to STDOUT
and that's that... though you may fall foul of differing buffers and get slightly unexpected ordering, in some cases.
Apparently, each imbues an orientation on the "destination" stream STDOUT
, and it is not allowed to mix operations on a stream that has been imbued with an orientation [C++11: 27.4.1]
and [C99: 7.19.2]
.
Violating "shall not"s from the standard usually lands you in the realm of undefined behavior. Undefined behavior might very well work properly on some implementations.
When cout
or wcout
is called for the first time, the orientation for stdout
becomes set. In the case of cout
, stdout
becomes a byte-oriented stream, and in the case of wcout
, stdout
becomes a wide-oriented stream. Per the C++ standard [27.4.1] and C standard [7.19.2], once the orientation of a stream is set, you should not call a function which is not compatible with the orientation of that stream.
Technically, you can definitely use both the narrow and the wide streams simultaneously. The result is, however, likely to be messed up unless you arrange for both of them to encode characters the same. This, unfortunately, comes with the caveat that you can't control the encodings used by the standard stream objects, at least not portably. Even if the encoding is the same, you need to make sure that partial characters are completely written, i.e. at the very least you need to flush the buffer when switching to the other width.