A brief guide to C++ template metaprogramming
github.com
github.com
Handle errors appropriately, don't wrap a C API like that for gods sake, thats just wasted code trying to be clever.
In 5 years time, someone will look at that and have to try and figure out what the you were trying to do.
Additionally, the author implements count in the worst way possible, it's highly inefficient and it will result in very long compilation time. You don't need to do tail recursion, you can just write:
template <class... T> using count = std::integral_constant<std::size_t, sizeof...(T)>;
Such errors make me uncomfortable as it seems the author isn't up to date on the topic. A+ for intent though.
Too many clever people saw 3 repetitions of a pattern and decided to show case every template trick to create a "generic library" that is compile-time optimized, usually to solve a problem that doesn't exist.
Multiply that many times through the course of a project and you end up with code that seems to try to maximizes job security.
On the other hand, I'm the exact opposite. I hate overly complex solutions to intrinsically simple problems. I use abstractions sparingly and only when they reduce overall, not just local complexity. Sometimes moving down a level of abstraction or two will yield a better solution. Write simple code. Focus on programming and solving the problem, not metaprogramming and the metaproblem. If you're spending weeks to write (and debug!) a huge "generic solution" spanning a dozen files and several thousand lines of code which someone else could do in an hour with a few dozen lines of code in one file, I don't think that's being clever at all. I've personally experienced this, being that "someone else".
The C++ purists will probably find this repulsive, but in some cases a macro will be the far simpler solution. What templates can do are not a strict superset of what the preprocessor can do.
"All problems in computer science can be solved by another level of indirection, except those caused by too many levels of indirection."
There are some things that can't be done via templates and you have to resort to the preprocessor, but for everything else, I will choose templates.
I do agree that premature generalization, in all forms, is often the root of all evil.
C++ templates are Turing complete, but the C preprocessor is not.
C++ template is an incredibly complex but also flexible and powerful beast that has no equivalent apart maybe the macro system of scheme. C++ templates allows to change the semantic of the program as if the language was changed. Genericity is just a special case of usage of C++ template. Its true nature is to extend the C++ language.
But the other thing about the Scheme macro system is that while its (relative) simplicity, elegance, safety, and power lead to its adoption by other languages (most famously, JS (in the form of an external preprocessor) and rust), it's actually quite underpowered by Lisp standards, and thus most implementations provide a nonstandard system for hygenic imperative macros, usually either er/ir macros, sc/rsc macros, or syntax-case. Of the three, I'd say syntax-case is an overly complex mess, and that the other two both have merits.
So since TMP is doing similar substitutions (AFAICT: I haven't done the deep dive) to syntax-rules, it's got very real, and relatively common, limits. And as C++ slowly expands in scope, it will have to deal with problems that syntax-rules has already solved (what if a function creates a lambda or takes a function pointer argument called ioctl, and your template expansion calls ioctl?), and TMP hasn't AFAICT.
And even when they fix all that, they'll still have C++, which is about as regular as the Linux Kernel relase schedule combined with The DevTeam's.
They are also unlike scheme macro system which works at the AST level; C++ templates work at the type level, which scheme lacks entirely[1].
> what if a function creates a lambda or takes a function pointer argument called ioctl, and your template expansion calls ioctl?
Are you talking about macro hygiene? C++ templates are hygienic, except for type dependent lookup which intentionally isn't.