Date: Sun, 7 Sep 2025 15:31:40 +0100
On Sun, 7 Sept 2025, 14:12 Jerome Saint-Martin via Std-Proposals, <
std-proposals_at_[hidden]> wrote:
> "Thanks for the reply. My point is precisely that the API allows a
> combination like ifstream with ios::out to compile — even though the
> semantics of the type make that combination nonsensical. That’s what I
> meant by 'illogical mode combinations'.
> Yes, you can extract the filebuf and do tricks, but that’s beside the
> point.
>
No it isn't. Your proposed open_file is equivalent to filebuf::open (which
is a wrapper around fopen). That supports opening a filebuf for reading or
writing, just like fopen.
The ifstream formatting layer is not responsible for opening the file, only
for providing the API for reading data from an open filebuf. It calls a
filebuf member function to open the file, and that opens it with the mode
you requested.
The issue is that the type and the mode should be coherent by design, and
> ideally, such contradictions should be caught at compile-time.
>
But the mode is a property of the filebuf not the ifstream, and the filebuf
type supports reading and writing. So it is coherent. Your mental model
just doesn't match the design.
If you intended to use the ifstream to write to a file and passing ios::out
was intentional, your error will be caught as soon as you try to use it
like an ostream.
std-proposals_at_[hidden]> wrote:
> "Thanks for the reply. My point is precisely that the API allows a
> combination like ifstream with ios::out to compile — even though the
> semantics of the type make that combination nonsensical. That’s what I
> meant by 'illogical mode combinations'.
> Yes, you can extract the filebuf and do tricks, but that’s beside the
> point.
>
No it isn't. Your proposed open_file is equivalent to filebuf::open (which
is a wrapper around fopen). That supports opening a filebuf for reading or
writing, just like fopen.
The ifstream formatting layer is not responsible for opening the file, only
for providing the API for reading data from an open filebuf. It calls a
filebuf member function to open the file, and that opens it with the mode
you requested.
The issue is that the type and the mode should be coherent by design, and
> ideally, such contradictions should be caught at compile-time.
>
But the mode is a property of the filebuf not the ifstream, and the filebuf
type supports reading and writing. So it is coherent. Your mental model
just doesn't match the design.
If you intended to use the ifstream to write to a file and passing ios::out
was intentional, your error will be caught as soon as you try to use it
like an ostream.
Received on 2025-09-07 14:32:00