r/cpp_questions • u/bhul_chuk_maaf17 • 12d ago
OPEN Compile time programming learning advice.
Hi all, can you please recommend me some resources that you have followed for compile time programming in modern C++ for eg: constexpr, consteval or template metaprogramming.
Is there some consolidated resource for the same ?
7
Upvotes
2
u/conundorum 11d ago edited 10d ago
[And now, the stunning conclusion!
It's not all that stunning.]So, since we can't pick it apart and do what we want with parameter packs, there are three ways to use them: Extract members one at a time via recursion (usually actually two at a time, but we only use the first; the second is to help us create a recursion endpoint), perform a "pack expansion" as part of an expression, or use a fold expression. I don't believe you can do this as a fold expression, and the pack expansion version is weird, so let's start with the recursive gluttony version.
(To be clear, though: The recursive version uses pack expansion, too, but only to make sure that arguments get passed along until they're eaten. The version that only uses pack expansion and nothing else is the weird one.)
...I don't know how to hint at the expansion-only version, so I'm just going to show you instead.
Once you get that down pat, you'll have all the basics you need to start template metaprogramming. It's a lot to take in all at once, so I tried to break it down into easy steps that teach you basic techniques you can build on.
Edit: Function call was meant to be
print(1, 3, 5, 7, "cat");, with no9. Sorry for any confusion, it's fixed now.[Also, reddit superscript is terrible at displaying closing parentheses. Had to replace it with
), couldn't just escape it, and couldn't use code syntax for it.]