<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
  
  <style type="text/css">
.comment { color: #999999; font-style: italic; }
.pre { color: #000099; }
.string { color: #009900; }
.char { color: #009900; }
.float { color: #996600; }
.int { color: #999900; }
.bool { color: #000000; font-weight: bold; }
.type { color: #FF6633; }
.flow { color: #FF0000; }
.keyword { color: #990000; }
.operator { color: #663300; font-weight: bold; }
.operator { color: #663300; font-weight: bold; }
pre.code {
    border: 2px solid #666;
    background-color: #F4F4F4;
    padding-left: 10px;
    padding-top: 0px;
}
code {
    border: 2px solid #d0d0d0;
    background-color: LightYellow;
    padding: 2px;
    padding-left: 10px;
    display:table;
    white-space:pre;
    margin:2px;
    margin-bottom:10px;
}
dt
{
    font-weight: bold;
}
    
.ins {
    background-color:#A0FFA0;
}

.del {
    background-color:#FFA0A0;
    text-decoration:line-through
}    
  </style>
  <title>Delimited iterators (Rev. 2)</title>

  
</head>
<body>
<p>N4007<br>
Revision of N3581<br>
2014-05-25<br>
Mike Spertus, Symantec<br>
<a href="mailto:mike_spertus@symantec.com">mike_spertus@symantec.com</a><br>
Nathan Wilson<br>
<a href="mailto:nwilson20@gmail.com">nwilson20@gmail.com</a><br>
<br></p>
<h1>Delimited iterators (Rev. 2)</h1>
<p>It is extremely tempting to use <tt>ostream_iterator</tt> to, say, print
a vector like:
<code>vector&lt;int&gt; v = {1, 4, 6};
cout &lt;&lt; "(";
copy(v.begin(), v.end(), ostream_iterator&lt;int&gt;(cout, ", "));
cout &lt;&lt; ")"; <span class="comment">// Oops! Prints (1, 4, 6, )</span></code></p>
<p>The problem is that the &ldquo;delimiter&rdquo; in the <tt>ostream_iterator</tt>
constructor call is better described as a suffix than a delimiter.</p>
<h2>Proposal</h2>
    <p>We propose
a new <tt>ostream_joiner</tt> class that acts like <tt>ostream_iterator</tt>
except that the delimiter is only placed between output elements:
<code>vector&lt;int&gt; v = {1, 4, 6};
cout &lt;&lt; "(";
copy(v.begin(), v.end(), std::make_ostream_joiner(cout, ", "));
cout &lt;&lt; ")"; <span class="comment">// Prints (1, 4, 6) as desired</span></code></p>
<p>If we want the delimiter after each element, like in <tt>ostream_iterator</tt>, we can specify
    it as follows<code>vector&lt;int&gt; v = {1, 4, 6};
<span class="comment">// Prints 1, 4, and 6 on their own lines as desired</span>
copy(v.begin(), v.end(), std::make_ostream_joiner(cout, "\n", std::suffix));
</code></p>
<h2>Discussion</h2>
<p>In the LEWG discussion of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3581.html">N3581</a>
in Issaquah, it was pointed out that the interactions of this proposal were unexpectedly subtle, so the authors
wish to discuss the design decisions in more detail.
<ul><li>As pointed out in LEWG, output iterators can be defined as having <tt>void</tt> value type (&sect;24.4.1p1),
    so the type being iterated need not be specified. It is apparently included in <tt>ostream_iterator</tt> for
    stylistic reasons only. Since brevity is of the essence in most uses of convenience classes such as this,
    we choose to omit the value type from <tt>ostream_joiner</tt>.</li>
<li>Since we are changing template parameters, we reject Option 1 from N3581(add functionality to <tt>ostream_iterator</tt> instead
    of creating a new class) and create a new class instead.</li>
<li>Since <tt>ostream_joiner</tt> is necessarily stateful and iterators are <em>CopyConstructible</em> and <em>CopyAssignable</em>,
    (&sect;24.2.2p2), we need to consider what happens when <tt>ostream_joiner</tt>s are copied. According to &sect;24.2.4p2,
    &ldquo;<em>Assignment through the same value of the iterator happens only once.</em>&rdquo; (Emphasis in the original).
    At Issaquah, this was interpreted as meaning that writing to one copy of an output iterator invalidates writing through
    the original output iterator. This conveniently means that the <tt>ostream_joiner</tt> can store whether we are
    outputting the first element as a member, without the need for undesirable shared state (although it is not prohibited).</li>
<li>There was a strong preference for an <tt>enum</tt> to distinguish between <tt>infix</tt> and <tt>suffix</tt> delimiters.
    I chose to use an ordinary non-scoped <tt>enum</tt> in <tt>std</tt> because scoped or local <tt>enum</tt>s would
    be very wordy for the convenience uses envisioned for a class like this. It is the authors belief that one of
    the reasons that <tt>ostream_iterator</tt> seems less used than one would expect is the long and cumbersome expression
    one needs to type.</li>
<li>Having an <tt>infix</tt> and <tt>suffix</tt> style begs the question of whether there should be a <tt>prefix</tt> style.
    We will pose this question to LEWG when presenting.</li>
<li>Another possible variant would be to have a separate prefix, infix, and suffix to the list.
    <code>copy(v.begin(), v.end(), std::ostream_joiner(cout, {"(", ", ", ")"}));</code> On the one hand,
    this may be guilding the lily. On the other hand, most of the uses we have run into would leverage this and those that don't
    could use the simpler notation, and it obviates the need for the annoying <tt>delimiter_style</tt> enum.</li>
<li>We provide a <tt>make_ostream_joiner</tt> function to simplify the deduction of template parameters as suggested by LEWG.</li>
<li>If <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3602.html">Template parameter deduction for constructors</a> is eventually adopted, we will be able to dispense with the <tt>make_</tt> prefix for notation that
    will be more concise without sacrificing clarity:<code>copy(v.begin(), v.end(), std::ostream_joiner(cout, ", "));</code></li>
</ul></p>
<h2>Implementation</h2>
Straightforward. I give my students a homework assignment to implement <tt>ostream_joiner</tt> every year.
<h2>Wording</h2>
Modify &sect;24.3 [iterator.synopsis] as follows:
<blockquote><pre style="padding:0;margin:0"><span class="comment">//24.6, stream iterators:</span>
<span class="ins">enum delimiter_type {</span>
<span class="ins">  infix,</span>
<span class="ins">  suffix</span>
<span class="ins">};</span>

template &lt;class T, class charT = char, class traits = char_traits&lt;charT&gt;,
    class Distance = ptrdiff_t&gt;
class istream_iterator;
template &lt;class T, class charT, class traits, class Distance&gt;
  bool operator==(const istream_iterator&;t'T,charT,traits,Distance&gt;&amp; x,
          const istream_iterator&lt;T,charT,traits,Distance&gt;&amp; y);
template &lt;class T, class charT, class traits, class Distance&gt;
  bool operator!=(const istream_iterator&lt;T,charT,traits,Distance&gt;&amp; x,
          const istream_iterator&lt;T,charT,traits,Distance&gt;&amp; y);
          
template &lt;class charT = char, class traits = char_traits&lt;charT&gt; &gt;
    class ostream_iterator;
<span class="ins">template &lt;class charT = char, class traits = char_traits&lt;charT&gt; &gt;
    class ostream_joiner;</span>
<span class="ins">template &lt;class charT, class traits&gt;
  ostream_joiner<charT, traits> make_ostream_joiner(basic_ostream&lt;charT, traits&gt;&amp; os, delimiter_style d = infix);</span>
template&lt;class charT, class traits = char_traits&lt;charT&gt; &gt;
class istreambuf_iterator;</pre></blockquote>
Add a section &sect;24.6.x named &ldquo;Class template ostream_joiner&rdquo; [ostream.joiner] between &sect;24.6.2 [ostream.iterator] and 
&sect;24.6.3 [istreambuf.iterator]. This section should be the same as &sect;24.6.2
with all occurrences of <tt>ostream_iterator</tt> replaced with <tt>ostream_joiner</tt>
<em>mutatis mutandis</em>
except as follows:
Make &sect;24.6.x [ostream.joiner] as follows:
<blockquote><span class="ins"><tt>ostream_joiner</tt> is defined as:</span>
<blockquote><pre style="padding:0;margin:0"><span class="ins">
namespace std {
  template &lt;class charT = char, class traits = char_traits&lt;charT&gt; &gt;
  class ostream_joiner:
    public iterator&lt;output_iterator_tag, void, void, void, void&gt; {
  public:
    typedef charT char_type;
    typedef traits traits_type;
    typedef basic_ostream&lt;charT,traits&gt; ostream_type;
    ostream_joiner(ostream_type&amp; s, const charT* delimiter, delimiter_style d = infix);
    ostream_joiner(const ostream_iterator&lt;T,charT,traits&gt;&amp; x);
   ~ostream_joiner();
    template&lt;typename T&gt;
    ostream_joiner&lt;charT,traits&gt;&amp; operator=(const T&amp; value);
    ostream_joiner&lt;charT,traits&gt;&amp; operator*();
    ostream_joiner&lt;charT,traits&gt;&amp; operator++();
    ostream_joiner&lt;charT,traits&gt;&amp; operator++(int);
  private:
    basic_ostream&lt;charT,traits&gt;* out_stream; <span class="comment">// exposition only</span>
    const charT* delim; <span class="comment">// exposition only</span>
    bool first_element; <span class="comment">// exposition only</span>
    delimiter_style style; <span class="comment">// exposition only</span>
  };
}</span></pre></blockquote></blockquote>
The corresponding portion of &sect;24.6.x.1 [ostream.joiner.cons.des] should look as follows:
<blockquote><span class="ins"><tt>ostream_joiner(ostream_type&amp; s, const charT* delimiter, delimiter_style d);</tt></span>
<blockquote><span class="ins"><em>Effects:</em> Initializes <em>out_stream</em> 
with <tt>&amp;s</tt>, <em>delim</em> with <tt>delimiter</tt>, <em>style</em> with <tt>d</tt>
and <em>first_element</em> with <tt>true</tt>.</span></blockquote>
<span class="ins"><tt>ostream_joiner(const ostream_joiner&amp; x);</tt></span>
<blockquote><span class="ins"><em>Effects:</em> Constructs a copy of <tt>x</tt>.</span></blockquote></blockquote>
The corresponding portion &sect;24.6.x.2 [ostream.joiner.ops] should look as follows:
<blockquote><span class="ins"><tt>template&lt;typename T&gt;</tt></span>
<blockquote><span class="ins"><tt>ostream_joiner&amp; operator=(const T&amp; value);</tt></span>
<blockquote><span class="ins"><em>Effects:</em></span>
<blockquote><pre style="padding:0;margin:0"><span class="ins">if(<em>delim</em> != 0) {
  if(<em>style</em>== suffix || !<em>first_element</em>)
   *<em>out_stream</em> &lt;&lt; <em>delim</em>;
  <em>first_element</em> = false;
}
*<em>out_stream</em> &lt;&lt; value;
return (*this);</span></pre></blockquote></blockquote></blockquote></blockquote>
The corresponding portion of &sect;24.6.x.3 [ostream.joiner.creation] should look as follows:
<blockquote><pre><span class="ins"><tt>template &lt;class charT, class traits&gt;
  ostream_joiner<charT, traits> make_ostream_joiner(basic_ostream&lt;charT, traits&gt;&amp; os, delimiter_style d = infix);</tt></span>
<blockquote><span class="ins"><em>Returns:</em> ostream_joiner&ltcharT, traits&gt(os, delimiter, d);</pre></span>

</body></html>