Title
Point of declaration in range-based for
Status
cd5
Section
8.6.5 [stmt.ranged]
Submitter
John Spicer

Created on 2012-07-16.00:00:00 last changed 47 months ago

Messages

Date: 2017-07-15.00:00:00

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

Date: 2017-05-15.00:00:00

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...

Date: 2013-04-15.00:00:00

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 ).

Date: 2012-10-15.00:00:00

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.

Date: 2022-02-18.07:47:23

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.)

History
Date User Action Args
2020-12-15 00:00:00adminsetstatus: drwp -> cd5
2018-02-27 00:00:00adminsetmessages: + msg6136
2018-02-27 00:00:00adminsetmessages: + msg6135
2018-02-27 00:00:00adminsetstatus: drafting -> drwp
2013-05-03 00:00:00adminsetmessages: + msg4333
2013-05-03 00:00:00adminsetstatus: open -> drafting
2012-11-03 00:00:00adminsetmessages: + msg4113
2012-07-16 00:00:00admincreate