0Pricing
C++ Academy · Lesson

Function and Class Templates Revisited

Write generic functions and classes with proper template syntax.

Function and Class Templates Revisited is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Templates?

Templates let you write code once and have the compiler generate specific versions for each type used. Cornerstone of generic programming.

Function Template Syntax

Declare type parameters with template <typename T>. Use T in the function signature.

template <typename T>
T max_of(T a, T b) {
    return (a > b) ? a : b;
}

std::cout << max_of(3, 7);          // int
std::cout << max_of(2.5, 1.5);      // double

typename vs class

The keywords typename and class are interchangeable in template parameter declarations. typename is more common in modern style.

Multiple Type Parameters

Templates can have multiple type parameters.

template <typename T, typename U>
auto combine(T a, U b) -> decltype(a + b) {
    return a + b;
}

Non-Type Parameters

Template parameters can also be non-type values: integers, enums, pointers.

template <typename T, size_t N>
struct StaticArray {
    T data[N];
};

StaticArray<int, 10> arr;

Class Templates

Generic classes use the same template syntax. Use the type parameter for members and methods.

template <typename T>
class Stack {
    std::vector<T> data_;
public:
    void push(T x) { data_.push_back(std::move(x)); }
    T pop()        { T t = std::move(data_.back()); data_.pop_back(); return t; }
};

Stack<int> s1;
Stack<std::string> s2;

Member Functions Outside the Class

Definitions outside the class need the template prefix on every member.

template <typename T>
class Stack {
public:
    void push(T x);
};

template <typename T>
void Stack<T>::push(T x) {
    data_.push_back(std::move(x));
}

Default Template Arguments

Like default function arguments. Used when the caller does not specify.

template <typename T = int>
class Box {
    T value;
};

Box<> b;       // T is int

Template Argument Deduction

The compiler often deduces template arguments from the call.

template <typename T>
void print(T x) { std::cout << x; }

print(42);          // T deduced as int
print("hello");    // T deduced as const char*
print<double>(3);   // explicit

Class Template Argument Deduction (C++17)

C++17 lets you omit class template arguments when they can be deduced from the constructor.

std::pair p(1, 2.5);            // pair<int, double>
std::vector v{1, 2, 3};         // vector<int>

Two-Phase Lookup

Templates undergo lookup in two phases — at definition (non-dependent names) and at instantiation (dependent names). This catches many errors at definition time.

Error Messages

Template errors can be intimidating. Read from the bottom up, focus on the source location, and use C++20 concepts to improve diagnostics.

Quick Check

What feature lets std::vector v{1, 2, 3}; compile without specifying std::vector<int>?

Recap

Templates generate type-specific code at compile time. Define function and class templates with template <typename T>. The compiler deduces arguments when possible — explicitly specify when needed.

Frequently asked questions

Is the “Function and Class Templates Revisited” lesson free?

Yes — the full text of “Function and Class Templates Revisited” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Function and Class Templates Revisited”?

Write generic functions and classes with proper template syntax. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Function and Class Templates Revisited” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Function and Class Templates Revisited
  2. Template Specialization Partial and Full
  3. Variadic Templates and Parameter Packs
  4. constexpr Functions and if constexpr
← Back to C++ Academy