Intermingling cout and wcout in a Program
The question arises when encountering a warning about mixing cout and wcout in a program. However, further investigation reveals contrasting opinions on the matter.
According to the C Standard [27.4.1], mixing wide and narrow character stream operations should follow the same semantics as mixing such operations on FILEs, as outlined in the ISO C standard's Amendment 1. The C Standard [7.19.2] further underscores that once a stream's orientation is set, whether byte-oriented or wide-oriented, it should not be intermixed with incompatible functions.
However, it is important to note that different compiler implementations may have differing behaviors regarding stream orientation. For example, Visual C apparently disregards the standard's requirements and allows for the intermingling of cout and wcout.
In the case of gcc, a feature known as stream orientation has been implemented. To avoid issues related to stream orientation, it is recommended to call std::ios::sync_with_stdio(false) at the program's inception.
In summary, while the C standard advises against mixing cout and wcout due to stream orientation, certain compiler implementations may have workarounds or different interpretations of stream handling. Referencing the specific compiler's documentation and using recommended practices is crucial for proper implementation.
The above is the detailed content of Is it safe to intermingle `cout` and `wcout` in a C program?. For more information, please follow other related articles on the PHP Chinese website!