r/cpp • u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting • Apr 28 '26
Things C++26 define_static_array can’t do
https://quuxplusone.github.io/blog/2026/04/24/define-static-array/
43
Upvotes
4
u/feverzsj Apr 29 '26
Just use
std::inplace_vector.2
u/friedkeenan Apr 29 '26
You can get a mutable static array by just copying out the spliced-in result of
std::meta::reflect_constant_array: https://godbolt.org/z/Y9s3ossoP2
u/UnusualPace679 Apr 30 '26
It's interesting that string literal objects cannot be template arguments, but the results of
std::define_static_stringcan.C++26 allows us to convert a string literal to a template argument with something like:
But I hope that we don't need to do this dance.
2
u/13steinj Apr 29 '26
I've been thinking about this a lot lately. I had to explain this "compiler's imagination" to a friend, they didn't get it, and to be fair, that claim is not that accurate (I eventually got the point across by saying "pocket universe outside of time", which gets through to Doctor Who fans I guess, but beside the point).
I don't understand the need for a two-step (nor this wrapping function). The compiler is smart enough to hold any number of these intermediate states "in it's imagination", I don't see why it can't
I believe this gets you the most sane result without having to teach arcane concepts. It's not the most performant option, but I'd say thats on the optimizer's quality of implementation.
-7
u/baby_shoGGoth_zsgg Apr 29 '26
wait 3-5 more c++ standards releases and someone will add a new std::whatever that is even more complex to address this issue with even more complexity and completely fail to resolve anything other than adding more complexity
0
u/[deleted] Apr 28 '26
[deleted] — view deleted comment