Skip to main content

15 docs tagged with "templates"

View all tags

auto Type Deduction

auto lets the compiler deduce variable types from initializers, reducing verbosity and improving maintainability.

Class Templates

Class templates create generic classes that work with different types. Think std::unique_ptr - same class, different types.

CRTP (Curiously Recurring Template Pattern)

CRTP is a template pattern where a class Derived inherits from a template base class Base, passing itself as a template argument. This enables compile-time polymorphism without virtual functions.

Dynamic vs Static Polymorphism

C++ supports two forms of polymorphism: dynamic (runtime, using virtual functions) and static (compile-time, using templates). Each has different trade-offs.

Fold Expressions (C++17)

Fold expressions provide a concise way to apply operators to variadic template parameter packs. They eliminate the need for recursive template patterns.

Full Template Specialization

Full specialization provides a completely custom implementation for specific template arguments. It's like saying "for this exact type, use this completely different code."

Function Templates

Function templates let you write one function that works with different types. The compiler generates specific versions for each type you use.

Partial Template Specialization

Partial specialization lets you specialize templates for a pattern of types, not just one specific type. It's like "for all pointer types" or "for all pairs of same types."

Policy-Based Design

Policy-based design decomposes complex behaviors into independent policy classes combined through templates. Each policy defines one aspect of behavior, and policies are mixed together to create flexible, reusable components.

Reference Collapsing Rules

When template type deduction creates "reference to reference", C++ applies collapsing rules. This enables perfect forwarding.

SFINAE and enable_if

SFINAE (Substitution Failure Is Not An Error) is a fundamental C++ template mechanism that enables conditional template compilation. std::enable_if is the primary tool for applying SFINAE in practice.

Template Argument Deduction

Template argument deduction lets the compiler figure out template parameters from function arguments automatically. No need to write func(5) when func(5) works!

Type Erasure

Type erasure hides the concrete type behind a common interface, allowing different types to be treated uniformly without inheritance. Combines the flexibility of runtime polymorphism with the performance benefits of templates.

Type Traits

Type traits are compile-time tools that query and transform types. They power template metaprogramming and SFINAE, letting you write code that adapts to different types automatically.

Variadic Templates

Variadic templates accept any number of arguments of any types. They're the foundation for functions like std::make_tuple, printf, and perfect forwarding.