35 ms·
> they are integrated into the semantics of eg. overload resolution in a way syntactic macros obviously can't be. Exactly, they are macros that can only be inv
by choeger 5y ago
> they are integrated into the semantics of eg. overload resolution in a way syntactic macros obviously can't be.
Exactly, they are macros that can only be invoked via some weird implicit entry points. Proper macros could be used very much in the same way, of course, if the compiler recognizes their output correspondingly. You could, for instance, use macros to generate traits/type classes to enhance overloading. Why someone would prefer this code generation to be so implicit, I don't understand.
> I don't see why decidability should render them incomparable.
Of course you can compare them if you want to point out that particular difference. But if you claim that templates are more powerful, the obvious answer is: Sure, because they're not type safe. Templates simply aren't a type system extension, they're more like macros.
- MauranKilom 5y ago> Sure, because they're not type safe. Templates simply aren't a type system extension, they're more like macros. Could you elaborate in which way C++ templates are not type-safe? Does the upcoming concepts feature alleviate any of it?
- choeger 5y agoA type safe language has a deterministic, terminating algorithm that can prove the absence of certain errors in a program. For instance you cannot use integers instead of functions in well-typed a Haskell program whereas you can make this mistake in a python program. C++ Templates are not type safe because they need to be evaluated before you can make any statement about their successful, correct application. This evaluation might not terminate. Macros habe the same issue, of course, but they explicitly give you a Turing-complete programming language so it feels more natural.
- moth-fuzz 5y agoC++ is a fully compiled language, and will not compile code that has type errors (barring you hack your way out of the spirit of the type system). Templates are no exception - they are type-safe once instantiated because they transform at that point into normal C++, which is type safe. Templates themselves are not types and are not meant to be, they’re meant to be templates, yes, more akin to macros, but not at any cost or defecit because of it. Frankly I don’t understand the criticism; I don’t care if I get a compiler error during template evaluation or during compilation or during linkage, a compiler error is a compiler error. Code that works works and code that doesn’t doesn’t.
- choeger 5y ago> Frankly I don’t understand the criticism; I don’t care if I get a compiler error during template evaluation or during compilation or during linkage, a compiler error is a compiler error. You might want to consider a setup where the library author is not the same person as the library user. Don't you think it's important that the former sees and not the latter? That's why linker errors and compiler errors and template errors are not equal. Also, if you make an error in a template or its instantiation you might not even get an error but an infinite (i.e., maybe aborted) computation. If you don't see the difference between these things, I cannot help you.