The following code:
struct S {
static constexpr int rolling_sum[4]{
0,
rolling_sum[0] + 1,
rolling_sum[1] + 2,
rolling_sum[2] + 3
};
};
is accepted by clang (tested with version 12), but rejected by gcc (tested with version 11) with the following errors:
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
| ^~~~~~~~~~~
Is this code valid C++?
My guess is that it should be valid because [basic.scope.pdecl] p1 states that the point of declaration of a variable is just before its initializer, meaning the variable should be in scope in its initializer; but I'm not sure if I'm overlooking something else that could be relevant here.