r/cpp • • Aug 06 '26

C++26: #embed

https://www.sandordargo.com/blog/2026/08/05/cpp26-embed
135 Upvotes

42 comments sorted by

View all comments

Show parent comment

2

u/Ambitious-Method-961 Aug 08 '26

So I went down the enum initialisation rabbit hole and initialising enums with integer literals in general is a no-no. This rule was relaxed for any enum with an explicit underlying type (including unscoped enums) to allow things like my_enum foo { 100 }; to work (assuming my_enum is an enum with an explicit underlying type). If there's more than one enum being initialised using that syntax then it's a compiler error.

Relaxing the rules to allow multiple enums to be initialised with literals is one option, or perhaps the other could be to add another param to #embed that adds in a cast option? Something like #embed "example.bin" cast_to (std::byte) which would then wrap each embedded integer with a cast to std::byte or any other value specified.

Assuming the included file was just 3 bytes with values 0, 1, 2 then this:

std::byte foo[] =  {
    #embed "example.bin" cast_to (std::byte)
};

Would expand to:

std::byte foo[] =  {
    std::byte (0), std::byte (1), std::byte (2)
};

And everything would work fine.

Anyway, that's a thought for someone far more versed in the language than me as I can already see a debate forming over whether it should be () or {} initialisation...