<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1129: istream(buf)_iterator should support literal sentinel value</title>
<meta property="og:title" content="Issue 1129: istream(buf)_iterator should support literal sentinel value">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1129.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#Resolved">Resolved</a> status.</em></p>
<h3 id="1129"><a href="lwg-defects.html#1129">1129</a>. <code>istream(buf)_iterator</code> should support literal sentinel value</h3>
<p><b>Section:</b> 24.6.2.2 <a href="https://wg21.link/istream.iterator.cons">[istream.iterator.cons]</a>, 24.6.4 <a href="https://wg21.link/istreambuf.iterator">[istreambuf.iterator]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2009-05-30 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#istream.iterator.cons">issues</a> in [istream.iterator.cons].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>istream_iterator</code> and <code>istreambuf_iterator</code> should support literal sentinel
values.  The default constructor is frequently used to terminate ranges, and
could easily be a literal value for <code>istreambuf_iterator</code>, and
<code>istream_iterator</code> when iterating value types.  A little more work using a
suitably sized/aligned char-array for storage (or an updated component like
<code>boost::optional</code> proposed for TR2) would allow <code>istream_iterator</code> to support
<code>constexpr</code> default constructor in all cases, although we might leave this
tweak as a QoI issue.  Note that requiring <code>constexpr</code> be supported also
allows us to place no-throw guarantees on this constructor too.
</p>

<p><i>[
2009-06-02 Daniel adds:
]</i></p>


<blockquote>
<p>
I agree with the usefulness of the issue suggestion, but we need
to ensure that <code>istream_iterator</code> <em>can</em> satisfy be literal if needed.
Currently this is not clear, because 24.6.2 <a href="https://wg21.link/istream.iterator">[istream.iterator]</a>/3 declares
a copy constructor and a destructor and explains their semantic in
24.6.2.2 <a href="https://wg21.link/istream.iterator.cons">[istream.iterator.cons]</a>/3+4.
</p>
<p>
The prototype semantic specification is ok (although it seems
somewhat redundant to me, because the semantic doesn't say
anything interesting in both cases), but for support of trivial class
types we also need a trivial copy constructor and destructor as of
11 <a href="https://wg21.link/class">[class]</a>/6. The current non-informative specification of these
two special members suggests to remove their explicit declaration
in the class and add explicit wording that says that if <code>T</code> is
trivial a default constructed iterator is also literal, alternatively it
would be possible to mark both as defaulted and add explicit
(memberwise) wording that guarantees that they are trivial.
</p>
<p>
Btw.: I'm quite sure that the <code>istreambuf_iterator</code> additions to
ensure triviality are not sufficient as suggested, because the
library does not yet give general guarantees that a defaulted
special member declaration makes this member also trivial.
Note that e.g. the atomic types do give a general statement!
</p>
<p>
Finally there is a wording issue: There does not exist something
like a "literal constructor". The core language uses the term
"constexpr constructor" for this.
</p>
<p>
Suggestion:
</p>
<ol>
<li>
<p>
Change 24.6.2 <a href="https://wg21.link/istream.iterator">[istream.iterator]</a>/3 as indicated:
</p>
<blockquote><pre>
<ins>constexpr</ins> istream_iterator();
istream_iterator(istream_type&amp; s);
istream_iterator(const istream_iterator<del>&lt;T,charT,traits,Distance&gt;</del>&amp; x)<ins> = default</ins>;
~istream_iterator()<ins> = default</ins>;
</pre></blockquote>
</li>
<li>
<p>
Change 24.6.2.2 <a href="https://wg21.link/istream.iterator.cons">[istream.iterator.cons]</a>/1 as indicated:
</p>
<blockquote><pre>
<ins>constexpr</ins> istream_iterator();
</pre>
<blockquote><p>
-1- <i>Effects:</i> Constructs the end-of-stream iterator. <ins>If <code>T</code> is a literal type,
then this constructor shall be a constexpr constructor.</ins>
</p></blockquote>
</blockquote>
</li>
<li>
<p>
Change 24.6.2.2 <a href="https://wg21.link/istream.iterator.cons">[istream.iterator.cons]</a>/3 as indicated:
</p>
<blockquote><pre>
istream_iterator(const istream_iterator<del>&lt;T,charT,traits,Distance&gt;</del>&amp; x)<ins> = default</ins>;
</pre>
<blockquote><p>
-3- <i>Effects:</i> Constructs a copy of <code>x</code>. <ins>If <code>T</code> is a literal type, then
this constructor shall be a trivial copy constructor.</ins>
</p></blockquote>
</blockquote>
</li>
<li>
<p>
Change 24.6.2.2 <a href="https://wg21.link/istream.iterator.cons">[istream.iterator.cons]</a>/4 as indicated:
</p>

<blockquote><pre>
~istream_iterator()<ins> = default</ins>;
</pre>
<blockquote><p>
-4- <i>Effects:</i> The iterator is destroyed. <ins>If <code>T</code> is a literal type, then
this destructor shall be a trivial
destructor.</ins>
</p></blockquote>
</blockquote>
</li>
<li>
<p>
Change 24.6.4 <a href="https://wg21.link/istreambuf.iterator">[istreambuf.iterator]</a> before p. 1 as indicated:
</p>

<blockquote><pre>
<ins>constexpr</ins> istreambuf_iterator() throw();
<ins>istreambuf_iterator(const istreambuf_iterator&amp;)  throw() = default;</ins>
<ins>~istreambuf_iterator()  throw() = default;</ins>
</pre></blockquote>
</li>
<li>
<p>
Change 24.6.4 <a href="https://wg21.link/istreambuf.iterator">[istreambuf.iterator]</a>/1 as indicated:
</p>
<blockquote><p>
[..] The default constructor <code>istreambuf_iterator()</code> and the constructor
<code>istreambuf_iterator(0)</code> both construct an end of stream iterator object 
suitable for use as an end-of-range. <ins>All specializations of 
<code>istreambuf_iterator</code> shall have a trivial copy constructor, a constexpr 
default constructor and a trivial destructor.</ins>
</p></blockquote>
</li>
</ol>
</blockquote>

<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
<del>NAD Editorial</del><ins>Resolved</ins>.  Addressed by
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2994.htm">N2994</a>.
</p></blockquote>



<p id="res-1129"><b>Proposed resolution:</b></p>
<p>
24.6.2 <a href="https://wg21.link/istream.iterator">[istream.iterator]</a> para 3
</p>

<blockquote><pre>
<ins>constexpr</ins> istream_iterator();
</pre></blockquote>

<p>
24.6.2.2 <a href="https://wg21.link/istream.iterator.cons">[istream.iterator.cons]</a>
</p>

<blockquote><pre>
<ins>constexpr</ins> istream_iterator();
</pre>
<blockquote><p>
-1- <i>Effects:</i> Constructs the end-of-stream iterator.
<ins>If <code>T</code> is a literal type, then this constructor shall
be a literal constructor.</ins>
</p></blockquote>
</blockquote>

<p>
24.6.4 <a href="https://wg21.link/istreambuf.iterator">[istreambuf.iterator]</a>
</p>

<blockquote><pre>
<ins>constexpr</ins> istreambuf_iterator() throw();
</pre></blockquote>






</body>
</html>
