Mixing cout and wcout
As you read in the "C Cookbook," the following code may result in an error:
cout << s << std::endl; // You shouldn't be able to wcout << ws << std::endl; // run these at the same time
This is due to the concept of "stream orientation". After a stream is created, but before any operations are performed on it, it is without orientation. However, once a wide-character input/output function (like wcout) is applied to it, it becomes a wide-oriented stream, and once a byte input/output function (like cout) is applied to it, it becomes a byte-oriented stream.
The C standard specifies that "Byte input/output functions shall not be applied to a wide-oriented stream, and wide character input/output functions shall not be applied to a byte-oriented stream." (C Standard [7.19.2]).
In the case of the code you provided, cout sets stdout to be a byte-oriented stream. Subsequently, wcout should not be used on stdout as per the standard. However, in practice, some compilers and environments may allow mixing wide-character and byte-oriented output functions on the same stream. This behavior may depend on the compiler version and platform.
The above is the detailed content of Can You Mix `cout` and `wcout` without Errors?. For more information, please follow other related articles on the PHP Chinese website!