C++ Academy · Lesson

Function and Class Templates Revisited

Write generic functions and classes with proper template syntax.

Lesson 1 of 414 steps

Function and Class Templates Revisited is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

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

Yes — any 3 lessons of the C++ Academy learning path, including “Function and Class Templates Revisited”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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