C++ Logo

sg16

Advanced search

Re: Follow up on SG16 review of P2996R2 (Reflection for C++26)

From: Peter Dimov <pdimov_at_[hidden]>
Date: Fri, 3 May 2024 11:10:16 +0300
Jens Maurer wrote:
> > The _streambuf_ then transcodes using the codecvt facet of the imbued
> > locale.
> >
> > https://eel.is/c++draft/filebuf#general-7
> > https://eel.is/c++draft/filebuf#virtuals-10
>
> But only for filebufs (i.e. std::fstream). Is there any evidence the standard
> prescribes that for std::cout as well?

You already asked this question. :-)

No, the standard doesn't explicitly say that the streambuf of std::cout
is a filebuf, it only says that it's associated with `stdout`.

Nevertheless, a filebuf-like behavior is what users expect, and
implementations generally implement.

E.g. MS STL uses a filebuf for cout:

https://github.com/microsoft/STL/blob/8dc4faadafb52e3e0a627e046b41258032d9bc6a/stl/src/cout.cpp#L17
https://github.com/microsoft/STL/blob/8dc4faadafb52e3e0a627e046b41258032d9bc6a/stl/src/cout.cpp#L22

libc++ uses __stdoutbuf, which is derived from filebuf:

https://github.com/llvm/llvm-project/blob/9731b77e80261c627d79980f8c275700bdaf6591/libcxx/src/std_stream.h#L241

and handles the wcout case slightly differently under Windows,
but only if nothing has been imbued.

libstdc++ uses stdio_filebuf, derived from filebuf:

https://github.com/gcc-mirror/gcc/blob/7117e1f6bf6de25c1ff26c4d7abcc79b407ca221/libstdc%2B%2B-v3/include/ext/stdio_filebuf.h#L52

when sync_with_stdio is off, and stdio_sync_ filebuf:

https://github.com/gcc-mirror/gcc/blob/7117e1f6bf6de25c1ff26c4d7abcc79b407ca221/libstdc%2B%2B-v3/include/ext/stdio_sync_filebuf.h#L58

when sync_with_stdio is on.

stdio_sync_ filebuf is not derived from filebuf, and bypasses
the locale and writes directly to stdout using putc and putwc,
without translation.

Either way, it's clear from the general design of the streams
that the transcoding via codecvt to the final encoding is
expected to happen in the stream buffer and not in the
stream inserter.

Received on 2024-05-03 08:10:21