Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing cout and wcout in same program

Tags:

c++

I was reading the "C++ Cookbook" which had the following snippet:

// cout  << s  << std::endl;  // You shouldn't be able to wcout << ws << std::endl;     // run these at the same time 

If you're interested in seeing the actual example, here is a link to the page on Google books.

Also, I found this SO question which seems to state that mixing wcout and cout is okay. Could someone explain to me what this comment is talking about?

EDIT

From C++ Standard [27.4.1]:

Mixing operations on corresponding wide- and narrow-character streams follows the same semantics as mixing such operations on FILEs, as specified in Amendment 1 of the ISO C standard.

From C Standard [7.19.2]:

Each stream has an orientation. After a stream is associated with an external file, but before any operations are performed on it, the stream is without orientation. Once a wide character input/output function has been applied to a stream without orientation, the stream becomes a wide-oriented stream. Similarly, once a byte input/output function has been applied to a stream without orientation, the stream becomes a byte-oriented stream. Only a call to the freopen function or the fwide function can otherwise alter the orientation of a stream. (A successful call to freopen removes any orientation.)

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.

So, the standard seems to say that you should not mix them. However, I found this quote from this article:

For Visual C++ 10.0 the fwide function is documented as being unimplemented. And from a practical point of view, at least at the level of outputting whole lines it apparently works fine to intermingle use of cout and wcout. So, happily, Visual C++ apparently just disregards the standard’s requirements and does not maintain an impractical explicit C FILE stream orientation.

And also, concerning gcc I found this quote from here:

This is a (new) feature, not a bug, see libstdc++/11705 and in general search about stream orientation in the C standard (C99, 7.19.2). In a nutshell you cannot mix byte oriented and wide oriented I/O. For now, due to the bug pointed out in libstdc++/11705, you can obtain something close to your expectations by calling std::ios::sync_with_stdio(false); at the beginning of your program.

like image 609
Jesse Good Avatar asked Jan 20 '12 21:01

Jesse Good


1 Answers

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.

like image 172
Jesse Good Avatar answered Sep 28 '22 16:09

Jesse Good