Template
Compilation Errors
Learn to recognize and decode compile-time errors that arise from C++ templates: why they happen, what they look like, and how to fix them.
What Are Templates?
Templates let you write generic code that works with any data type. Instead of writing separate functions for int, double, and string, you write one template and let the compiler generate the specific versions.
Two-Phase Compilation
C++ compiles templates in two distinct phases. Understanding this is the key to understanding why template errors are so different from regular errors.
Why Are Template Errors Hard?
Core Rule: Instantiation Triggers Errors
A template is not fully checked until you actually use it with a specific type. The compiler cannot verify that T supports an operation until it knows what T is.
Anatomy of a Template Error
Template error messages have a predictable structure. Learning to read them is a critical skill.
Dependent vs Non-Dependent Names
The compiler treats names differently depending on whether they depend on the template parameter T. This distinction controls when errors are reported.
| Dependent Name | Non-Dependent Name | |
|---|---|---|
| Depends on T? | Yes | No |
| Checked when? | Phase 2 (instantiation) | Phase 1 (definition) |
| Example | a + b (where a is type T) | std::cout |
| Error timing | Only when you call with bad T | Immediately, even if never called |
Five Categories of Template Errors
operator<)How to Diagnose Template Errors
Example 1: Missing Operator
Example 2: Deduction Failure
Example 3: Missing typename Keyword
Example 4: Cascading Error Avalanche
One mistake inside a template used by the STL can produce hundreds of lines of error output. Here is a simplified example:
Common Mistakes
typename keyword. Compilers vary in how they report this.Error Type Summary
| Error Category | Cause | Fix |
|---|---|---|
| Missing operator | T lacks required operator | Define the operator for T |
| Deduction failure | Conflicting types for T | Specify T explicitly |
| Missing typename | Dependent nested type | Add typename keyword |
| Ambiguous overload | Multiple templates match | Add constraints or SFINAE |
| Cascading errors | One error in STL chain | Fix the first error only |
Question 1 of 5
A template function uses a < b to compare two values of type T. You call it with a custom struct that has no operators defined. When does the compiler report the error?
operator< until you actually call the function with that type.Question 2 of 5
Consider the following template function:
template <typename T>
T multiply(T a, T b) { return a * b; }
And this call in main():
multiply(5, 2.0);
What error does this produce?
multiply<double>(5, 2.0).Question 3 of 5
You see this error: "need 'typename' before 'Container::value_type'". What does this tell you?
Question 4 of 5
A single call to std::sort on a vector of your custom struct produces 47 lines of error output. What is the most likely root cause?
operator< by default. When your type doesn't provide it, the error cascades through multiple levels of STL templates, producing many lines. The fix: define operator< for your struct or pass a custom comparator to sort.Question 5 of 5
In two-phase template compilation, when is a non-dependent name (like a misspelled standard library function) checked?