3 ms·
C++ templates are not expanded before type checking. Template instantiation is type driven. In fact you can even conditionally expand a template on whether an a
by gpderetta 6y ago
C++ templates are not expanded before type checking. Template instantiation is type driven. In fact you can even conditionally expand a template on whether an arbitrary expression typechecks. You wouldn't be able to do it if templates were a different phase.
Only early extremely non conforming compilers had macro-like template expansion.
Edit: although calling templates type level macros wouldn't be completely wrong.
Edit2: a better definition is that templates are type level functions: they take compile time values, types and other type functions and return values, types and type level functions.
- zenhack 6y agoFair enough; I suppose I was oversimplifying. I think the broader point I was trying to make (in addition to the specific downsides I mentioned) still holds though. The Template Haskell paper has some interesting things to say on the nature of C++ templates: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/meta-haskell.pdf https://www.microsoft.com/en-us/research/wp-content/uploads/...