Skip to main content
Module 8 · Lesson 2 of 218/26 in the course~16 min
Module lessons (2/2)

Template Classes

Lesson preview5 hands-on exercises readyCourse C++ · Module 8 — Templates

Implement containers and generic classes parameterized with respect to the content type and CTAD.

What you'll learn in this lesson

  • Design parametric classes and data structures on the T (template <typename T> class Box) type.
  • Leverage non-type template parameters (std::size_t N) and C++17 automatic inference (CTAD).
  • Customize behavior for individual types via full specialization (template <> class Wrapper<char>).

Overview

The entire C++ Standard Library (std::vector<T>, std::unique_ptr<T>, std::optional<T>) is based on template classes: classes parameterized on one or more types and even on constant values known at compile time.

Prepending template <typename T> makes T available in fields, constructors, and methods throughout the class:

Sections covered in this lesson

  1. 11. Definition of a Template Class
  2. 22. CTAD (Class Template Argument Deduction, C++17+)
  3. 33. Non-Type Parameters (template <typename T, int Capacity>)
  4. 44. Total Specialization (template <>)
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 8 — Templates

Unlock "Template Classes" and the full Course C++ curriculum

Implement containers and generic classes parameterized with respect to the content type and CTAD.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course C++ plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.