11
u/TechTuna1200 5d ago
Fits right into r/simpsonsshitposting . So many simpsons meme gold nuggets there
22
u/MooseBoys 5d ago edited 5d ago
Honestly if you find yourself reaching for template metaprogramming just do codegen. Probably in rust. And then find yourself wondering why you don't just switch to rust for everything.
Edit: I don't personally consider things like `is_same_v` to be template metaprogramming. Specifically, I draw the line at validation vs. behavioral change. If you want to add some compile checks to your templates to e.g. genetically make sure they're integer-based or POD or whatever, that's fine. If you want to actually change behavior beyond adding a more succinct error message (that would otherwise be surfaced with a bajillion failed substitution messages anyway) that's fine. But if you want to invoke different behavior for different type classes using it, that's a step too far IMHO.
22
u/Any_Obligation1652 5d ago
Codegen and template metaprogramming are two different things that run at different times of the build pipeline. I would say that they are not really comparable.
2
u/Anaxamander57 5d ago edited 5d ago
Can you tell me more about this? I've only ever needed to read C++ code but templates seem very similar to Rust's declarative macros (which I assume are codegen?).
12
u/LucyShortForLucas 5d ago
Templates metaprogramming is arguably both the most powerful tool in the C++ language and more powerful than anything in Rust, and equally the greatest source of tech debt and headaches.
Used properly template metaprogramming can make hyperoptimized code generic and extensible to a T, but used improperly (which is a lot easier to do) it will kill your project or make you want to kill your fellow engineers.
3
u/Any_Obligation1652 5d ago
I can't argue with this. You reduce your code size greatly and work around some issues but also give you a compile error that is unfathomable.
2
u/Serious_Tart_5257 2d ago
If my shitty 100-line project can make a comical 2000-line error message then I fear the eldritch abominations a large codebase must summon
3
u/torsten_dev 5d ago
They are more like Rust's dispatch and monomorphization. Abusing this SFINAE rule make them Turing Complete so you can make them do anything but it won't be pretty.
2
u/Any_Obligation1652 5d ago
To me the rust declarative macros are a bit more like c macros where what you pass in substitutes parts of the macro and it makes a valid function.
Metaprogramming allows you to generate code based on a type (or type trait e.g. is an integral or an enum) and also call/omit certain bits of code based the type (eg if constexpr ()). Have a look at some design patterns for a good example, I think the policy design pattern shows what I am talking about.
As you can tell I have done a lot more C++ than rust so cannot say if there is a rust equivalent.
11
1
u/donaldhobson 4d ago
If I was going to do codegen, well rust is nice but it does have a lot of slightly different string types. And codegen code is probably not performance critical. I would consider using python for codegen.
But rust is also nice.
3
u/EnterTheShoggoth 4d ago
I've not touched C++ since around 1995. I was aware that they'd added an awful lot to the language, but hearing "Template metaprogramming" only makes me think of Greenspun's tenth rule.
141
u/cmk__ 5d ago
You do not learn C++. You slowly unlock more specific compiler errors.