--- name: cpp-templates-metaprogramming user-invocable: false description: Use when C++ templates and metaprogramming including template specialization, SFINAE, type traits, and C++20 concepts. allowed-tools: - Read - Write - Edit - Grep - Glob - Bash --- # C++ Templates and Metaprogramming Template metaprogramming enables compile-time computation and code generation, creating flexible, efficient abstractions without runtime overhead. This skill covers function and class templates, specialization, SFINAE, type traits, and modern concepts-based template constraints. ## Function Templates Function templates enable writing generic algorithms that work with any type satisfying requirements. ```cpp #include #include #include // Basic function template template T maximum(T a, T b) { return (a > b) ? a : b; } // Multiple template parameters template auto add(T a, U b) -> decltype(a + b) { return a + b; } // Template with non-type parameters template size_t array_size(T (&)[N]) { return N; } // Template overloading template void print(T value) { std::cout << value << "\n"; } template void print(const std::vector& vec) { for (const auto& item : vec) { std::cout << item << " "; } std::cout << "\n"; } void function_template_examples() { auto max_int = maximum(10, 20); auto max_double = maximum(3.14, 2.71); auto max_string = maximum(std::string("abc"), std::string("xyz")); auto sum = add(5, 3.14); // int + double int arr[] = {1, 2, 3, 4, 5}; std::cout << "Array size: " << array_size(arr) << "\n"; print(42); print(std::vector{1, 2, 3}); } ``` ## Class Templates Class templates enable creating generic containers and data structures. ```cpp #include #include // Basic class template template class Stack { T* data_; size_t size_; size_t capacity_; public: Stack(size_t capacity = 10) : data_(new T[capacity]) , size_(0) , capacity_(capacity) {} ~Stack() { delete[] data_; } void push(const T& value) { if (size_ >= capacity_) { resize(); } data_[size_++] = value; } T pop() { if (size_ == 0) { throw std::underflow_error("Stack is empty"); } return data_[--size_]; } bool empty() const { return size_ == 0; } size_t size() const { return size_; } private: void resize() { capacity_ *= 2; T* new_data = new T[capacity_]; for (size_t i = 0; i < size_; ++i) { new_data[i] = data_[i]; } delete[] data_; data_ = new_data; } }; // Multiple template parameters template class Pair { Key key_; Value value_; public: Pair(const Key& k, const Value& v) : key_(k), value_(v) {} const Key& key() const { return key_; } const Value& value() const { return value_; } }; // Template with default parameters template> class Vector { // Implementation }; void class_template_examples() { Stack int_stack; int_stack.push(1); int_stack.push(2); std::cout << int_stack.pop() << "\n"; Stack str_stack; str_stack.push("hello"); Pair p("age", 30); } ``` ## Template Specialization Template specialization allows providing custom implementations for specific types. ```cpp #include #include // Primary template template class Container { T value_; public: Container(const T& value) : value_(value) {} void print() const { std::cout << "Generic: " << value_ << "\n"; } size_t memory_size() const { return sizeof(T); } }; // Full specialization for const char* template<> class Container { const char* value_; public: Container(const char* value) : value_(value) {} void print() const { std::cout << "C-string: " << value_ << "\n"; } size_t memory_size() const { return std::strlen(value_) + 1; } }; // Partial specialization for pointers template class Container { T* value_; public: Container(T* value) : value_(value) {} void print() const { std::cout << "Pointer: " << *value_ << "\n"; } size_t memory_size() const { return sizeof(T*); } }; // Function template specialization template bool is_negative(T value) { return value < 0; } template<> bool is_negative(bool value) { return false; // bool can't be negative } void specialization_examples() { Container c1(42); c1.print(); // Generic Container c2("hello"); c2.print(); // C-string int x = 10; Container c3(&x); c3.print(); // Pointer } ``` ## SFINAE (Substitution Failure Is Not An Error) SFINAE enables compile-time function selection based on type properties. ```cpp #include #include #include // Enable if type has begin() and end() template typename std::enable_if< std::is_same< decltype(std::declval().begin()), decltype(std::declval().end()) >::value >::type print_container(const T& container) { std::cout << "Container: "; for (const auto& item : container) { std::cout << item << " "; } std::cout << "\n"; } // Enable if type is arithmetic template typename std::enable_if::value>::type print_value(T value) { std::cout << "Number: " << value << "\n"; } // Enable if type is not arithmetic template typename std::enable_if::value>::type print_value(const T& value) { std::cout << "Non-number: " << value << "\n"; } // Using std::enable_if as template parameter template::value>> T safe_divide(T a, T b) { if (b == 0) { throw std::domain_error("Division by zero"); } return a / b; } // Tag dispatching (alternative to SFINAE) template void process_impl(T value, std::true_type /* is_pointer */) { std::cout << "Processing pointer: " << *value << "\n"; } template void process_impl(T value, std::false_type /* is_pointer */) { std::cout << "Processing value: " << value << "\n"; } template void process(T value) { process_impl(value, std::is_pointer{}); } void sfinae_examples() { std::vector vec{1, 2, 3}; print_container(vec); print_value(42); print_value(std::string("hello")); std::cout << safe_divide(10, 2) << "\n"; int x = 100; process(x); process(&x); } ``` ## Type Traits Type traits provide compile-time type information and transformations. ```cpp #include #include #include // Using standard type traits template void analyze_type() { std::cout << "Type analysis:\n"; std::cout << " Is integral: " << std::is_integral::value << "\n"; std::cout << " Is floating point: " << std::is_floating_point::value << "\n"; std::cout << " Is pointer: " << std::is_pointer::value << "\n"; std::cout << " Is const: " << std::is_const::value << "\n"; std::cout << " Size: " << sizeof(T) << "\n"; } // Type transformations template void transform_type() { using NoCV = std::remove_cv_t; using NoRef = std::remove_reference_t; using NoPtr = std::remove_pointer_t; using AddConst = std::add_const_t; using AddLRef = std::add_lvalue_reference_t; std::cout << "Is same after remove_cv: " << std::is_same::value << "\n"; } // Custom type trait template struct is_string : std::false_type {}; template<> struct is_string : std::true_type {}; template<> struct is_string : std::true_type {}; template inline constexpr bool is_string_v = is_string::value; // Conditional types template using MakeUnsigned = std::conditional_t< std::is_signed::value, std::make_unsigned_t, T >; // Compile-time if (C++17) template void print_type(const T& value) { if constexpr (std::is_integral_v) { std::cout << "Integer: " << value << "\n"; } else if constexpr (std::is_floating_point_v) { std::cout << "Float: " << value << "\n"; } else if constexpr (is_string_v) { std::cout << "String: " << value << "\n"; } else { std::cout << "Unknown type\n"; } } void type_traits_examples() { analyze_type(); analyze_type(); print_type(42); print_type(3.14); print_type(std::string("hello")); } ``` ## Variadic Templates Variadic templates enable functions and classes accepting any number of arguments. ```cpp #include #include // Base case void print_all() { std::cout << "\n"; } // Recursive variadic template template void print_all(T first, Args... rest) { std::cout << first << " "; print_all(rest...); } // Fold expressions (C++17) template auto sum_all(Args... args) { return (args + ...); } template auto multiply_all(Args... args) { return (args * ... * 1); } // Variadic class template template class Tuple; template<> class Tuple<> { public: static constexpr size_t size = 0; }; template class Tuple : private Tuple { Head head_; public: static constexpr size_t size = 1 + Tuple::size; Tuple(Head h, Tail... t) : Tuple(t...), head_(h) {} Head& head() { return head_; } const Head& head() const { return head_; } Tuple& tail() { return *this; } }; // Parameter pack expansion template void process_all(Args... args) { // Expand in initializer list int dummy[] = { (std::cout << args << " ", 0)... }; (void)dummy; // Suppress unused warning } // Index sequence for compile-time iteration template void print_indices(std::index_sequence) { ((std::cout << Is << " "), ...); std::cout << "\n"; } void variadic_examples() { print_all(1, 2.5, "hello", std::string("world")); auto total = sum_all(1, 2, 3, 4, 5); auto product = multiply_all(2, 3, 4); Tuple t(42, 3.14, "test"); std::cout << "Tuple size: " << decltype(t)::size << "\n"; print_indices(std::make_index_sequence<5>{}); } ``` ## Template Metaprogramming Template metaprogramming performs compile-time computation using templates. ```cpp #include // Compile-time factorial template struct Factorial { static constexpr int value = N * Factorial::value; }; template<> struct Factorial<0> { static constexpr int value = 1; }; // Compile-time Fibonacci template struct Fibonacci { static constexpr int value = Fibonacci::value + Fibonacci::value; }; template<> struct Fibonacci<0> { static constexpr int value = 0; }; template<> struct Fibonacci<1> { static constexpr int value = 1; }; // Type list manipulation template struct TypeList {}; // Get size of type list template struct Length; template struct Length> { static constexpr size_t value = sizeof...(Types); }; // Get element at index template struct At; template struct At> { using type = typename At>::type; }; template struct At<0, TypeList> { using type = Head; }; // Check if type is in list template struct Contains; template struct Contains> : std::false_type {}; template struct Contains> : Contains> {}; template struct Contains> : std::true_type {}; // Constexpr functions (C++11 and later) constexpr int factorial_constexpr(int n) { return (n <= 1) ? 1 : n * factorial_constexpr(n - 1); } constexpr int fibonacci_constexpr(int n) { return (n <= 1) ? n : fibonacci_constexpr(n - 1) + fibonacci_constexpr(n - 2); } void metaprogramming_examples() { // Computed at compile time constexpr int fact5 = Factorial<5>::value; constexpr int fib7 = Fibonacci<7>::value; std::cout << "5! = " << fact5 << "\n"; std::cout << "fib(7) = " << fib7 << "\n"; using MyTypes = TypeList; std::cout << "Type list length: " << Length::value << "\n"; using SecondType = At<1, MyTypes>::type; // double std::cout << "Contains int: " << Contains::value << "\n"; // Modern constexpr constexpr int fact6 = factorial_constexpr(6); std::cout << "6! = " << fact6 << "\n"; } ``` ## Concepts (C++20) Concepts provide named constraints for template parameters with better error messages. ```cpp #include #include // Basic concept template concept Numeric = std::integral || std::floating_point; // Concept with requirements template concept Addable = requires(T a, T b) { { a + b } -> std::convertible_to; }; // Concept with multiple constraints template concept Container = requires(T c) { typename T::value_type; typename T::iterator; { c.begin() } -> std::same_as; { c.end() } -> std::same_as; { c.size() } -> std::convertible_to; }; // Using concepts in function templates template T add(T a, T b) { return a + b; } // Concept as return type constraint template auto square(T x) -> std::same_as auto { return x * x; } // Multiple concept constraints template concept Sortable = std::totally_ordered && std::copyable; template void sort_values(std::vector& values) { std::sort(values.begin(), values.end()); } // Subsumption (concept refinement) template concept SignedNumeric = Numeric && std::signed_integral; template void process(T value) { std::cout << "Processing numeric\n"; } template void process(T value) { std::cout << "Processing signed numeric\n"; } void concepts_examples() { auto result = add(5, 10); // OK auto dresult = add(5.5, 2.3); // OK // auto sresult = add("hi", "there"); // Error std::vector vec{3, 1, 2}; sort_values(vec); process(5); // Calls SignedNumeric version process(5.5); // Calls Numeric version } ``` ## Best Practices 1. Use concepts instead of SFINAE in C++20 for clearer template constraints 2. Prefer `constexpr` functions over template metaprogramming for readability 3. Use `std::enable_if_t` and type trait `_v` and `_t` suffixes for conciseness 4. Document template requirements clearly even without concepts 5. Use `decltype(auto)` for perfect return type deduction 6. Prefer template specialization over SFINAE when full implementation differs 7. Use fold expressions instead of recursive variadic templates when possible 8. Mark template functions `inline` or define in headers to avoid linking errors 9. Use `static_assert` to validate template parameters at compile time 10. Prefer standard library type traits over custom implementations ## Common Pitfalls 1. Forgetting to define template member functions in headers, causing linker errors 2. Infinite template recursion without proper base cases 3. Complex SFINAE expressions that are hard to read and maintain 4. Not using `typename` keyword when referring to dependent types 5. Template bloat from unnecessary instantiations of large templates 6. Circular dependencies in template specializations 7. Ambiguous function overloads when multiple SFINAE conditions match 8. Excessive compile times from complex template metaprogramming 9. Not marking template `constexpr` functions as `constexpr` 10. Using templates when runtime polymorphism would be simpler and sufficient ## When to Use Templates and Metaprogramming Use templates and metaprogramming when you need: - Generic algorithms that work with multiple types - Compile-time computation and code generation - Zero-overhead abstractions without runtime cost - Type-safe interfaces with strong compile-time checking - Containers and data structures for any type - Expression templates for domain-specific languages - Policy-based design with compile-time configuration - Elimination of code duplication across similar implementations - Static polymorphism without virtual function overhead - Modern C++ libraries with flexible, composable components ## Resources - [C++ Templates: The Complete Guide](https://www.oreilly.com/library/view/c-templates-the/9780134778808/) - [Modern C++ Programming Cookbook](https://www.packtpub.com/product/modern-c-programming-cookbook-third-edition/9781835080542) - [C++ Template Metaprogramming](https://www.boost.org/doc/libs/1_82_0/libs/mpl/doc/index.html) - [cppreference Templates](https://en.cppreference.com/w/cpp/language/templates)