Stay up-to-date with everything C++!
Content directly fetched from the subreddit just for you.
Join our group for discussions : @programminginc
Powered by : @r_channels
Post #24421
21
C+ @r_cpp
Showing posts older than #24422 · Back to latest
static_XXX analogs of standard containers and so on.
std::vector into constexpr std::span (not only std::vector in fact but lets focus on that).// this WON'T compile
// constexpr std::vector nsdm = nonstatic_data_members_of(^^T, std::meta::access_context::unchecked());
// this WILL compile
constexpr std::span nsdm = define_static_array(nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()));
std::span nor std::string_view are structured types! SO you cannot use them as NTTP! And you're forced to use old hacks to transform std::span and std::string_view into std::array, because std::array IS a structured type. constexpr char* (string literal, cannot be NTTP) and const char* constexpr (NOT a strign literal, can be NTTP). And this DOES matter when you're trying to use string literals as NTTP (for instance you wanna pass a name of a member as template argument and use it in you reflection). Yes there is a hack with static_string workaround, but static_string is effectively an std::array under the hoods, whereas define_static_string gives you const char* constexpr if I'm not mistaken. And now you have to somehow find a common ground between static_string (aka array) and const char* constexpr...std::span is a bad choise (why not std::array?!). template for but we lack some kind of spread functionalitytemplate for is good. But you may also want to spread your std::vector<std::meta::info> and initialize something using fold-expressions for instance (in general, you may want to spread variadic in any of allowed contexts). And here lies another issue: you can't easily do that using built-in C++26 reflection functionality - your are forced my write a hacky wrappers youself (overcoming all these issues with NTTP on the way). Overall constexpr metaprogramming and variadics don't work NICELY together, unfortunately.define_aggregate insideconsteval {
// this doesn't compile
// static constexpr auto cached_data = define_some_kind_of_aggregate(^^T);
}
define_aggregate a struct which is declared outside of your class.std::meta::is_complete_type. Imagine you implement different SoA containers and all of them share same reference type based on original TValue type. You can't do this using current proposal.define_aggregate meta-function, coz templates IS the way we are generating the code now.CONSTEXPR EVALUATION CONTEXT AND THE LACK OF GOOD SUPPORT FOR NTTP TEMPLATE ARGUMENTS in current C++. constexpr variable and 2. it has to be a structured type. So lets dive into these two statements. constexpr variable. This one is harder to achive as you may think. consteval function IS NOT a constexpr variable. Which means you cannot use it as NTTP or refactor you consteval function onto multiple smaller consteval functions (you're forced to pass it as NTTP which is not always possible because of NTTP restrictions). And you encounter this issue ALL THE TIME - you just write "your usual C++" consteval function (remember, this is our dream we aim for), but then suddenly you need this particular value inside of it to be constexpr 3 layers deep down the callstack... You refactor, make it constexpr (if you're lucky and you can do that) but then you realise that your for loop doesn't work anymore (coz you cannot have constexpr variable inside for loop), and you need to use template for loop instead. Also, you cannot use the addresses of constexpr variables (and iterators) which means you're range algorithms aren't always easy to use. And my guess that all of this won't change any time soon. std::vector. And std::vector cannot be constexpr (at least for now, do we plan to fix that in future releases of C++?) so you can't use it as constexpr variable. Which means you cannot use it as NTTP. Same thing for standard containers as std::map or std::set. And even if we WILL be able to use standard containers in as constexpr variable will they be structured types?...