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:
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...
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 (assumingmy_enumis 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 tostd::byteor any other value specified.Assuming the included file was just 3 bytes with values 0, 1, 2 then this:
Would expand to:
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...