<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2328: Rvalue stream extraction should use perfect forwarding</title>
<meta property="og:title" content="Issue 2328: Rvalue stream extraction should use perfect forwarding">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2328.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#C++17">C++17</a> status.</em></p>
<h3 id="2328"><a href="lwg-defects.html#2328">2328</a>. Rvalue stream extraction should use perfect forwarding</h3>
<p><b>Section:</b> 31.7.5.6 <a href="https://wg21.link/istream.rvalue">[istream.rvalue]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2013-09-21 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#istream.rvalue">issues</a> in [istream.rvalue].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
31.7.5.6 <a href="https://wg21.link/istream.rvalue">[istream.rvalue]</a> declares <code>operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp;&amp; is, T&amp; x)</code>.  
However,  [istream::extractors]/7 declares <code>operator&gt;&gt;(basic_istream&lt;charT,traits&gt;&amp; in, charT* s)</code>, 
plus additional overloads for <code>unsigned char*</code> and <code>signed char*</code>. This means that 
"<code>return_rvalue_istream() &gt;&gt; &amp;arr[0]</code>" won't compile, because <code>T&amp;</code> won't bind to the rvalue 
<code>&amp;arr[0]</code>.
</p>

<p><i>[2014-02-12 Issaquah : recategorize as P3]</i></p>


<p>
Jonathan Wakely: Bill was certain the change is right, I think so with less certainty
</p>

<p>
Jeffrey Yaskin: I think he's right, hate that we need this
</p>

<p>
Jonathan Wakely: is this the security issue Jeffrey raised on lib reflector?
</p>

<p>
Move to P3
</p>

<p><i>[2015-05-06 Lenexa: Move to Review]</i></p>


<p>WEB, MC: Proposed wording changes one signature (in two places) to take a forwarding reference.</p>
<p>TK: Should be consistent with an istream rvalue?</p>
<p>MC: This is the case where you pass the stream by rvalue reference.</p>
<p>RP: I would try it before standardizing.</p>
<p>TK: Does it break anything?</p>
<p>RP, TK: It will take all arguments, will be an exact match for everything.</p>
<p>RS, TK: This adapts streaming into an rvalue stream to make it act like streaming into an lvalue stream.</p>
<p>RS: Should this really return the stream by lvalue reference instead of by rvalue reference? &rArr; new LWG issue.</p>
<p>RP: Security issue?</p>
<p>MC: No. That's <code>istream &gt;&gt; char*</code>, C++ version of <code>gets()</code>. Remove it, as we did for 
<code>gets()</code>? &rArr; new LWG issue.</p>
<p>RS: Proposed resolution looks correct to me.</p>
<p>MC: Makes me (and Jonathan Wakely) feel uneasy.</p>
<p>Move to Review, consensus.</p>

<p><i>[2016-01-15, Daniel comments and suggests improved wording]</i></p>

<p>
It has been pointed out by Tim Song, that the initial P/R (deleting the <i>Returns</i> paragraph and making the <i>Effects</i> 
"equivalent to <code>return is &gt;&gt; /* stuff */;</code>") breaks cases where the applicable <code>operator&gt;&gt;</code> doesn't 
return a type that is convertible to a reference to the stream:
</p>
<blockquote><pre>
struct A{};
void operator&gt;&gt;(std::istream&amp;, A&amp;){ }

void f() {
  A a;
  std::istringstream() &gt;&gt; a; // was OK, now error
}
</pre></blockquote>
<p>
This seems like an unintended wording artifact of the "Equivalent to" Phrase Of Power
and could be easily fixed by changing the second part of the P/R slightly as follows:
</p>
<blockquote><pre>
template &lt;class charT, class traits, class T&gt;
  basic_istream&lt;charT, traits&gt;&amp;
  operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp;&amp; is, T&amp;<ins>&amp;</ins> x);
</pre><blockquote>
<p>
-1- <i>Effects:</i> <ins>Equivalent to:</ins><del><code>is &gt;&gt;x</code></del>
</p>
<blockquote><pre>
<ins>is &gt;&gt; std::forward&lt;T&gt;(x);
return is;</ins>
</pre></blockquote>
<p>
<del>-2- <i>Returns:</i> <code>is</code></del>
</p>
</blockquote></blockquote>

<p>
<strong>Previous resolution [SUPERSEDED]:</strong>
</p>
<blockquote class="note">
<p>This wording is relative to N4567.</p>

<ol>
<li><p>Edit  [iostream.format.overview], header <code>&lt;istream&gt;</code> synopsis, as indicated:</p>

<blockquote><pre>
namespace std {
  [&hellip;]
  template &lt;class charT, class traits, class T&gt;
    basic_istream&lt;charT, traits&gt;&amp;
    operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp;&amp; is, T&amp;<ins>&amp;</ins> x);
}
</pre></blockquote>
</li>

<li><p>Edit 31.7.5.6 <a href="https://wg21.link/istream.rvalue">[istream.rvalue]</a> as indicated:</p>

<blockquote><pre>
template &lt;class charT, class traits, class T&gt;
  basic_istream&lt;charT, traits&gt;&amp;
  operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp;&amp; is, T&amp;<ins>&amp;</ins> x);
</pre><blockquote>
<p>
-1- <i>Effects:</i> <ins>Equivalent to <code>return </code></ins><code>is &gt;&gt;<del>x</del> <ins>std::forward&lt;T&gt;(x)</ins></code>
<p/>
<del>-2- <i>Returns:</i> <code>is</code></del>
</p>
</blockquote></blockquote>
</li>
</ol>
</blockquote>

<p><i>[2016-03 Jacksonville: Move to Ready]</i></p>



<p id="res-2328"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4567.</p>

<ol>
<li><p>Edit  [iostream.format.overview], header <code>&lt;istream&gt;</code> synopsis, as indicated:</p>

<blockquote><pre>
namespace std {
  [&hellip;]
  template &lt;class charT, class traits, class T&gt;
    basic_istream&lt;charT, traits&gt;&amp;
    operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp;&amp; is, T&amp;<ins>&amp;</ins> x);
}
</pre></blockquote>
</li>

<li><p>Edit 31.7.5.6 <a href="https://wg21.link/istream.rvalue">[istream.rvalue]</a> as indicated:</p>

<blockquote><pre>
template &lt;class charT, class traits, class T&gt;
  basic_istream&lt;charT, traits&gt;&amp;
  operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp;&amp; is, T&amp;<ins>&amp;</ins> x);
</pre><blockquote>
<p>
-1- <i>Effects:</i> <ins>Equivalent to:</ins><del><code>is &gt;&gt;x</code></del>
</p>
<blockquote><pre>
<ins>is &gt;&gt; std::forward&lt;T&gt;(x);
return is;</ins>
</pre></blockquote>
<p>
<del>-2- <i>Returns:</i> <code>is</code></del>
</p>
</blockquote></blockquote>

</li>
</ol>





</body>
</html>
