3 ms·
Well, since templates are turing complete, I would tend to say that they are more powerful. But they work very differently. Templates do code generation and _Ge
by halosghost 11y ago
Well, since templates are turing complete, I would tend to say that they are more powerful. But they work very differently. Templates do code generation and _Generic is just a fancy-pants type-based macro-dispatch.
Like I said, I do not think _Generic is a bad thing (though it can get really messy—which is not to say that Templates can't), just that the example given in the OP is a bit contrived.
- dllthomas 11y agoIt is computationally more powerful, sure. If that's what you'd intended to be talking about, then fine. But that is a distinct notion from "you are able to do more with it" - which is typically the more relevant bit when discussing the power of a feature in a programming language outside a restricted academic context. (Incidentally, other things being equal - including ability to get done the thing you want to get done - "Turing complete" is a bug, not a feature!) Expanding a little - "Turing complete" means that your system is capable of computing anything that can be computed in some encoding, but that doesn't mean that it is linked to the rest of the world in a way that will do something useful with that encoding of the result. Token-manipulating C macros, while obviously of less powerful in the sense of computability, can do things that templates cannot do (name mangling, annotations, compiler pragmas) - and with _Generic, any of these can now be type directed. Of course, with great power comes the responsibility never to use half of that... but that's pretty true of C++ as well :p