c++ - constexpr 静态模板函数 : g++ error is a warning on clang

标签 c++ templates language-lawyer function-templates storage-class-specifier

考虑以下片段:

#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/

相关文章:

c++读取(.cso)编译的着色器对象返回\0

c++ - 使用 SFINAE 有条件地解析分配器成员

c++ - 模板和单独编译

xcode - Xcode 8模板存储在哪里?

c++ - std::thread::id 的要求。可以雾化吗?

c - 使用 char * 访问 int 是否可能具有未定义的行为?

c++ - 在不扩展类的情况下为 QDialog 中的按键事件添加回调

c++ - 如何列出 .so 文件中的符号

c++ - 特定于类的释放函数可以在常量表达式中使用吗?

C++ 无法使用父类(super class)的方法?