[英]constexpr static template function: g++ error is a warning on clang
Consider the following snippet: 请考虑以下代码段:
#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;
}
This code compiles nicely with both g++ and clang. 这段代码可以很好地编译g ++和clang。 Very nice. 非常好。 Now add static
to the template function specialization: 现在将static
添加到模板函数专门化:
template<>
constexpr static int f<0>() { return 1; }
then g++ 6.1 reacts with an error: 然后g ++ 6.1对错误做出反应:
11 : error: explicit template specialization cannot have a storage class 11:错误:显式模板专业化不能有存储类
and clang 3.8 too: 和clang 3.8:
11 : error: explicit specialization has extraneous, inconsistent storage class 'static' 11:错误:显式特化具有无关的,不一致的存储类'静态'
They look like in agreement. 他们看起来像是一致的。 Very nice again. 再好不过。 Now, add static
keyword also the template function general case: 现在,添加static
关键字也是模板函数的一般情况:
g++ 6.1: g ++ 6.1:
11 : error: explicit template specialization cannot have a storage class 11:错误:显式模板专业化不能有存储类
clang 3.8 compiles with a warning: clang 3.8编译并发出警告:
11 : warning: explicit specialization cannot have a storage class 11:警告:显式专业化不能有存储类
and clang result returns the correct answer. 和clang结果返回正确的答案。
Is this a bug in clang? 这是clang中的错误吗? If not, in which case does it make sense not to throw an error? 如果没有,在哪种情况下不抛出错误是有道理的?
It's as simple as [dcl.stc]/1 (which goes as far back as C++98): 它就像[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. 不应在显式特化(14.7.3)或显式实例化(14.7.2)指令中指定thread_local
以外的存储类说明符 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.