Templates are not like normal functions or classes—the compiler doesn’t generate code for them immediately. Instead, templates act like blueprints. Actual code gets produced only when the compiler needs a concrete type.
When the compiler first sees a template, it parses the structure but does not generate machine code.
Example:
template <typename T>
T add(T a, T b) {
return a + b;
}
The compiler:
T supports + yetThis “syntax-only” checking is why invalid templates may compile until they are instantiated.
The compiler generates actual code only when the template is used with a specific type:
int x = add(1, 2); // Instantiates add<int>
double y = add(1.5, 2.0); // Instantiates add<double>
Now it checks:
int + int make sense? ✔double + double make sense? ✔If you try something invalid: