3 ms·
With constexpr and not consteval you can really do crazy stuff at compile time. It's super neat and it has eradicated macros everywhere in my code, except in th
by qalmakka 5y ago
With constexpr and not consteval you can really do crazy stuff at compile time. It's super neat and it has eradicated macros everywhere in my code, except in those places where I need conditional compilation.
- jahnu 5y agoI really wonder why I'm finding very few uses of constexpr in my code. I feel like I'm missing out on something important but just can't find places where it works for me.
- qalmakka 5y agoHere's some examples: 1. Declare all your constants as constexpr. They can then be used everywhere in constexpr functions, and every time you invoke a constexpr function with constexpr arguments or literals there's a very high chance it will be executed at runtime. You can make expressions clearer and express stuff with functions, without paying the cost of the invocation. For instance, Intellisense and Clangd often show the value of very complex expressions on hover. 2. `if constexpr` is very effective to execute code conditionally in functions or lambdas with `auto` parameters, you just need to combine it with `std::same_as` or `std::is_same_v`. This is immensely useful with std::variant and std::visit, for instance.
- secondcoming 5y agoYes. Just last week I replaced a std::enable_if that need 3 functions with one function that used 'if constexpr'. I'm sure I'll find more places soon.