<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.3.9: http://docutils.sourceforge.net/" />
<title>Proposal for new for-loop (revision 1)</title>
<meta name="author" content="Thorsten Ottosen" />
<meta name="date" content="2005-08-24" />
<style type="text/css">

/*
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:date: $Date: 2004/01/12 02:49:54 $
:version: $Revision: 1.2 $
:copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
*/

.first {
  margin-top: 0 }

.last {
  margin-bottom: 0 }

a.toc-backref {
  text-decoration: none ;
  color: black }

dd {
  margin-bottom: 0.5em }

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.attention, div.caution, div.danger, div.error, div.hint,
div.important, div.note, div.tip, div.warning, div.admonition {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

div.hint p.admonition-title, div.important p.admonition-title,
div.note p.admonition-title, div.tip p.admonition-title,
div.admonition p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em }

div.footer, div.header {
  font-size: smaller }

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 0em 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1.title {
  text-align: center }

h2.subtitle {
  text-align: center }

hr {
  width: 75% }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.line-block {
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee }

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.option-argument {
  font-style: italic }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

table {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.citation {
  border-left: solid thin gray ;
  padding-left: 0.5ex }

table.docinfo {
  margin: 2em 4em }

table.footnote {
  border-left: solid thin black ;
  padding-left: 0.5ex }

td, th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

th.docinfo-name, th.field-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap }

h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
  font-size: 100% }

tt {
  background-color: #eeeeee }

ul.auto-toc {
  list-style-type: none }
  
pre{
  BORDER-RIGHT: gray 1pt solid;
  PADDING-RIGHT: 2pt;
  BORDER-TOP: gray 1pt solid;
  DISPLAY: block;
  PADDING-LEFT: 2pt;
  PADDING-BOTTOM: 2pt;
  BORDER-LEFT: gray 1pt solid;
  MARGIN-RIGHT: 32pt;
  PADDING-TOP: 2pt;
  BORDER-BOTTOM: gray 1pt solid;
  FONT-FAMILY: "Courier New", Courier, mono; 
  background-color: #EEEEEE; 
}


.keyword{color: #0000FF;}
.identifier{}
.comment{font-style: italic; color: #008000;}
.special{color: #800040;}
.preprocessor{color: #3F007F;}
.string{font-style: italic; color: #666666;}
.literal{/*font-style: italic;*/ color: #000000;}
.strike { text-decoration: line-through }
.u { text-decoration: underline }

</style>
</head>
<body>
<div class="document" id="proposal-for-new-for-loop-revision-1">
<h1 class="title">Proposal for new for-loop (revision 1)</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Thorsten Ottosen</td></tr>
<tr><th class="docinfo-name">Contact:</th>
<td><a class="first last reference" href="mailto:nesotto&#64;cs.aau.dk">nesotto&#64;cs.aau.dk</a></td></tr>
<tr class="field"><th class="docinfo-name">organizations:</th><td class="field-body">Dezide Aps and Aalborg University</td>
</tr>
<tr><th class="docinfo-name">Date:</th>
<td>2005-08-24</td></tr>
<tr class="field"><th class="docinfo-name">Number:</th><td class="field-body">WG21/N1868 and J16/05-0128</td>
</tr>
<tr class="field"><th class="docinfo-name">Working Group:</th><td class="field-body">Evolution</td>
</tr>
</tbody>
</table>
<div class="abstract topic">
<p class="topic-title first">Abstract</p>
<p>Just about any modern language has some form of &quot;for each&quot; built into it.
This paper introduces a new &quot;for each&quot; like for-loop and describes the necessary
core-language changes.</p>
</div>
<div class="contents topic" id="table-of-contents">
<p class="topic-title first"><a name="table-of-contents">Table of Contents</a></p>
<ul class="auto-toc simple">
<li><a class="reference" href="#motivation" id="id1" name="id1">1&nbsp;&nbsp;&nbsp;Motivation</a></li>
<li><a class="reference" href="#examples" id="id2" name="id2">2&nbsp;&nbsp;&nbsp;Examples</a></li>
<li><a class="reference" href="#the-proposal" id="id3" name="id3">3&nbsp;&nbsp;&nbsp;The proposal</a></li>
<li><a class="reference" href="#making-a-custom-type-work-with-the-for-loop" id="id4" name="id4">4&nbsp;&nbsp;&nbsp;Making a custom type work with the for loop</a></li>
<li><a class="reference" href="#alternative-design" id="id5" name="id5">5&nbsp;&nbsp;&nbsp;Alternative design</a></li>
<li><a class="reference" href="#acknowledgements" id="id6" name="id6">6&nbsp;&nbsp;&nbsp;Acknowledgements</a></li>
</ul>
</div>
<div class="section" id="motivation">
<h1><a class="toc-backref" href="#id1" name="motivation">1&nbsp;&nbsp;&nbsp;Motivation</a></h1>
<p>Being able to iterate over a range of values is common operation
that is unnecessarily difficult and verbose in current C++. This makes the
language harder to use for novice and experienced programmers alike.</p>
<p>The benefit of a new for loop is two-fold:</p>
<ul>
<li><p class="first"><em>Accessibility</em>. Just about any modern language has added a &quot;for each&quot; construct and C++ should do the same to make the language more accessible.</p>
</li>
<li><p class="first"><em>Performance</em>. A second benefit is that a more consistent performance of the loop is guaranteed; for example, it is quite common to write</p>
<pre class="literal-block">
for( container::iterator i = c.begin(); i != c.end(); ++i )
   ...
</pre>
<p>which leads to repeated calls to <tt class="docutils literal"><span class="pre">c.end()</span></tt>. Moreover, the new for loop might be easier parallelized.</p>
</li>
</ul>
<p>This proposal is based on the discussion following Tom Plum's
presentation at the 2004 meeting
in Sydney.</p>
<p>There is a continued interest in a better for loop for C++
(no pun intended).
For example, Eric Niebler's <tt class="docutils literal"><span class="pre">BOOST_FOREACH</span></tt> macro has recently
been accepted into Boost (not yet part of the distribution)
and Trolltech has provided a <a class="reference" href="http://doc.trolltech.com/4.0/qt4-tulip.html">foreach</a> macro for QT 4.</p>
</div>
<div class="section" id="examples">
<h1><a class="toc-backref" href="#id2" name="examples">2&nbsp;&nbsp;&nbsp;Examples</a></h1>
<p>The new for-loop is based on half-open iterator ranges
of the form <tt class="docutils literal"><span class="pre">[begin,end)</span></tt>. An example of its usage
could be</p>
<pre class="literal-block">
vector&lt;int&gt; vec = ...;
for( int i : vec )
    std::cout &lt;&lt; i; 
</pre>
<p>Remark: the above syntax received consensus in Sydney.</p>
<p>One can also access the values of the range as references</p>
<pre class="literal-block">
vector&lt;int&gt; vec = ...;
for( int&amp; i : vec );
    i = 42;
</pre>
<p>The construct works with a wide variety of ranges. For example:</p>
<pre class="literal-block">
// builtin arrays
int an_array[] = {...};
for( char c : an_array )
    std::cout &lt;&lt; c;

// pairs of iterators
typedef vector&lt;int&gt;::iterator iter;
std::pair&lt;iter,iter&gt; p = ...;
for( int i : p )
    std::cout &lt;&lt; i;
</pre>
<p>Remark: this proposal deals only with the core-language mechanism
that makes the loop work with a range; a separate proposal 
will consider the necessary library extensions because
the library components are of great value in many other contexts.</p>
<p>The proposal is thus dependent on the following other 
C++0x proposals:</p>
<ul class="simple">
<li><a class="reference" href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1794.pdf">n1794</a> and <a class="reference" href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1705.pdf">n1705</a> (<tt class="docutils literal"><span class="pre">auto</span></tt>/<tt class="docutils literal"><span class="pre">decltype</span></tt>)</li>
<li><a class="reference" href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1770.html">n1770</a> (move-semantics)</li>
<li><a class="reference" href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1871.html">n1871</a> (Range Library, only Part 1)</li>
</ul>
</div>
<div class="section" id="the-proposal">
<h1><a class="toc-backref" href="#id3" name="the-proposal">3&nbsp;&nbsp;&nbsp;The proposal</a></h1>
<p>The new for-loop is based on
the compiler calling standard library functions. A loop
like</p>
<pre class="literal-block">
for( int i : vec )
    std::cout &lt;&lt; i;
</pre>
<p>is translated into</p>
<pre class="literal-block">
using std::begin;   // enable ADL
using std::end;     // ditto
auto&amp;&amp; __rng( vec );
for( auto __begin = begin(__rng), __end = end(__rng);
     __begin != __end; 
     ++__begin )
{
    int i = *__begin;
    std::cout &lt;&lt; i;
}      
</pre>
<p>Notice that the end iterator is only calculated once.</p>
<!-- - Because ``__rng`` is an rvalue-reference we can very -->
<!-- efficiently work on structures like ``vector<string>``. -->
<p>The user is required to include the standard header <tt class="docutils literal"><span class="pre">&lt;iterator&gt;</span></tt> in which 
the default version of <tt class="docutils literal"><span class="pre">begin()/end()</span></tt> is defined:</p>
<pre class="literal-block">
namespace std
{
    template&lt; class T &gt;
    auto begin( T&amp;&amp; t ) -&gt; decltype( t.begin() )
    {
        return t.begin();
    }

    template&lt; class T &gt;
    auto end( T&amp;&amp; t ) -&gt; decltype( t.end() )
    {
        return t.end();
    }
}  
</pre>
<p>The entire library support needed for the new loop
may be found in <a class="reference" href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1871.html">n1871</a> (Part 1).</p>
</div>
<span id="range-library-proposal"></span><div class="section" id="making-a-custom-type-work-with-the-for-loop">
<h1><a class="toc-backref" href="#id4" name="making-a-custom-type-work-with-the-for-loop">4&nbsp;&nbsp;&nbsp;Making a custom type work with the for loop</a></h1>
<p>The protocol for making a non-conforming UDT work with the new for loop is
quite simple:</p>
<pre class="literal-block">
// UDT
class MyContainer
{
    char*  data_;
    size_t size_;
    
 public:
    
    char*       Begin() { return data_; }
    const char* Begin() const { return data_; }
    char*       End() { return data_ + size; }
    const char* End() const { return data_ + size; }
};

// for-loop requirements
char*       begin( MyContainer&amp; c )       { return c.Begin(); }
const char* begin( const MyContainer&amp; c ) { return c.End(); }
char*       begin( MyContainer&amp; c )       { return c.Begin(); }
const char* begin( const MyContainer&amp; c ) { return c.End(); }    
</pre>
<p>We then rely on ADL for finding the right version of <tt class="docutils literal"><span class="pre">begin()</span></tt> and
<tt class="docutils literal"><span class="pre">end()</span></tt>.</p>
<p>Alternatively the user may apply one of the utilities that
comes with the library:</p>
<pre class="literal-block">
MyContainer m = ...;

for( char c : std::make_range(m.Begin(),m.End()) )
    ...         
</pre>
</div>
<div class="section" id="alternative-design">
<h1><a class="toc-backref" href="#id5" name="alternative-design">5&nbsp;&nbsp;&nbsp;Alternative design</a></h1>
<p>One could also imagine
a different design which did not rely
on inclusion of the <tt class="docutils literal"><span class="pre">&lt;iterator&gt;</span></tt> header.</p>
<p>The benefits would be</p>
<ul class="simple">
<li>it is simpler</li>
<li>slightly easier support for UDTs</li>
</ul>
<p>The downsides would be</p>
<ul class="simple">
<li>support for eg. <tt class="docutils literal"><span class="pre">pair&lt;iterator,iterator&gt;</span></tt>
would require an adapter</li>
</ul>
<p>Including <tt class="docutils literal"><span class="pre">&lt;iterator</span></tt> should <em>not</em> be seen
as a big problem, though. It is impossible not
to include it implicitly when using one
of the standard containers.</p>
</div>
<div class="section" id="acknowledgements">
<h1><a class="toc-backref" href="#id6" name="acknowledgements">6&nbsp;&nbsp;&nbsp;Acknowledgements</a></h1>
<p>The author would like to thank Lawrence Crowl, 
Bjarne Stroustrup, Doug Gregor, Daveed Vandevoorde and
Peter Dimov.</p>
<!-- ############################################### -->
<!-- ################## Links ###################### -->
<!-- ############################################### -->
<!-- ========= -->
<!-- Footnotes -->
<!-- ========= -->
<!-- ############################################### -->
<!-- ################## Footnotes ################## -->
<!-- ############################################### -->
</div>
</div>
</body>
</html>
