constexpr 静态模板函数:g++ 错误是对 clang 的警告
constexpr static template function: g++ error is a warning on clang
考虑以下片段:
#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 结果 returns 正确答案。
这是 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.
考虑以下片段:
#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 结果 returns 正确答案。
这是 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.