C++ Templates Cheat Sheet
Covers C++ function and class templates, template specialization, variadic templates, and concepts for writing generic, type-safe code.
3 PagesAdvancedApr 15, 2026
Function Templates
Write one function that works across multiple types.
cpp
template <typename T>T myMax(T a, T b) { return (a > b) ? a : b;}int i = myMax(3, 7); // T deduced as intdouble d = myMax(1.5, 2.5); // T deduced as doublestd::string s = myMax<std::string>("abc", "abd"); // explicit instantiation
Class Templates
Parameterize an entire class by type.
cpp
template <typename T>class Stack {public: void push(const T& value) { data.push_back(value); } void pop() { data.pop_back(); } T& top() { return data.back(); } bool empty() const { return data.empty(); }private: std::vector<T> data;};Stack<int> intStack;intStack.push(42);// Non-type template parametertemplate <typename T, size_t N>class FixedArray { T data[N];};FixedArray<int, 10> arr;
Template Specialization
Provide a custom implementation for a specific type.
cpp
template <typename T>struct TypeName { static std::string get() { return "unknown"; }};// Full specializationtemplate <>struct TypeName<int> { static std::string get() { return "int"; }};// Partial specialization (only allowed for class/struct templates)template <typename T>struct TypeName<T*> { static std::string get() { return TypeName<T>::get() + "*"; }};
Variadic Templates
Accept an arbitrary number of template arguments.
cpp
template <typename T>T sum(T v) { return v; }template <typename T, typename... Args>T sum(T first, Args... rest) { return first + sum(rest...); // recursive parameter pack expansion}sum(1, 2, 3, 4); // 10// C++17 fold expression, no recursion neededtemplate <typename... Args>auto sumFold(Args... args) { return (args + ...);}
Key Concepts
Terminology for reasoning about templates.
- Template Instantiation- The compiler generates concrete code for each distinct set of template arguments used.
- SFINAE- "Substitution Failure Is Not An Error" - invalid substitutions remove an overload from consideration instead of erroring.
- Concepts (C++20)- Named compile-time predicates that constrain template parameters, e.g. template<std::integral T>.
- Type Trait- A compile-time metafunction like std::is_integral<T> or std::enable_if, from <type_traits>.
- Two-Phase Lookup- Template code is checked at definition (non-dependent names) and again at instantiation (dependent names).
Pro Tip
Use C++20 concepts (e.g. template<std::integral T>) instead of std::enable_if SFINAE tricks when available - they give far clearer compiler errors and self-documenting constraints.
Was this cheat sheet helpful?
Explore Topics
#CTemplates#CTemplatesCheatSheet#Programming#Advanced#FunctionTemplates#ClassTemplates#TemplateSpecialization#VariadicTemplates#OOP#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance