Mixing cout and wcout in same program

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.


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.