C++ Logo

std-proposals

Advanced search

Re: [std-proposals] constexpr support in std::filesystem API

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 12 Mar 2024 07:29:44 -0700
On Tue, Mar 12, 2024 at 7:15 AM Sebastian Wittmeier via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> To be fair:
>
> How much different is it in power from a C23 #embed?
>
> Both feature that certain files, the ones which the implementation allows,
> can be fully (binarily) read at compile time.
>
> Differences:
>
> - the filepath can be created at runtime -> better hiding of the file
> read read; C23 probably supports macros for the filepath?
>
> - the contents can be processed by constexpr functions -> probably the
> same can be achieved by mixing C23 with C++ constexpr
>
> - the files could be read several times -> Denial of Service build; a
> long build can probably be achieved by other ways
>

The big one is the the filepath can be created at "runtime." Yes, #embed
allows you to use macros — for example you can now write a quine as
https://godbolt.org/z/hTj8jvcf8
— but you can't actually smuggle in a fully constexpr-evaluated string like
https://godbolt.org/z/v74GdjKse

constexpr char myfilename[] = "test.txt";
constexpr char data[] = {
#embed (myfilename) // doesn't work
};

So #embed doesn't seem to be useful as a "constexpr filesystem,"
unfortunately.

Similarly, AFAICT no vendor supports `_Pragma(mystring)` with mystring a
constexpr-evaluated string; the argument must be literally a string
literal. But IIUC, C++26 *is* getting `static_assert(false, mystring)` —
not with char arrays, but at least with strings and string_views. Example:
https://godbolt.org/z/vdjf9KP3E

my $.02,
–Arthur

Received on 2024-03-12 14:29:59