Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between "T" and "const T" in template parameter?

Is there any difference between following 2 syntax:

template<int N> struct A;         // (1) 

and

template<const int N> struct A;   // (2) 

Any general guideline for when to use each syntax ?

like image 944
iammilind Avatar asked Mar 13 '12 06:03

iammilind


People also ask

What is the difference between template Typename T and template T?

There is no difference. typename and class are interchangeable in the declaration of a type template parameter.

What is correct for template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

What does const T& mean in C++?

const T& value() const {... } This means the function value() will return a const T& type and in between (in the function) won't modify the class itself.

What is template type parameter?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

What is a template parameter in C++?

An identifier that names a non-type template parameter of class type T denotes a static storage duration object of type const T, called a template parameter object, whose value is that of the corresponding template argument after it has been converted to the type of the template parameter.

What is the difference between function templates and template arguments?

Each function template has a single function parameter whose type is a specialization of X with template arguments corresponding to the template parameters from the respective function template where, for each template parameter PP in the template parameter list of the function template, a corresponding template argument AA is formed.

What is the difference between template and template parameter declaration?

Unlike type template parameter declaration, template template parameter declaration can only use the keyword class and not typename . In the body of the template declaration, the name of this parameter is a template-name (and needs arguments to be instantiated).

How to deduce the type of a non-type template parameter?

The deduction is performed as if by deducing the type of the variable x in the invented declaration T x = template-argument;, where T is the declared type of the template parameter. If the deduced type is not permitted for a non-type template parameter, the program is ill-formed. template<auto n > struct B { /* ...


2 Answers

No.

§14.1 [temp.param] p5

[...] The top-level cv-qualifiers on the template-parameter are ignored when determining its type.

like image 111
Xeo Avatar answered Oct 06 '22 03:10

Xeo


I found this doing a quick search of the standard:

template<const short cs> class B { }; template<short s> void g(B<s>); void k2() {     B<1> b;     g(b); // OK: cv-qualifiers are ignored on template parameter types } 

The comment says they are ignored.

I'll recommend not using const in template parameters as it's unnecessary. Note that it's not 'implied' either - they're constant expressions which is different from const.

like image 42
Pubby Avatar answered Oct 06 '22 05:10

Pubby