我在网上找到的大多数例子都更喜欢“西方风格”constexpr
(C++11),consteval
, and constinit
(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
.
Sort of.
它是的一部分声明说明符序列,其中的说明符可以按任意顺序排列。这与允许您编写的规则相同volatile int static long unsigned inline long const x = 1;
但它不属于声明者(特别是,ptr 运算符),所以你不能这样做int* constexpr x = nullptr;
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)