以下代码:
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 规定变量的声明点就在其初始化之前,意味着变量应该在其初始化器中作用域内。但我不确定是否有其他相关问题被忽略了。