<?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.7: http://docutils.sourceforge.net/" />
<title>Proposal for new for-loop</title>
<meta name="author" content="Thorsten Ottosen" />
<meta name="date" content="2005-04-27" />
<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;}

</style>
</head>
<body>
<div class="document" id="proposal-for-new-for-loop">
<h1 class="title">Proposal for new for-loop</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-04-27</td></tr>
<tr class="field"><th class="docinfo-name">Number:</th><td class="field-body">WG21/N1796 and  J16/05-0056</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="#acknowledgements" id="id5" name="id5">5&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 class="simple">
<li><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.</li>
</ul>
<!-- * A good for-each construct also makes the need for language-level lambda-functions
  much less necessary because the looping construct is trivial. -->
<ul>
<li><p class="first"><em>Performance</em>. A third 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>
</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 should also work with a wide variety of ranges. For example:</p>
<pre class="literal-block">
// builtin arrays
const int[] integers = { 1, 2, 3, 4, 5, 6 };
for( int i : integers )
    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>
</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>This proposal suggest that 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
for( auto __begin = begin(vec),
          __end   = end(vec);
     __begin != __end; ++__begin )
{
    int i = *__begin;
    std::cout &lt;&lt; i;
}          
</pre>
<p>Notice that the end iterator is only calculated once.</p>
<p>The user is <em>required</em> 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>
<!-- Remarks: specialized versions of ``std::range_traits<T>`` are supplied
for eg. ``std::pair`` are provided in the appropriate header.
It is recommended that specializations for built-in arrays are provided. -->
</div>
<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() const { return data_; } 
    char* End() const { return data_ + size; } 
};

// for-loop requirements
std::if_&lt; std::is_const&lt;MyContainer&gt;, const char*, char*&gt;::type
begin( MyContainer&amp;&amp; c )
{
    return c.Begin();
}   

std::if_&lt; std::is_const&lt;MyContainer&gt;, const char*, char*&gt;::type 
end( MyContainer&amp;&amp; c )
{
    return c.End();
}   
</pre>
<p>Notice that even though the original container was not const-correct, the new range interface
is. Alternatively the user may just write an adapter</p>
<pre class="literal-block">
class my_range_adapter
{
    MyContainer&amp; c;
public:
    my_range_adapter( MyContainer&amp; r ) : c(r)
    { }

    char* begin()
    {
        return c.Begin();
    }   

    const char* begin() const
    {
        return c.Begin();
    }   

    char* end()
    {
        return c.End();
    }   

    const char* end() const
    {
        return c.End();
    }   
};
...
// usage
MyContainer m = ...;
for( char c : my_range_adapter(m) )
    ...;
</pre>
<p>Basically we here exploit that the default version of
<tt class="docutils literal"><span class="pre">.begin()/.end()</span></tt> are such that they work with
standard containers.</p>
<p>As a third alternative the user may apply one of the utilities that
comes with the standard library:</p>
<pre class="literal-block">
for( char c : std::make_iterator_range(m.Begin(),m.End()) )
    ...         
</pre>
</div>
<div class="section" id="acknowledgements">
<h1><a class="toc-backref" href="#id5" name="acknowledgements">5&nbsp;&nbsp;&nbsp;Acknowledgements</a></h1>
<p>The author would like to thank Lawrence Crowl, Bjarne Stroustrup,
Doug Gregor and Daveed Vandevoorde.</p>
<!-- ############################################### -->
<!-- ################## Links ###################### -->
<!-- ############################################### -->
<!-- ========= -->
<!-- Footnotes -->
<!-- ========= -->
<!-- ############################################### -->
<!-- ################## Footnotes ################## -->
<!-- ############################################### -->
</div>
</div>
</body>
</html>
