【问题标题】:Is east constexpr / constinit / consteval allowed in C++20?C++20 中是否允许使用east constexpr / constinit / consteval?
【发布时间】:2019-09-20 18:04:19
【问题描述】:

我在网上找到的大多数示例都更喜欢 constexpr (C++11)、constevalconstinit (C++20) 的“西方风格”:

consteval auto sqr(int n) {
  return n*n;
}
constexpr auto r = sqr(100);  // OK
constinit static auto x = r; 

由于我不是语言律师,我有以下问题: 这些说明符是否允许使用“东方风格”?示例:

auto consteval sqr(int n) {
  return n*n;
}
auto constexpr r = sqr(100);  // OK
static auto constinit x = r; 

明确一点:我无意发动“西方”/“东方”语言战争。我对意见不感兴趣,只是在事实,尤其是目前 wandbox 上的 clang 和 gcc head 版本给出错误但在constinit / consteval 上没有答案。

【问题讨论】:

  • 请注意,constinit 仍然可以伴随一个非冗余的const(而不是constexpr)。

标签: c++ c++20


【解决方案1】:

有点。

它是 decl-specifier-seq 的一部分,其中的说明符可以是任何顺序。这条规则允许你写volatile int static long unsigned inline long const x = 1;

但它不是 declarator 的一部分(特别是 ptr-operator),所以你不能这样做int* constexpr x = nullptr;

【讨论】:

  • 这种类型是怪物,你知道的。
猜你喜欢
  • 1970-01-01
  • 2020-01-10
  • 2019-12-05
  • 1970-01-01
  • 2021-10-26
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多