CWG Issue 2825 (original) (raw)
This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118f. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.
2025-11-07
2825. Range-based for statement using a braced-init-list
Section: 8.6.5 [stmt.ranged]Status: CD7Submitter: Arthur O'DwyerDate: 2023-11-08Liaison: EWG
[Accepted as a DR at the March, 2024 meeting.]
Consider:
for (int i : { 1, 2, 3 }) // argument-dependent lookup for begin(std::initializer_list) /* ... */;
This is undesirable; instead, a member function begin should be preferred.
Proposed resolution (approved by CWG 2023-11-09):
Change in 8.6.5 [stmt.ranged] bullet 1.3 as follows:
begin-expr and end-expr are determined as follows:
- if the
for-range-initializer is an expression oftype of range is a reference to an array type R, begin-expr and end-expr are range and range + N, respectively, where N is the array bound. If R is an array of unknown bound or an array of incomplete type, the program is ill-formed;- if the
for-range-initializer is an expression oftype of range is a reference to a class type C, and searches in the scope of C (6.5.2 [class.member.lookup]) for the names begin and end each find at least one declaration, begin-expr and end-expr are range .begin() and range .end(), respectively;- otherwise, begin-expr and end-expr are begin(range ) and end(range ), respectively, where begin and end undergo argument-dependent lookup (6.5.4 [basic.lookup.argdep]). [_Note 1:_ Ordinary unqualified lookup (6.5.3 [basic.lookup.unqual]) is not performed. —_end note_]
Additional notes (November, 2023)
Forwarded to EWG viapaper issue 1694for confirmation of the design direction.
EWG 2023-11-09
Approved by EWG.