Template Parameters

Let's familiarize ourselves with template parameters in this lesson.

We'll cover the following

Template Parameter

Every template is parameterized by one or more template parameters, indicated in the parameter-list of the template.

C++ supports three different kinds of template parameters

  1. Type parameter
std::vector<int> vec = {1, 2, 3, 4, 5}; 
  1. Non-type parameter
std::array<int, 5> arr = {1, 2, 3, 4, 5};
  1. Template-template parameter
template <typename T, template <typename, typename> class Cont> class Matrix{
...
Matrix<int, std::vector> myIntVec;

Types

A type parameter is a typical case for template arguments.

  • Type parameters are class types and fundamental types

Non-Types

Non-types are template parameters which can be evaluated at compile-time.

The following types are possible

  • Integers and enumerations

  • Pointers to objects, functions, and attributes of a class

  • References to objects and functions

  • std::nullptr_t constant

With C++17, floating-point numbers and strings cannot be used as non-type parameters.

To learn more about template parameters, click here.

Get hands-on with 1200+ tech skills courses.