Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11: template parameter redefines default argument

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?

like image 546
Andreas Florath Avatar asked Jan 07 '13 13:01

Andreas Florath


People also ask

How do you define default template arguments?

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.

What is a default template parameter in C++?

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.

What are template parameters in C++11?

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

Can you put default parameters in function arguments in C++?

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) { // ...


1 Answers

§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 ]

like image 92
Bartek Banachewicz Avatar answered Sep 16 '22 20:09

Bartek Banachewicz