使用 gcc 编译以下源代码时没有错误/警告:
template< typename T = int > T func( );
template< typename T = int > T func( );
当我用clang++编译同样的源代码时,出现如下错误:
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.
编译命令
[clang++|g++] -Wall -Werror -std=c++11 redeftempparam.cc
(版本信息:gcc 4.7.2,clang version 3.3(trunk 171722))
我的问题:
允许这种类型的重定义吗?如果不是:你能指出 C++ 标准中的适当点吗?
最佳答案
§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 ]
关于C++11:模板参数重新定义默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14197436/