<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 1523</TITLE>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<STYLE TYPE="text/css">
  INS { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  .INS { text-decoration:none; background-color:#D0FFD0 }
  DEL { text-decoration:line-through; background-color:#FFA0A0 }
  .DEL { text-decoration:line-through; background-color: #FFD0D0 }
  @media (prefers-color-scheme: dark) {
    HTML { background-color:#202020; color:#f0f0f0; }
    A { color:#5bc0ff; }
    A:visited { color:#c6a8ff; }
    A:hover, a:focus { color:#afd7ff; }
    INS { background-color:#033a16; color:#aff5b4; }
    .INS { background-color: #033a16; }
    DEL { background-color:#67060c; color:#ffdcd7; }
    .DEL { background-color:#67060c; }
  }
  SPAN.cmnt { font-family:Times; font-style:italic }
</STYLE>
</HEAD>
<BODY>
<P><EM>This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21
  Core Issues List revision 118b.
  See http://www.open-std.org/jtc1/sc22/wg21/ for the official
  list.</EM></P>
<P>2025-09-28</P>
<HR>
<A NAME="1523"></A><H4>1523.
  
Point of declaration in range-based <TT>for</TT>
</H4>
<B>Section: </B>8.6.5&#160; [<A href="https://wg21.link/stmt.ranged">stmt.ranged</A>]
 &#160;&#160;&#160;

 <B>Status: </B>CD5
 &#160;&#160;&#160;

 <B>Submitter: </B>John Spicer
 &#160;&#160;&#160;

 <B>Date: </B>2012-07-16<BR>


<P>[Voted into the WP at the July, 2017 meeting.]</P>

<P>According to the general rule for declarations in 6.4.2 [<A href="https://wg21.link/basic.scope.pdecl#1">basic.scope.pdecl</A>] paragraph 1,
</P>

<BLOCKQUOTE>

The <I>point of declaration</I> for a name is immediately after its
complete declarator (9.3 [<A href="https://wg21.link/dcl.decl">dcl.decl</A>]) and before its
<I>initializer</I> (if any), except as noted below.

</BLOCKQUOTE>

<P>However, the rewritten expansion of the range-based <TT>for</TT>
statement in 8.6.5 [<A href="https://wg21.link/stmt.ranged#1">stmt.ranged</A>] paragraph 1 contradicts
this general rule, so that the index variable is not visible in the
<I>range-init</I>:</P>

<PRE>
  for (int i : {i}) ;   //<SPAN CLASS="cmnt"> error: </SPAN>i<SPAN CLASS="cmnt"> not in scope</SPAN>
</PRE>

<P>(See also <A HREF="1498.html">issue 1498</A> for another
question regarding the rewritten form of the range-based <TT>for</TT>.)</P>

<P><B>Notes from the October, 2012 meeting:</B></P>

<P>EWG is discussing <A HREF="900.html">issue 900</A> and the
outcome of that discussion should be taken into consideration in
addressing this issue.</P>

<P><B>Notes from the April, 2013 meeting:</B></P>

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

<P><B>Proposed resolution (May, 2017):</B></P>

<P>Add the following as a new pareagraph following
6.4.2 [<A href="https://wg21.link/basic.scope.pdecl#9">basic.scope.pdecl</A>] paragraph 9:</P>

<BLOCKQUOTE>

<P>The point of declaration for a function-local predefined variable
(9.6 [<A href="https://wg21.link/dcl.fct.def">dcl.fct.def</A>]) is immediately before
the <I>function-body</I> of a function definition.</P>

<P><INS>The point of declaration for the variable or the structured
bindings declared in the <I>for-range-declaration</I> of a
range-based <TT>for</TT> statement (8.6.5 [<A href="https://wg21.link/stmt.ranged">stmt.ranged</A>]) is
immediately after the <I>for-range-initializer</I>.</INS></P>

<P>The point of declaration for a template parameter...</P>

</BLOCKQUOTE>

<BR><BR>
</BODY>
</HTML>
