<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2714: complex stream extraction underspecified</title>
<meta property="og:title" content="Issue 2714: complex stream extraction underspecified">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2714.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#New">New</a> status.</em></p>
<h3 id="2714"><a href="lwg-active.html#2714">2714</a>. <code>complex</code> stream extraction underspecified</h3>
<p><b>Section:</b> 29.4.6 <a href="https://wg21.link/complex.ops">[complex.ops]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Tim Song <b>Opened:</b> 2016-05-23 <b>Last modified:</b> 2018-10-16</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#complex.ops">issues</a> in [complex.ops].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The specification of <code>operator&gt;&gt;(istream&amp;, complex&lt;T&gt;&amp;)</code> is extremely short on details. 
It currently reads, in its entirety (29.4.6 <a href="https://wg21.link/complex.ops">[complex.ops]</a>/12-15):
</p>
<blockquote class="note">
<pre>
template&lt;class T, class charT, class traits&gt;
basic_istream&lt;charT, traits&gt;&amp; operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp; is, complex&lt;T&gt;&amp; x);
</pre>
<blockquote>
<p>
<i>Effects:</i> Extracts a complex number <code>x</code> of the form: <code>u</code>, <code>(u)</code>, or <code>(u,v)</code>, 
where <code>u</code> is the real part and <code>v</code> is the imaginary part (31.7.5.3 <a href="https://wg21.link/istream.formatted">[istream.formatted]</a>).
<p/>
<i>Requires:</i> The input values shall be convertible to <code>T</code>.
<p/>
If bad input is encountered, calls <code>is.setstate(ios_base::failbit)</code> (which may throw <code>ios::failure</code> 
(31.5.4.4 <a href="https://wg21.link/iostate.flags">[iostate.flags]</a>)).
<p/>
<i>Returns:</i> <code>is</code>.
<p/>
<i>Remarks:</i> This extraction is performed as a series of simpler extractions. Therefore, the skipping of whitespace 
is specified to be the same for each of the simpler extractions.
</p>
</blockquote>
</blockquote>
<p>
It is completely unclear:
</p>
<ul>
<li>which "simpler extractions" are performed,</li>
<li>how the extracted characters are matched to the special characters <code>'('</code>, <code>')'</code> and <code>','</code> 
(by <code>==</code>, or by <code>traits::eq</code>),</li>
<li>what is left in the stream on failure. (For example, with <code>"(0, 0]"</code>, libstdc++ extracts the <code>]</code> 
while libc++ leaves it in the stream.)</li>
</ul>
<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<blockquote class="note">
<i>Drafting note:</i> the following wording is based on:
<ul>
<li>Characters are extracted using <code>operator&gt;&gt;</code> and compared using <code>traits::eq</code>.</li>
<li>Mismatched characters are returned to the stream. </li>
</ul>
</blockquote>
<p>This wording is relative to N4582.</p>
<ol>
<li><p>Replace 29.4.6 <a href="https://wg21.link/complex.ops">[complex.ops]</a>/12-15 with the following paragraphs:</p>

<blockquote>
<pre>
template&lt;class T, class charT, class traits&gt;
basic_istream&lt;charT, traits&gt;&amp;
operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp; is, complex&lt;T&gt;&amp; x);
</pre>
<blockquote>
<p>
-?- <i>Effects:</i> First, extracts a character from <code>is</code>.
<ul>
<li>If the character extracted is equal to <code>is.widen('(')</code>, extracts an object <code>u</code> of type <code>T</code> from <code>is</code>, 
then extracts a character from <code>is</code>.
<ul>
    <li>
    If this character is equal to <code>is.widen(')')</code>, then assigns <code>complex&lt;T&gt;(u)</code> to <code>x</code>.
    </li>
    <li>
      Otherwise, if this character is equal to <code>is.widen(',')</code>, extracts an object <code>v</code> of type <code>T</code> 
      from <code>is</code>, then extracts a character from <code>is</code>.
      If this character is equal to <code>is.widen(')')</code>, then assigns <code>complex&lt;T&gt;(u, v)</code> to <code>x</code>;
      otherwise returns the character to <code>is</code> and the extraction fails.
    </li>
    <li>
      Otherwise, returns the character to <code>is</code> and the extraction fails.
    </li>
  </ul>
</li>
<li> Otherwise, returns the character to <code>is</code>, extracts an object <code>u</code> of type <code>T</code> from <code>is</code>, and 
assigns <code>complex&lt;T&gt;(u)</code> to <code>x</code>.
</li>
</ul>
In the description above, characters are extracted from <code>is</code> as if by <code>operator&gt;&gt;</code> 
(31.7.5.3.3 <a href="https://wg21.link/istream.extractors">[istream.extractors]</a>), and returned to the stream as if by <code>basic_istream::putback</code> 
(31.7.5.4 <a href="https://wg21.link/istream.unformatted">[istream.unformatted]</a>). Character equality is determined using <code>traits::eq</code>.
An object <code>t</code> of type <code>T</code> is extracted from <code>is</code> as if by <code> is &gt;&gt; t</code>.
<p/>
If any extraction operation fails, no further operation is performed and the whole extraction fails.
<p/>
On failure, calls <code>is.setstate(ios_base::failbit)</code> (which may throw <code>ios::failure</code>
(31.5.4.4 <a href="https://wg21.link/iostate.flags">[iostate.flags]</a>)).
<p/>
-?- <i>Returns:</i> <code>is</code>.
<p/>
-?- [<i>Note</i>: This extraction is performed as a series of simpler extractions. Therefore, the skipping of whitespace
is specified to be the same for each of the simpler extractions. &mdash; <i>end note</i>]
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>
<p><i>[2017-12-13 Tim Song adjusts the P/R to avoid relying on <code>putback</code>.]</i></p>



<p id="res-2714"><b>Proposed resolution:</b></p>
<blockquote class="note">
<i>Drafting note:</i> the following wording assumes that:
<ul>
<li>Characters are extracted using <code>operator&gt;&gt;</code> and compared using <code>traits::eq</code>.</li>
<li>Mismatched characters are not extracted. </li>
<li><code>x</code> is assigned a value-initialized <code>complex</code> on failure for consistency with the arithmetic 
extractors (compare LWG <a href="lwg-defects.html#696" title="istream::operator&gt;&gt;(int&amp;) broken (Status: C++11)">696</a><sup><a href="https://cplusplus.github.io/LWG/issue696" title="Latest snapshot">(i)</a></sup>). </li>
</ul>
</blockquote>
<p>This wording is relative to <a href="https://wg21.link/N4778">N4778</a>.</p>
<ol>
<li><p>Replace 29.4.6 <a href="https://wg21.link/complex.ops">[complex.ops]</a>/12-16 with the following paragraphs:</p>

<blockquote>
<pre>
template&lt;class T, class charT, class traits&gt;
basic_istream&lt;charT, traits&gt;&amp;
operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp; is, complex&lt;T&gt;&amp; x);
</pre>
<blockquote>
<p>
-?- <i>Effects:</i> Let <code><i>PEEK</i>(is)</code> be a formatted input function (31.7.5.3.1 <a href="https://wg21.link/istream.formatted.reqmts">[istream.formatted.reqmts]</a>) of 
<code>is</code> that returns the next character that 
would be extracted from <code>is</code> by <code>operator&gt;&gt;</code>. [<i>Note</i>: The <code>sentry</code> object is constructed 
and destroyed,
 but the returned character is not extracted from the stream. &mdash; <i>end note</i>] 
<ul>
<li>If <code><i>PEEK</i>(is)</code> is not equal to <code>is.widen('(')</code>, extracts an object <code>u</code> of type <code>T</code> 
from <code>is</code>, and assigns <code>complex&lt;T&gt;(u)</code> to <code>x</code>.</li>
<li>Otherwise, extracts that character from <code>is</code>, then extracts an object <code>u</code> of type <code>T</code> from <code>is</code>, then:
<ul>
   <li>If <code><i>PEEK</i>(is)</code> is equal to <code>is.widen(')')</code>, then extracts that character from <code>is</code> and 
   assigns <code>complex&lt;T&gt;(u)</code> to <code>x</code>. </li>
   <li>Otherwise, if it is equal to <code>is.widen(',')</code>, then extracts that character from <code>is</code> and then extracts 
   an object <code>v</code> of type <code>T</code> from <code>is</code>, then:
    <ul>
       <li>If <code><i>PEEK</i>(is)</code> is equal to <code>is.widen(')')</code>, then extracts that character from <code>is</code> and 
       assigns <code>complex&lt;T&gt;(u, v)</code> to <code>x</code>.</li>
       <li> Otherwise, the extraction fails.</li>
    </ul>
   </li>
   <li>Otherwise, the extraction fails.</li>
</ul>
</li>
</ul>
In the description above, characters are extracted from <code>is</code> as if by <code>operator&gt;&gt;</code> (31.7.5.3.3 <a href="https://wg21.link/istream.extractors">[istream.extractors]</a>), character equality is determined using <code>traits::eq</code>, and an object <code>t</code> of type <code>T</code> is extracted from <code>is</code> 
as if by <code> is &gt;&gt; t</code>.
<p/>
If any extraction operation fails, no further operation is performed and the whole extraction fails.
<p/>
On failure, assigns <code>complex&lt;T&gt;()</code> to <code>x</code> and calls <code>is.setstate(ios_base::failbit)</code> (which may throw 
<code>ios::failure</code> (31.5.4.4 <a href="https://wg21.link/iostate.flags">[iostate.flags]</a>)).
<p/>
-?- <i>Returns:</i> <code>is</code>.
<p/>
-?- [<i>Note</i>: This extraction is performed as a series of simpler extractions. Therefore, the skipping of whitespace
is specified to be the same for each of the simpler extractions. &mdash; <i>end note</i>]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
