<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3934: std::complex&lt;T&gt;::operator=(const T&amp;) has no specification</title>
<meta property="og:title" content="Issue 3934: std::complex&lt;T&gt;::operator=(const T&amp;) has no specification">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3934.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="3934"><a href="lwg-active.html#3934">3934</a>. <code>std::complex&lt;T&gt;::operator=(const T&amp;)</code> has no specification</h3>
<p><b>Section:</b> 29.4.3 <a href="https://wg21.link/complex">[complex]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2023-05-20 <b>Last modified:</b> 2023-06-01</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#complex">active issues</a> in [complex].</p>
<p><b>View all other</b> <a href="lwg-index.html#complex">issues</a> in [complex].</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 class template <code>complex</code> synopsis in 29.4.3 <a href="https://wg21.link/complex">[complex]</a> shows the following member function:
</p>
<blockquote><pre>
constexpr complex&amp; operator= (const T&amp;);
</pre></blockquote>
<p>
but does not specify its semantics. This affects a code example such as the following one:
</p>
<blockquote><pre>
#include &lt;complex&gt;
#include &lt;iostream&gt;

int main()
{
  std::complex&lt;double&gt; z(1, 1);
  z = 2;
  std::cout &lt;&lt; z &lt;&lt; std::endl;
}
</pre></blockquote>
<p>
This problem exists since the 1998 version of the standard (at that time this was declared in subclause [lib.complex]),
but fortunately the three major implementations all behave consistently by assigning the provided value to the real
part and nullifying the imaginary part, causing the output <code>(2, 0)</code>, which is consistent with the expected
behaviour of usual mathematical convention and that of C's built-in <code>complex</code> types. We should specify this.
<p/>
The lack of this member specification was observed while a proposed wording for LWG <a href="lwg-active.html#3933" title="P1467R9 accidentally changed the signatures of certain constructors of std::complex (Status: New)">3933</a><sup><a href="https://cplusplus.github.io/LWG/issue3933" title="Latest snapshot">(i)</a></sup> was prepared.
</p>


<p><i>[2023-06-01; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>



<p id="res-3934"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4950" title=" Working Draft, Standard for Programming Language C++">N4950</a>.
</p>

<blockquote class="note">
<p>
[<i>Drafting Note:</i> Two mutually exclusive options are prepared, depicted below by <b>Option A</b> and 
<b>Option B</b>, respectively.] 
</p>
</blockquote>

<blockquote class="note">
<p>
[<i>Drafting note</i>: The wording forms used below intentionally deviate from the rest of the [complex.member.ops]
wording forms, because it seems much simpler and clearer to follow the wording forms used that specify the effects
of <code>imag</code> and <code>real</code> functions. I decided to use "part" instead of "component", which is shorter and
more often used in the rest of the specification]
</p>
</blockquote>

<p>
<b>Option A:</b> This assumes that LWG <a href="lwg-active.html#3933" title="P1467R9 accidentally changed the signatures of certain constructors of std::complex (Status: New)">3933</a><sup><a href="https://cplusplus.github.io/LWG/issue3933" title="Latest snapshot">(i)</a></sup> is considered as NAD and just adds the missing prototype
specification assuming that the parameter style of the current working draft is intended.
</p>

<ol>

<li>
<p>Add a new prototype specification at the very beginning of 29.4.5 <a href="https://wg21.link/complex.member.ops">[complex.member.ops]</a> as indicated:</p>

<blockquote>
<pre>
<ins>constexpr complex&amp; operator=(const T&amp; rhs);</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Effects</i>: Assigns the value <code>rhs</code> to the real part and the value <code>T()</code> 
to the imaginary part of the complex value <code>*this</code>.</ins>
<p/>
<ins>-?- <i>Returns</i>: <code>*this</code>.</ins>
</p>
</blockquote>
<pre>
constexpr complex&amp; operator+=(const T&amp; rhs);
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>

</ol>

<p>
<b>Option b:</b> This assumes that LWG <a href="lwg-active.html#3933" title="P1467R9 accidentally changed the signatures of certain constructors of std::complex (Status: New)">3933</a><sup><a href="https://cplusplus.github.io/LWG/issue3933" title="Latest snapshot">(i)</a></sup> will be resolved as initially presented and just adds the missing prototype
specification assuming that the parameter style suggesting two mutually excluded overloads is intended. The wording delta is presented
against the proposed wording of LWG <a href="lwg-active.html#3933" title="P1467R9 accidentally changed the signatures of certain constructors of std::complex (Status: New)">3933</a><sup><a href="https://cplusplus.github.io/LWG/issue3933" title="Latest snapshot">(i)</a></sup>.
</p>

<ol>

<li>
<p>Add a new prototype specification at the very beginning of 29.4.5 <a href="https://wg21.link/complex.member.ops">[complex.member.ops]</a> as indicated:</p>

<blockquote>
<pre>
<ins>constexpr complex&amp; operator=(T rhs) requires floating_point&lt;T&gt;;
constexpr complex&amp; operator=(const T&amp; rhs) requires (!floating_point&lt;T&gt;);</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Effects</i>: Assigns the value <code>rhs</code> to the real part and the value <code>T()</code> 
to the imaginary part of the complex value <code>*this</code>.</ins>
<p/>
<ins>-?- <i>Returns</i>: <code>*this</code>.</ins>
</p>
</blockquote>
<pre>
constexpr complex&amp; operator+=(T) requires floating_point&lt;T&gt;;
constexpr complex&amp; operator+=(const T&amp;) requires (!floating_point&lt;T&gt;);
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
