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

2024-12-19


1523. Point of declaration in range-based for

Section: 8.6.5  [stmt.ranged]     Status: CD5     Submitter: John Spicer     Date: 2012-07-16

[Voted into the WP at the July, 2017 meeting.]

According to the general rule for declarations in 6.4.2 [basic.scope.pdecl] paragraph 1,

The point of declaration for a name is immediately after its complete declarator (9.3 [dcl.decl]) and before its initializer (if any), except as noted below.

However, the rewritten expansion of the range-based for statement in 8.6.5 [stmt.ranged] paragraph 1 contradicts this general rule, so that the index variable is not visible in the range-init:

  for (int i : {i}) ;   // error: i not in scope

(See also issue 1498 for another question regarding the rewritten form of the range-based for.)

Notes from the October, 2012 meeting:

EWG is discussing issue 900 and the outcome of that discussion should be taken into consideration in addressing this issue.

Notes from the April, 2013 meeting:

The approach favored by CWG for resolving this issue is to change the point of declaration of the variable in the for-range-declaration to be after the ).

Proposed resolution (May, 2017):

Add the following as a new pareagraph following 6.4.2 [basic.scope.pdecl] paragraph 9:

The point of declaration for a function-local predefined variable (9.5 [dcl.fct.def]) is immediately before the function-body of a function definition.

The point of declaration for the variable or the structured bindings declared in the for-range-declaration of a range-based for statement (8.6.5 [stmt.ranged]) is immediately after the for-range-initializer.

The point of declaration for a template parameter...