Search code examples
c++templatesnon-type

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


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.


Solution

  • "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.