C++ Tutorial

Class Templates

Generic classes, containers, and partial specialization

What are Class Templates?

In C++, code is often the same for different data types. For example, a Stack class remains the same for storing both an int and a string. Instead of writing multiple versions of the same class (which is hard to maintain!), you can write a single "generic" Class Template.
cpp
// Template: Any type 'T'!
template <typename T>
class Box {
public:
    T value;

    void set(T v) { value = v; }
    T get() const { return value; }
};

// Compiler generates 'int' version!
Box<int> intBox;
intBox.set(10);

// Compiler generates 'string' version!
Box<std::string> strBox;
strBox.set("Alpha");

How It Works: "Template Instantiation"

Wait! The compiler doesn't actually compile the template itself. When you use Box<int>, the compiler reads your template and generates a real, hidden class specifically for int during compilation. This means templates have zero runtime overhead! They are as fast as if you had written the classes manually.

CTAD (Class Template Argument Deduction)

Since C++17, you often don't have to provide the type (<int>). The compiler can "deduce" it from the constructor arguments.
cpp
Box myBox{100}; // Automatically deduces 'int'!

Handling Multiple Types

What if you want to store two different types of data? Use multiple template parameters!
cpp
template <typename T, typename U>
struct Pair {
  T first;
  U second;
};

// Deduces both types!
Pair result{1, 5.5}; 

Complete & Partial Specialization

Sometimes you need a completely different version of the template for one specific type (like a bool).
cpp
// Partial: Specialization for pointers!
template <typename T>
class Box<T*> {
    // Specialized logic for pointer safety!
};

// Complete: Specialization for bool!
template <>
class Box<bool> {
    // Specialized logic for a boolean!
};

Summary

FeatureSyntaxBehavior
templatetemplate <typename T>Define a generic class
InstantiationBox<int> bCompiler generates the class code
CTADBox b{100}Compiler guesses the type! (C++17)
Specializationtemplate <>Custom logic for one specific type
Partialtemplate <T*>Custom logic for a group of types
Best ForContainers, smart pointers, generic objects
PerformanceZero runtime cost (Same as manual code!)
Binary Size"Code Bloat": Big binaries; one version for EACH type!
HeaderTemplates must be defined ENTIRELY in headers!
STLStandard containers (vector, map) are all templates!
ConceptsUse concept (C++20!) to restrict allowed types!
DependentTypes that depend on template parameters
Meta"Template Metaprogramming" (Coding during compilation!)
typenameCan also use class instead (They are identical!)
Defaulttemplate <typename T = int> (Default type!)
Non-typetemplate <int N> (Template using a constant constant!)
SafetyErrors found during compilation, not crash!
Type CheckUse std::is_integral (C++11 type traits!)
Aliastemplate <typename T> using MyBox = Box<T>;
Arraystd::array<int, N> is a classic class template!
ConstraintUse enable_if for older C++ versions
ModernConcepts make class templates much safer!
FriendlyA template can have friends of the same type!