Why is it called "non-type" template parameter?

1.2k Views Asked by At

In the C++ template terminology we have non-type template parameters, type template parameters, and template template parameters (and then the same list with arguments).

Why is it called non-type? Isn't it a value? Shouldn't it be "value template parameter"?

Do I miss anything if I think of them as value template parameters?

Note: out of curiosity, I checked the documentation of the D language, and they call it value.

3

There are 3 best solutions below

2
On BEST ANSWER

"value" has a very specific non-intuitive definition in C++ that does not necessarily apply to non-type template arguments:

3.9 Types [basic.types]

4 The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T). The value representation of an object is the set of bits that hold the value of type T. For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values.

Even though the C++ standard does occasionally use the word "value" informally, it's good that they haven't done so here. A non-type template argument type does not need to be trivially copyable. Specifically, as Mike Seymour commented on the question, it could be a reference type.

That said, I do feel I should note that I don't think the term "non-type template parameter" is a correct one. It used to be, but we now have template template parameters, which aren't types, but also aren't non-type template parameters.

3
On

I am not part of the committee, but I would say the reason is that while you could easily describe arguments for non-type template parameters of type int or even const char * as a value, it gets less clear for a non-type template parameter of reference type, e.g. std::string &. The argument for that parameter is an object, not its value.

2
On

It will be more clear if to consider standard class std::array It is declared the following way

template <class T, size_t N >
struct array;

As it is seen it has two template parameters: type parameter T and non-type parameter N. Why is N a non-type parameter? Because its type is already defined and is equal to size_t. For this non-type parameter N you have to supply some constant value. For example

std::array<int, 10> aq1;
std::array<double, 20> a2;

T is a type parameter because we have to supply a type as a template argument that to instantiate an object of this class. We may not supply a value as for example

std::array<10, 10> a3;

though 10 is integer literal of type int. But we may write

std::array<decltype( 10 ), 10> a4;

Compare the first template argument setting and the second template argument setting.