r/cpp_questions • u/SoerenNissen • 5d ago
OPEN Can you recommend a good source for learning constexpr/consteval patterns?
The specific thing I'm thinking of right now is a function that looks like:
auto func(auto... args)
{
for each arg {
if arg is type T {
do a thing with T
}
else if arg is type U {
do a thing with U
}
else {
compile error
}
}
}
but I'm not just looking to solve just this specific issue, I'd like to learn a substantial amount of this skillset.
4
Upvotes
7
u/fm01 5d ago
Maybe tell us where you feel like you're missing knowledge, because if clauses like that are not any different from runtime clauses except you put 'if constexpr' and a compile time condition. There's really not much difference in structure between runtime and compile time code, you just need to check that you only use classes and functions that can be compile time evaluated
3
u/n1ghtyunso 5d ago
typical patterns are partial class template specialization and certain fold expression tricks.
looking up that alone should already give you a nice set of tools.
Apart from that, its really vague on what exactly you are looking for generally.
3
u/TotaIIyHuman 5d ago
if you need early return
you can do short circuiting with
&&instead of,for foldingprobably code a lib that involves lots of meta programming
-1
u/New-Step3500 5d ago
I made a video on compile time evaluation https://youtu.be/9-8GPx2UdBo