Non-type Template Parameters

Get introduced to non-type template parameters.

C++ supports non-types as template parameters. Essentially non-types could be:

  • integers and enumerators
  • pointers or references to objects, to functions, and to attributes of a class
  • std::nullptr_t

πŸ”‘ Typical non-type template parameter

When I ask the students in my class if they ever used a non-type as template parameter they say: No! Of course, I answer my tricky question and show an often-used example for non-type template parameters:

std::array<int, 5> myVec;

Constant 55 is a non-type used as a template argument.

Since the first C++standard, C++98, there has been an ongoing discussion in the C++ community about supporting floating-point template parameters. Now, we have them and more. C++20 supports floating-points, literal types, and strings literals as non-types.

Get hands-on with 1200+ tech skills courses.