类范围内的constexpr变量的初始值设定项是否允许引用该变量?
以下代码:
struct S {
static constexpr int rolling_sum[4]{
0,
rolling_sum[0] + 1,
rolling_sum[1] + 2,
rolling_sum[2] + 3
};
};
被 clang 接受(用版本 12 测试),但被 gcc(用版本 11 测试)拒绝,并出现以下错误:
test.cpp:4:9: error: ‘rolling_sum’ was not declared in this scope
4 | rolling_sum[0] + 1,
| ^~~~~~~~~~~
test.cpp:5:9: error: ‘rolling_sum’ was not declared in this scope
5 | rolling_sum[1] + 2,
| ^~~~~~~~~~~
test.cpp:6:9: error: ‘rolling_sum’ was not declared in this scope
6 | rolling_sum[2] + 3
| ^~~~~~~~~~~
这段代码是有效的 C++ 吗?
我的猜测是它应该是有效的,因为[basic.scope.pdecl] p1声明变量的声明点就在其初始化程序之前,这意味着该变量应该在其初始化程序的范围内;但我不确定我是否忽略了其他可能与此相关的内容。