2 ms·
> Arguably, the decision to keep using the C linker and use name mangling and template expansion in the compiler instead of making the linker smarter (or adding
by cbsmith 14y ago
> Arguably, the decision to keep using the C linker and use name mangling and template expansion in the compiler instead of making the linker smarter (or adding a phase between compilation/assembly and linking) was a mistake.
I don't know that it was a mistake per se, but this problem with template compilation overhead is definitely a seemingly inevitable consequence.
> Certainly incremental compilation and linking would be easier if template expansion were performed on object code (or a low-level intermediate representation) rather than source code.
This is effectively what happens... it just happens at compile time instead of link time. Given this template:
<bar.hpp>
template <typename foo>
foo bar(foo x) { return foo; }
a compiler is asked to compile:
#include <bar.hpp> //source code is parsed and intermediate representation generated by the compiler
int x = bar(some_int); //performs template expansion on said object code
float y = bar(some_y); //performs template expansion on said object code
//bar.hpp's source is examined only once!
This is why you can't do some things in templates that you can do with say, C macros (and vice-versa).