C++ - Advanced Templates & Metaprogramming

Overview

Estimated time: 80–100 minutes

Build powerful generic code with modern template techniques: variadics, folds, constraints, CRTP, and type detection idioms.

Learning Objectives

  • Write variadic templates and use fold expressions.
  • Use concepts/requires instead of SFINAE where possible.
  • Recognize CRTP and detection idiom patterns.

Prerequisites

Variadic + fold

template
auto sum(Ts... xs){ return (xs + ... + 0); }

Detection idiom (C++17) and concepts (C++20)

template
struct has_size: std::false_type{};
template
struct has_size().size())>>: std::true_type{};

Common Pitfalls

  • Overcomplicating templates where simpler overloads or runtime polymorphism would suffice.