Why is it called "non-type" template parameter?
Asked Answered
S

3

13

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.

Styracaceous answered 11/11, 2014 at 9:43 Comment(3)
They could be references. I wouldn't regard those as values, but you'd need a more pedantic language-lawyer than me to determine whether or not one should.Labourite
I'd have to dig up my copy of the ARM, bur IIRC type template parameters were introduced first, non-type parameters second, then template template arguments, and finally now variadic templates. If we'd designed this all from the start, we might have named it better.Punak
I'm not a C++ programmer at all, I used to love it back in college but those were the very old days. My only comment here is to show some respect to the C++ community for having to deal with a standard that is ever so growing and just talking about the language seems quite complex to me. I don't know how you guys do it...but you do..so +1.Roadwork
G
12

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

Gstring answered 11/11, 2014 at 9:54 Comment(2)
It seems to be talking about value representation which I cannot really figure out: is it the bit pattern within an object instance representation which discriminates it from any other object instance (from the same class or another)?Breslau
@Breslau To be honest, I don't know if I'm reading it entirely correctly, but it seems to me that the value representation is effectively those bits in the object representation that contribute to the value (i.e. those bits that aren't padding).Gstring
C
4

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.

Carillonneur answered 11/11, 2014 at 9:48 Comment(3)
What's the value of a reference? A would say it's the object. Is it wrong to think in those terms? (and is this turtles all the way down?:)Styracaceous
@KarolyHorvath "What's the value of a reference? A would say it's the object. Is it wrong to think in those terms?" if that works for you, I don't see any harm in it, but would documenting your terminological perspective in the C++ Standard, and explaining and defending it to the C++ community, be more or less confusing and time-consuming for others than saying "Non-Type"?Brantbrantford
I will continue to use non-type ;)Styracaceous
P
0

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.

Pascoe answered 11/11, 2014 at 10:5 Comment(2)
I know what a non-type template parameter is. I don't see how this answers my question.Styracaceous
@Karoly Horvath It looks at least strange. If you know then why are you asking?! My answer clear demonstartes the difference between a type parameter and a non-type parameter.Pascoe

© 2022 - 2024 — McMap. All rights reserved.