Templates & Code Generation
Templates live entirely at compile time. For every type you instantiate them with, the compiler stamps out concrete, real code. We're going to peek at exactly what comes out the other side: how many copies of your code end up in the binary, what template bloat actually looks like in practice, and how the optimiser tames template-heavy programs.
One Template, Multiple Functions
template<typename T>T max_val(T a, T b) { return a > b ? a : b;}
int main() { int i = max_val(3, 5); double d = max_val(3.14, 2.72); long l = max_val(100L, 200L); return 0;}The compiler generates three completely separate functions:
; max_val<int>(int, int)_Z7max_valIiET_S0_S0_: cmp edi, esi mov eax, esi cmovg eax, edi ; Conditional move for int comparison ret
; max_val<double>(double, double)_Z7max_valIdET_S0_S0_: maxsd xmm0, xmm1 ; xmm0 = (xmm0 > xmm1) ? xmm0 : xmm1 ret
; max_val<long>(long, long)_Z7max_valIlET_S0_S0_: cmp rdi, rsi ; 64-bit comparison (vs 32-bit for int) mov rax, rsi cmovg rax, rdi retThree independent functions, each with its own machine code. The int version reaches for 32-bit registers and cmp. Doubles go to SSE, where a single maxsd happens to compute exactly a > b ? a : b. Longs run on 64-bit registers. Same source, three specialised bodies in the binary.
Demangle the names to see the template parameters:
c++filt _Z7max_valIiET_S0_S0_# max_val<int>(int, int)
c++filt _Z7max_valIdET_S0_S0_# max_val<double>(double, double)Template Bloat
Every fresh instantiation hatches new machine code. The result, when it gets out of hand, is template bloat:
template<typename T>class Vector { T *data; size_t size, capacity;public: void push_back(const T &val); T &operator[](size_t i); // ... 20 more methods};
// Each of these creates a separate copy of every Vector method it uses:Vector<int> v1;Vector<double> v2;Vector<std::string> v3;Vector<std::pair<int, int>> v4;Implicit instantiation is lazy: a member function of a class template is only generated if something in the program uses it. Call all twenty methods on all four types, though, and you're staring at up to 80 separate functions in the object file. Easy to verify:
g++ -c vector_example.cpp -o vector_example.onm vector_example.o | c++filt | grep "Vector"# Lists every method that was actually instantiated, per typeIn practice the linker drops unused functions and merges duplicates. Even so, projects that lean hard on Boost or the STL routinely watch their binaries balloon.
How the Compiler Avoids Redundant Instantiations
When several .cpp files all instantiate the same template with the same types, each one bakes its own copy. The linker cleans up the duplicates using COMDAT sections:
readelf -g vector_example.o# Lists the COMDAT section groups - these can be deduplicated by the linkernm -C vector_example.o | grep ' W '# The instantiations show up as weak (W) symbolsEach instantiation lands in its own COMDAT group. When the linker spots identical groups across object files, it keeps one and tosses the rest.
Inline vs Out-of-Line Instantiation
Templates defined in headers are typically inlined:
// In a header - likely inlinedtemplate<typename T>T square(T x) { return x * x; }At -O2:
; Instead of calling square<int>, the compiler inlines it:main: mov eax, 5 imul eax, eax ; 5 * 5 = 25, no function callThere's no square<int> symbol in the binary at all. The compiler pasted x * x straight into the caller. That's the dream scenario for templates: type-specialised, fully inlined, no overhead anywhere on the way.
Larger template functions that the inliner won't touch end up as plain old standalone functions:
; A larger template function that cannot be inlined:_Z12process_listIiEvRSt6vectorIT_SaIS1_EE: push rbp mov rbp, rsp ; ... 50+ instructions ... pop rbp retconstexpr and Compile-Time Evaluation
constexpr functions can be evaluated entirely at compile time:
constexpr int factorial(int n) { return n <= 1 ? 1 : n * factorial(n - 1);}
int main() { int x = factorial(10); return x;}At -O2:
main: mov eax, 3628800 ; factorial(10) = 3628800, computed at compile time retThe recursion is gone. Compilation handled 10! = 3628800 and dropped the answer in as a constant. Strictly, int x = factorial(10) doesn't force compile-time evaluation; the optimiser folds it because the argument is a constant. Write constexpr int x = factorial(10); (or use it as an array size or template argument) if you need the guarantee, even at -O0. No call, no recursion, no loop. Just an immediate.
Template Specialisation in Assembly
Template specialisation lets you provide type-specific implementations:
template<typename T>void print_type() { // Generic version}
template<>void print_type<int>() { // Specialized for int}
template<>void print_type<double>() { // Specialized for double}Each specialisation compiles down to its own function. Selection happens at compile time, so by the time the binary exists, the choice has already been made. No runtime dispatch involved.
SFINAE at the Binary Level
SFINAE (Substitution Failure Is Not An Error) is entirely a compile-time mechanism:
template<typename T>typename std::enable_if<std::is_integral<T>::value, T>::typeprocess(T val) { return val * 2;}
template<typename T>typename std::enable_if<std::is_floating_point<T>::value, T>::typeprocess(T val) { return val * 0.5;}No fingerprint of SFINAE survives into the assembly. The compiler picks the overload at compile time and emits whatever was specialised:
; When called with int: lea eax, [rdi+rdi] ; val * 2
; When called with double: mulsd xmm0, QWORD PTR .LC_HALF ; val * 0.5SFINAE, enable_if, type_traits, concepts from C++20 — all of it is compile-time machinery. None of it makes its way into the binary. The only thing it influences is which overloads the compiler emits at all.
Variadic Templates
Variadic templates use recursive instantiation:
template<typename T>T sum(T val) { return val;}
template<typename T, typename... Args>T sum(T first, Args... rest) { return first + sum(rest...);}
int result = sum(1, 2, 3, 4, 5);The compiler generates one instantiation per pack length, each calling the next, until the single-argument overload ends the recursion. Reading the tree bottom-up shows how the result is built:
Five functions! But at -O2, the compiler inlines all of them:
mov eax, 15 ; 1+2+3+4+5 = 15, computed at compile timeThe whole recursive template expansion folds away at compile time, leaving a single constant behind.
Measuring Template Impact
You can measure how much code templates generate:
# Size of object filels -la example.o
# Count unique symbolsnm example.o | c++filt | wc -l
# Find the largest template instantiationsnm --size-sort example.o | c++filt | tail -20In release builds with LTO turned on, the linker (now armed with whole-program info) can throw away template instantiations nothing actually calls. Bloat that survived the per-object pass dies here.
The Template Tradeoff
Templates buy you the best possible runtime performance: type-specific code, fully inlined, optimised to the hilt. The bill is paid in compile time and binary size, since you may end up with several instantiations of the same logic. At runtime the cost is zero or even negative, because the compiler often beats virtual dispatch by resolving everything up front and inlining the result.
That's the C++ creed in one sentence: abstractions shouldn't cost a thing at runtime. Templates honour it by handing the work to the compiler.