考虑以下片段:
#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 : error: explicit template specialization cannot have a storage class
还有 clang 3.8:
11 : error: explicit specialization has extraneous, inconsistent storage class 'static'
他们看起来很一致。再次非常好。
现在,添加 static
关键字也是模板函数的一般情况:
g++ 6.1:
11 : error: explicit template specialization cannot have a storage class
clang 3.8 编译时出现警告:
11 : warning: explicit specialization cannot have a storage class
clang 结果返回正确答案。
这是 clang 中的错误吗?如果不是,在什么情况下不抛出错误有意义?
最佳答案
就像[dcl.stc]/1一样简单(可以追溯到 C++98):
A storage-class-specifier other than
thread_local
shall not be specified in an explicit specialization (14.7.3) or an explicit instantiation (14.7.2) directive.
关于c++ - constexpr 静态模板函数 : g++ error is a warning on clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879642/