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 ?
There is no difference. typename and class are interchangeable in the declaration of a type 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.)
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.
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.
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.
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.
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).
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 { /* ...
No.
§14.1 [temp.param] p5
[...] The top-level cv-qualifiers on the template-parameter are ignored when determining its type.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With