https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94897
Bug ID: 94897 Summary: range-for produces a variable initialiser with use of a forward decl. Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: iains at gcc dot gnu.org Target Milestone: --- It seems that parser.c:cp_convert_range_for () is not implementing the lowering that its header comment outlines. for this: #include <array> void foo () { const std::array<int, 5> expectedValues = {{0, 3, 1, 4, 2}}; for (int expectedValue : expectedValues) { ; } } we are getting the statement tree below, the key being (V = Var decl, Va = artificial var decl) where the first entry initialiser contains a forward ref. ---- V int expectedValue = E (std::array<int, E <cst>5>::value_type)E *VD foo::__for_begin; Va const std::array<int, E <cst>5>& __for_range = E (const std::array<int, E <cst>5>&)E &foo::expectedValues; Va const std::array<int, E <cst>5>::value_type* __for_begin; Va const std::array<int, E <cst>5>::value_type* __for_end; ===== FD foo () - <STATEMENT_LIST>{ S BIND_EXPR [scope 0x10399a3c0] BLOCK #0 not outer [ao] 0x0 [super-ctx] 0x0 SUBBLOCKS 0x10399a360 VARS { V const std::array<int, E <cst>5>expectedValues = {E CE {E <cst>0, E <cst>3, E <cst>1, E <cst>4, E <cst>2}}; } BODY { - <STATEMENT_LIST>{ CPs<<sD const std::array<int, E <cst>5>expectedValues = {E CE {E <cst>0, E <cst>3, E <cst>1, E <cst>4, E <cst>2}}; CPE>> S BIND_EXPR [scope 0x10399a360] BLOCK #0 not outer [ao] 0x0 [super-ctx] 0x10399a3c0 SUBBLOCKS 0x0 VARS { V int expectedValue = E (std::array<int, E <cst>5>::value_type)E *VD foo::__for_begin; Va const std::array<int, E <cst>5>& __for_range = E (const std::array<int, E <cst>5>&)E &foo::expectedValues; Va const std::array<int, E <cst>5>::value_type* __for_begin; Va const std::array<int, E <cst>5>::value_type* __for_end; } BODY { - <STATEMENT_LIST>{ sD const std::array<int, E <cst>5>& __for_range = E (const std::array<int, E <cst>5>&)E &foo::expectedValues; sD const std::array<int, E <cst>5>::value_type* __for_begin; CPs<<S xE (void)E foo::__for_begin = foo::__for_range.std::array<int, E <cst>5>::begin (); CPE>> sD const std::array<int, E <cst>5>::value_type* __for_end; CPs<<S xE (void)E foo::__for_end = foo::__for_range.std::array<int, E <cst>5>::end (); CPE>> sS for (; E CE foo::__for_begin != foo::__for_end; CPe<<E (void)E CE ++foo::__for_begin CPE>>) sD int expectedValue = E (std::array<int, E <cst>5>::value_type)E *VD foo::__for_begin; } } } }