Template Parameter

Template Parameter

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

C++ supports three different kinds of template parameter:

1. Type parameter

std::vector<int> vec = {1, 2, 3, 4, 5}; 

2. Non-type parameter

std::array<int, 5> arr = {1, 2, 3, 4, 5};

3. Template-template Parameter

template <typename T, template <typename, typename> class Cont> 
class Matrix{ ...
Matrix<int, std::vector> myIntVec;

Types

Type parameters are of class types and fundamental types.

class Account;

template <typename T> 
class ClassTemplate{};

ClassTemplate<int> clTempInt; 
ClassTemplate<double> clTempDouble;
ClassTemplate<Account> clTempAccount;

ClassTemplate<std::string> clTempString;

Non-Types

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

The following types are possible:

  • Integers and enumerations
  • Pointer on objects, functions, and on the attributes of a class
  • References to objects and functions
  • std::nullptr_t constant

Float point numbers and strings cannot be used as non-type parameters.

Get hands-on with 1200+ tech skills courses.