C++11:模板参数重新定义默认参数

标签 c++ templates c++11 default-value

使用 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/

相关文章:

django 设置变量在传递给模板时丢失

c++ - 删除从析构函数调用的抛出析构函数

c++ - Endianness 和 C API 的 : Specifically OpenSSL

c++ - 用 vector 元素调用函数的通用模板

c++ - 从函数返回迭代器

C++ - 使用该方法的部分特化重载模板类方法

c++ - 缓存单个重写计算 C++11

c++ - 如何放置线程数组的动态大小

c++ - 容器和派生类

c++ - 一个处理 char* 和 wchar_t* 的函数