【发布时间】:2021-10-19 01:30:41
【问题描述】:
以下代码:
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 声明变量的声明点就在其初始化程序之前,这意味着变量应该在其初始化程序的范围内;但我不确定我是否忽略了其他可能与此处相关的内容。
【问题讨论】:
-
它似乎在类似的测试中工作:godbolt.org/z/sYY84zehh(但在实际示例中仍然没有,不知道为什么)。似乎问题可能与
struct中定义的数组有关。
标签: c++ c++14 language-lawyer constexpr