Mixing cout and wcout in one program

I read 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 a real-world example, here is a link to a page on Google books .

Also, I found this SO question , which seems to say that you can mix wcout and cout. Can someone explain to me what this comment is about?

EDIT

From the C ++ standard [27.4.1]:

The mixing operations in the respective wide and narrowband streams follow the same semantics as the mixing operations in the FILE files, as specified in Amendment 1 to ISO C.

From standard C [7.19.2]:

Each thread has an orientation. After the stream is associated with an external file, but before performing any operations on it, the stream has no orientation. Once a wide character I / O function was applied to a stream without orientation, the stream becomes a wide- oriented stream. Likewise, when a byte I / O function has applied to a stream without orientation, the stream becomes a byte-oriented stream. Otherwise, only calling the freopen function or the fwide function can change the orientation of the stream. (A successful call to freopen removes any orientation.)

Byte I / O functions should not be applied to a wide- oriented stream, and wide character I / O functions should not be applied to a byte 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 outstanding. And from a practical point of view, at least at the output level of whole lines, it obviously works great to mix the use of cout and wcout. Therefore, fortunately, Visual C ++, obviously, simply ignores the requirements of the standards and does not support the impractical explicit orientation of the C FILE stream.

And also for GCC, I found this quote from here :

This is a (new) function, not an error, see Libstd C ++ / 11705 and the general search for stream orientation in the C standard (C99, 7.19.2). In a nutshell, you cannot mix byte-oriented and wide-oriented input-output. At the moment, due to an error As indicated in libstd C ++ / 11705, you can get something close to your expectation by calling std :: ios :: sync_with_stdio (false); at the beginning is your program.

+25
c ++
Jan 20 '12 at 21:15
source share
5 answers

When cout or wcout is called for the first time, the orientation for stdout set. In the case of cout , stdout becomes a byte stream, and in the case of wcout , stdout becomes a widely oriented stream. In accordance with the C ++ standard [27.4.1] and the C standard [7.19.2], after setting the stream orientation, one should not call a function incompatible with the orientation of this stream.

+23
Jan 21 '12 at 21:56
source share

I have no idea.

Disabling threads, you cannot run any two statements "at the same time". Of course, you can use cout and wcout at different points in your program. They both map to STDOUT and what is it ... although in some cases you may be mistaken in different buffers and get a slightly unexpected order.

Apparently, each of them imposes an orientation on the STDOUT "destination" stream, and it is not allowed to mix operations on a stream that has been impregnated with the [C++11: 27.4.1] and [C99: 7.19.2] .

+2
Jan 20 2018-12-21T00:
source share

Technically, you can definitely use both narrow and wide streams at the same time. However, the result is likely to be ruined if you do not agree that both of them encode characters the same way. Unfortunately, this is because you cannot control the encodings used by standard stream objects, at least not portable ones. Even if the encoding is the same, you need to make sure that the partial characters are completely written, that is, at least you need to clear the buffer when switching to a different width.

+2
Jan 20 '12 at 21:33
source share

Violating "does not mean that the standard does not mean that you are in the realm of undefined behavior. Undefined behavior can work very well on some implementations.

+2
Jan 20 '12 at 23:08
source share

As you can assume: cout and wcout are two different streams, and the quotes you provided do not talk about how the orientation of the streams correlates with the basic orientation of the file. Maybe the threads are quietly reorienting stdout under the hood?

0
Jan 21 2018-12-01T00:
source share



All Articles