<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<style>
.ins {background-color:#A0FFA0;font-weight: bold}
.del {background-color:#FFA0A0;text-decoration:line-through}
</style>
<title>Wording for Range-Based For Loop (Option #5)</title>
<body>

<h1>Wording for Range-Based For Loop (Option #5)</h1>

Document number: N3271=11-0041<br>
Author: Doug Gregor<br>
Date: 2011-03-23<br>
Reply-To: doug.gregor@gmail.com<br>
Revises: N3257=11-0027

<h2>Proposed Wording</h2>

<p>This paper provides proposed wording for "option #5" in N3257 by Jonathan Wakely and Bjarne Stroustrup, which specifies that the range-based for loop should look for member functions <code>begin()</code> and <code>end()</code> first, and fall back to the current ADL-based behavior only when the type of the range does not contain either "begin" or "end".</p>

<h2>Proposed Wording</h2>

<p>Modify 6.5.4 [stmt.ranged]p1 by inserting the following new bullet after the first:</p>

<ul>
  <li>if <code>_RangeT</code> is an array type, <var>begin-expr</var> and <var>end-exp</var>r are <code>__range</code> and <code>__range + __bound</code>, respectively, where <code>__bound</code> is the array bound. If <code>_RangeT</code> is an array of unknown size or an array of incomplete type, the program is ill-formed<span class="del">.</span><span class="ins">;</span></li>

  <li><span class="ins">if <code>_RangeT</code> is a class type, the <var>unqualified-id</var>s <code>begin</code> and <code>end</code> are looked up in the scope of class <code>_RangeT</code> as if by class member access lookup (3.4.5), and if either (or both) finds at least one declaration, <var>begin-expr</var> and <var>end-expr</var> are <code>__range.begin()</code> and <code>__range.end()</code>, respectively;</span></li>
  
  <li>otherwise, <var>begin-expr</var> and <var>end-expr</var> are <code>begin(__range)</code> and <code>end(__range)</code>, respectively, where begin and end are looked up with argument-dependent lookup (3.4.2). For the purposes of this name lookup, namespace std is an associated namespace.</li>
</ul>
</body>
</html>
 