【问题标题】:constexpr static template function: g++ error is a warning on clangconstexpr 静态模板函数:g++ 错误是关于 clang 的警告
【发布时间】:2016-10-19 04:16:22
【问题描述】:

考虑以下 sn-p:

#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 : 错误:显式模板特化不能有存储类

还有 clang 3.8:

11 : 错误:显式特化具有无关的、不一致的存储类“静态”

他们看起来很一致。再次非常好。 现在,添加static关键字也是模板函数一般情况:

g++ 6.1:

11 : 错误:显式模板特化不能有存储类

clang 3.8 编译时出现警告:

11 : 警告:显式特化不能有存储类

clang 结果返回正确答案。

这是 clang 的错误吗?如果不是,在什么情况下不抛出错误有意义?

【问题讨论】:

  • 我说,这是一个错误。 g++ 是正确的。
  • 这是什么错误?您收到了诊断信息,因此代码有问题。大多数时候你应该使用 -Werror 来停止编译。
  • @NathanOliver:标准是怎么说的?你通过标准了吗?

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


【解决方案1】:

就像[dcl.stc]/1 一样简单(可以追溯到 C++98):

thread_local 以外的 storage-class-specifier 不得在显式特化 (14.7.3) 或显式实例化 (14.7.2) 指令中指定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-27
    • 2020-06-17
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2016-07-29
    • 2015-12-25
    • 2022-07-18
    相关资源
    最近更新 更多