【发布时间】:2016-10-19 04:16:22
【问题描述】:
考虑以下 sn-p:
#include <iostream>
template <int I>
constexpr int f() { return I * f<I-1>(); }
template<>
constexpr int f<0>() { return 1; }
int main () {
std::cout << f<5>();
return 0;
}
这段代码可以很好地与 g++ 和 clang 一起编译。很不错。
现在将static 添加到模板函数特化中:
template<>
constexpr static int f<0>() { return 1; }
然后 g++ 6.1 会报错:
11 : 错误:显式模板特化不能有存储类
还有 clang 3.8:
11 : 错误:显式特化具有无关的、不一致的存储类“静态”
他们看起来很一致。再次非常好。
现在,添加static关键字也是模板函数一般情况:
g++ 6.1:
11 : 错误:显式模板特化不能有存储类
clang 3.8 编译时出现警告:
11 : 警告:显式特化不能有存储类
clang 结果返回正确答案。
这是 clang 的错误吗?如果不是,在什么情况下不抛出错误有意义?
【问题讨论】:
-
我说,这是一个错误。 g++ 是正确的。
-
这是什么错误?您收到了诊断信息,因此代码有问题。大多数时候你应该使用 -Werror 来停止编译。
-
@NathanOliver:标准是怎么说的?你通过标准了吗?
标签: c++ templates language-lawyer function-templates storage-class-specifier