This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-10-26


2616. Imprecise restrictions on break and continue

Section: Clause 8  [stmt.stmt]     Status: C++23     Submitter: Jim X     Date: 2022-08-24

[Accepted as a DR at the November, 2022 meeting.]

Consider:

for (int i = 0; i< 10; ++i){
  auto f = [](){
    break; // #1
  };
}

Subclause 8.7.2 [stmt.break] paragraph 1 specifies:

The break statement shall occur only in an iteration-statement or a switch statement and causes termination of the smallest enclosing iteration-statement or switch statement; control passes to the statement following the terminated statement, if any.

Does the break at #1 "occur" in the for loop?

Proposed resolution (approved by CWG 2022-08-26):

  1. Append to 8.1 [stmt.pre] paragraph 3 as follows:

    ... A statement S1 is enclosed by a statement S2 if S2 encloses S1.

    The rules for conditions apply both...

  2. Change in 8.2 [stmt.label] paragraph 2 as follows:

    Case labels and default labels shall occur only in switch statements A labeled-statement whose label is a case or default label shall be enclosed by (8.1 [stmt.pre]) a switch statement (8.5.3 [stmt.switch])..
  3. Change in 8.7.2 [stmt.break] paragraph 1 as follows:

    The A break statement shall occur only in be enclosed by (8.1 [stmt.pre]) an iteration-statement (8.6 [stmt.iter]) or a switch statement and (8.5.3 [stmt.switch]). The break statement causes termination of the smallest such enclosing iteration-statement or switch statement; control passes to the statement following the terminated statement, if any.
  4. Change in 8.7.3 [stmt.cont] paragraph 1 as follows:

    The A continue statement shall occur only in be enclosed by (8.1 [stmt.pre]) an iteration-statement (8.6 [stmt.iter]) and. The continue statement causes control to pass to the loop-continuation portion of the smallest such enclosing iteration-statement statement, that is, to the end of the loop. ...