C++ Logo


Advanced search

Re: [std-proposals] Add an interface to std::fstream::open to open unnamed file wb+

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 25 Apr 2023 11:35:26 -0700
On Tuesday, 25 April 2023 11:03:48 PDT Louis Langholtz via Std-Proposals
> For me, opening an fstream with no filename means opening an unnamed file.

An unnamed file is not the same as a randomly-generated name. Linux does
support creating unnamed files, using the O_TMPFILE option, but they can't be
created in all filesystems and this also runs afoul of users needing a name in
their code. So we need to have both functionalities: unnamed files and files
with randomly-generated names.

QTemporaryFile supports both. It will try to create an unnamed file in the
directory you asked of it and fall back to randomly-generated names otherwise.
And if you ask it for the name, it'll materialise the file by using the
linkat() trick.

In any case, Jason's concern was about naming this function simply open(). I
agree with him: that's a horrible API. It sounds like it should open a file
whose name was previously specified, such as reopening a previously opened file.
QFile has an open() with no parameters, but that's because it also supports
passing the file name in the constructor or via setFileName. Instead, this
should have a better name or that it be added to a class derived from fstream
specifically for temporary files (like QTemporaryFile is to QFile), or both.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-04-25 18:35:28