4 ms·
> The issue with templates is that you can’t really type check them at the point of definition. Instead they need to be checked each time the template is used.
by signa11 6y ago
> The issue with templates is that you can’t really type check them at the point of definition. Instead they need to be checked each time the template is used.
> This can make compilation slow but it can also make template writing very hard because a bug might only appear when a template is instantiated with certain types.
yup. templates are 'compiled' in 2 phases as follows:
- without instantiation at definition time, the code itself is checked for correctness, completely ignoring the template parameters.
- at instantiation time, the code is checked again to ensure that all parts depending on template parameters are valid.
the 2-phase translation breaks canonical notions about compile+link, where declarations of function is sufficient to compile its use...