Home > Backend Development > C++ > body text

Can You Mix `cout` and `wcout` without Errors?

Linda Hamilton
Release: 2024-11-14 17:13:01
Original
467 people have browsed it

Can You Mix `cout` and `wcout` without Errors?

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template