简体   繁体   English

constexpr静态模板函数:g ++错误是对clang的警告

[英]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.

相关问题 类定义静态constexpr结构的未定义引用,g ++ vs clang - undefined reference to class static constexpr struct, g++ vs clang Constexpr 函数返回联合成员:g++ 与 clang++:无诊断与错误 - Constexpr function returning member of union: g++ vs. clang++: no diagnostics vs. error 使用 std::acos 和 clang++ 而不是 g++ 的 Constexpr 编译错误 - Constexpr compile error using std::acos with clang++ not g++ 关于静态模板化constexpr的clang警告(未定义内联函数) - Clang warning about static templated constexpr (inline function is not defined) 哪个是更专业的模板功能? clang和g ++有所不同 - Which is the more specialized template function? clang and g++ differ on that 候选模板被忽略:替换失败(clang但不是g ++错误) - candidate template ignored: substitution failure(error with clang but not g++) 使用lambdas的变量模板:使用g ++但使用clang ++运行时出错 - Variadic template using lambdas : error with g++ but running with clang++ static_assert无法将const char *模板参数识别为constexpr:g ++错误? - static_assert doesn't recognize a const char* template parameter as constexpr: g++ bug? G ++ vs Clang:constexpr和const的行为不一致 - G++ vs Clang : inconsistent behavior for constexpr and const constexpr表达式和变量生命周期,g ++和clang不同意的例子 - constexpr expression and variable lifetime, an example where g++ and clang disagree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM