C++ Templates
Purpose
Guide agents through reading and fixing template error messages, using concepts as cleaner constraints, understanding SFINAE vs concepts trade-offs, and profiling template instantiation depth and compile times with Templight.
Triggers
- "How do I read this massive C++ template error?"
- "How do I use concepts to constrain a template?"
- "What's the difference between SFINAE and concepts?"
- "My templates make compilation very slow"
- "How do I write a requires-clause?"
- "How do I profile template instantiation times?"
Workflow
1. Reading template error messages
Template errors print full instantiation chains. Strategy: read from the bottom up.
Rules for reading:
- Find the first error line (top of output) — that's your code
- Skip all the
note:lines until you find "required from here" or "in instantiation of" - The bottom of the stack shows the type that failed substitution
2. SFINAE — legacy constraint technique
SFINAE (Substitution Failure Is Not An Error) silently removes overloads that fail substitution:
SFINAE errors are cryptic. Prefer concepts (C++20) for new code.
3. Concepts — modern constraints (C++20)
4. Requires expressions
5. SFINAE vs concepts comparison
Migration: replace enable_if with concept constraints; replace void_t helpers with requires.
6. Template instantiation profiling with Templight
7. Reducing template compile times
Related skills
- Use
skills/build-systems/build-accelerationfor ccache and PCH to reduce overall compile time - Use
skills/compilers/clangfor Clang-specific diagnostics and concept error output - Use
skills/low-level-programming/cpp-coroutinesfor another advanced C++20 feature


