Skip to main content
Be Bitwise
C++What Your C++ Code Actually Does

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

cpp
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:

asm
; 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
ret

Three 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:

bash
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:

cpp
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:

bash
g++ -c vector_example.cpp -o vector_example.o
nm vector_example.o | c++filt | grep "Vector"
# Lists every method that was actually instantiated, per type

In 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:

bash
readelf -g vector_example.o
# Lists the COMDAT section groups - these can be deduplicated by the linker
nm -C vector_example.o | grep ' W '
# The instantiations show up as weak (W) symbols

Each 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:

cpp
// In a header - likely inlined
template<typename T>
T square(T x) { return x * x; }

At -O2:

asm
; Instead of calling square<int>, the compiler inlines it:
main:
mov eax, 5
imul eax, eax ; 5 * 5 = 25, no function call

There'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:

asm
; A larger template function that cannot be inlined:
_Z12process_listIiEvRSt6vectorIT_SaIS1_EE:
push rbp
mov rbp, rsp
; ... 50+ instructions ...
pop rbp
ret

constexpr and Compile-Time Evaluation

constexpr functions can be evaluated entirely at compile time:

cpp
constexpr int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
int main() {
int x = factorial(10);
return x;
}

At -O2:

asm
main:
mov eax, 3628800 ; factorial(10) = 3628800, computed at compile time
ret

The 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:

cpp
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:

cpp
template<typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type
process(T val) {
return val * 2;
}
template<typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type
process(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:

asm
; When called with int:
lea eax, [rdi+rdi] ; val * 2
; When called with double:
mulsd xmm0, QWORD PTR .LC_HALF ; val * 0.5

SFINAE, 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:

cpp
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:

asm
mov eax, 15 ; 1+2+3+4+5 = 15, computed at compile time

The 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:

bash
# Size of object file
ls -la example.o
# Count unique symbols
nm example.o | c++filt | wc -l
# Find the largest template instantiations
nm --size-sort example.o | c++filt | tail -20

In 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.