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/30814461/

相关文章:

c++ - 使用 `std::vector` 时 `std::unique_ptr` 中的数据不同

c++ - struct 中不可访问的模板化构造函数

c++ - 快速计算两个数组之间的相等字节数

c++ - constexpr 的模板参数

c++ - 在另一个共享库中使用共享库

c++ - 正在写入 &str[0] 缓冲区(std :string) well-defined behaviour in C++11?

c++ - 使用 SecureZeroMemory() 真的有助于提高应用程序的安全性吗?

c++ - 依赖注入(inject)/继承设计模式的构造函数参数太多

Django block 反式计数复数

c++ - 如何在 C++ operator() 中专门化调用模板?