When compiling the following source code with gcc there are no errors / warnings:
template< typename T = int > T func( );
template< typename T = int > T func( );
When I compile the same source code with clang++, I got the following error:
redeftempparam.cc:2:24: error: template parameter redefines default argument
template< typename T = int > T func( );
^
redeftempparam.cc:1:24: note: previous default template argument defined here
template< typename T = int > T func( );
^
1 error generated.
Command to compile
[clang++|g++] -Wall -Werror -std=c++11 redeftempparam.cc
(Version information: gcc 4.7.2, clang version 3.3 (trunk 171722))
My question:
Is this type of redefinition allowed? If not: can you please point me to the appropriate point in the C++ standard?
Default template arguments Default template arguments are specified in the parameter lists after the = sign. Defaults can be specified for any kind of template parameter (type, non-type, or template), but not to parameter packs.
If the default is specified for a template parameter of a primary class template , primary variable template, (since C++14)or alias template, each subsequent template parameter must have a default argument, except the very last one may be a template parameter pack.
(C++11) Every template is parameterized by one or more template parameters, indicated in the parameter-list of the template declaration syntax: Each parameter in parameter-list may be: a template template parameter. 1) A non-type template parameter with an optional name. 2) A non-type template parameter with an optional name and a default value.
As we saw in Default parameters in C++: the facts (including the secret ones), defaults parameters in function arguments cannot do that. The following code, for instance, is illegal: void f (int x, int y = x) { // ...
§14.1.12:
A template-parameter shall not be given default arguments by two different declarations in the same scope.
[Example:
template<class T = int> class X; template<class T = int> class X { /∗... ∗/ }; // error
— end example ]
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