CKTIS / C++
Non-type template parameters
A non-type template parameter takes a value rather than a type. See how `std::size_t Size` fixes an array’s extent and how the compiler deduces it from function arguments.
Version: ISO C++23; implementation support may vary
Syntax
template<class T, std::size_t Size> struct Buffer;
template<std::size_t Size>
using AoS = std::array<Row, Size>;
template<std::size_t Size>
void copy_rows(const AoS<Size>& source, AoS<Size>& destination);
template<auto Value> struct Tag;In template<class T, std::size_t Size>, T takes a type, while Size takes a value. Parameters like Size are called non-type template parameters (NTTPs). Size is the parameter name; 2 in AoS<2> is the template argument. The extent is part of the array type, so AoS<2> and AoS<3> are different types.
An expression supplied for Size must be a converted constant expression of type std::size_t, available from <cstddef>. You can use the literal 2, or write constexpr std::size_t n = 2; and use AoS<n>. A size read from input at run time does not meet this requirement; using it in AoS<n> is a compile-time error.
The declaration using AoS = std::array<Row, Size>;, preceded by the template header, defines an alias template, not a new type. In the function parameters, & declares an lvalue reference; it does not take an address. const AoS<Size>& source prevents modification through that reference, while AoS<Size>& destination lets the function modify the caller’s destination array. Both references bind to existing arrays; destination = source copies elements rather than rebinding a reference.
Since C++17, template<auto Value> lets the compiler deduce the parameter’s type from the argument, which still has to satisfy the NTTP rules. Since C++20, permitted types include floating-point types and certain literal class types that meet the structural-type requirements. Pointer and reference arguments have separate restrictions: for example, they cannot point or refer to a temporary object or a string literal. This entry covers ISO C++23; implementation support may vary.
Examples
#include <array>
#include <cstddef>
#include <iostream>
struct Row {
int value;
};
template<std::size_t Size>
using AoS = std::array<Row, Size>;
template<std::size_t Size>
void copy_rows(const AoS<Size>& source, AoS<Size>& destination)
{
destination = source;
}
int main()
{
const AoS<2> source{Row{7}, Row{11}};
AoS<2> destination{};
copy_rows(source, destination);
std::cout << destination[0].value << ' ' << destination[1].value << '\n';
}Result
7 11
`copy_rows(source, destination)` deduces `Size = 2` from the types of both arrays. `copy_rows<2>(source, destination)` supplies the same value explicitly. If the arrays have different extents, the call without an explicit argument fails to compile because deduction produces conflicting values for `Size`. Expected output: `7 11`.
Related lessons
- C++ · Lesson 21 · Advanced · Version: 3
Sources
- Working Draft, Programming Languages — C++ (N4950; [temp.param] and [temp.arg.nontype])
- Declaring non-type template arguments with auto (P0127R2)
- Constant template arguments [temp.arg.nontype]
- Deducing template arguments from a type [temp.deduct.type]
- Alias templates [temp.alias]
- References [dcl.ref]
- Class template array — Overview [array.overview]