Title
Range-based for statement using a braced-init-list
Status
drwp
Section
8.6.5 [stmt.ranged]
Submitter
Arthur O'Dwyer

Created on 2023-11-08.00:00:00 last changed 5 months ago

Messages

Date: 2023-11-15.00:00:00

Additional notes (November, 2023)

Forwarded to EWG via paper issue 1694 for confirmation of the design direction.

EWG 2023-11-09

Approved by EWG.

Date: 2023-12-02.13:36:01

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 of type 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 of type 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]
Date: 2024-03-15.00:00:00

[Accepted as a DR at the March, 2024 meeting.]

Consider:

  for (int i : { 1, 2, 3 })  // argument-dependent lookup for begin(std::initializer_list<int>)
    /* ... */;

This is undesirable; instead, a member function begin should be preferred.

History
Date User Action Args
2024-07-20 13:52:34adminsetstatus: dr -> drwp
2024-04-05 21:43:46adminsetstatus: ready -> dr
2024-03-20 14:10:31adminsetstatus: tentatively ready -> ready
2023-12-02 13:36:01adminsetstatus: open -> tentatively ready
2023-11-09 08:18:19adminsetmessages: + msg7504
2023-11-09 08:13:26adminsetmessages: + msg7503
2023-11-08 00:00:00admincreate