<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 387: std::complex over-encapsulated</title>
<meta property="og:title" content="Issue 387: std::complex over-encapsulated">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue387.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#CD1">CD1</a> status.</em></p>
<h3 id="387"><a href="lwg-defects.html#387">387</a>. <code>std::complex</code> over-encapsulated</h3>
<p><b>Section:</b> 29.4 <a href="https://wg21.link/complex.numbers">[complex.numbers]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Gabriel Dos Reis <b>Opened:</b> 2002-11-08 <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#complex.numbers">issues</a> in [complex.numbers].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The absence of explicit description of <code>std::complex&lt;T&gt;</code> layout
makes it imposible to reuse existing software developed in traditional
languages like Fortran or C with unambigous and commonly accepted
layout assumptions.  There ought to be a way for practitioners to
predict with confidence the layout of <code>std::complex&lt;T&gt;</code> whenever <code>T</code>
is a numerical datatype.  The absence of ways to access individual
parts of a <code>std::complex&lt;T&gt;</code> object as lvalues unduly promotes
severe pessimizations. For example, the only way to change,
independently, the real and imaginary parts is to write something like
</p>

<pre>
complex&lt;T&gt; z;
// ...
// set the real part to r
z = complex&lt;T&gt;(r, z.imag());
// ...
// set the imaginary part to i
z = complex&lt;T&gt;(z.real(), i);
</pre>

<p>
At this point, it seems appropriate to recall that a complex number
is, in effect, just a pair of numbers with no particular invariant to
maintain.  Existing practice in numerical computations has it that a
complex number datatype is usually represented by Cartesian
coordinates. Therefore the over-encapsulation put in the specification
of <code>std::complex&lt;&gt;</code> is not justified.
</p>



<p id="res-387"><b>Proposed resolution:</b></p>
<p>Add the following requirements to 29.4 <a href="https://wg21.link/complex.numbers">[complex.numbers]</a> as 26.3/4:</p>
<blockquote>
<p>If <code>z</code> is an lvalue expression of type <i>cv</i> <code>std::complex&lt;T&gt;</code> then</p>

<ul>
<li>the expression <code>reinterpret_cast&lt;cv T(&amp;)[2]&gt;(z)</code>
is well-formed; and</li>
<li><code>reinterpret_cast&lt;cv T(&amp;)[2]&gt;(z)[0]</code> designates the
real part of <code>z</code>; and</li>
<li><code>reinterpret_cast&lt;cv T(&amp;)[2]&gt;(z)[1]</code> designates the
imaginary part of <code>z</code>.</li>
</ul>

<p>
Moreover, if <code>a</code> is an expression of pointer type <i>cv</i> <code>complex&lt;T&gt;*</code>
and the expression <code>a[i]</code> is well-defined for an integer expression
<code>i</code> then:
</p>

<ul>
<li><code>reinterpret_cast&lt;cv T*&gt;(a)[2*i]</code> designates the real
part of <code>a[i]</code>; and</li>
<li><code>reinterpret_cast&lt;cv T*&gt;(a)[2*i+1]</code> designates the
imaginary part of <code>a[i]</code>.</li>
</ul>
</blockquote>

<p>
In 29.4.3 <a href="https://wg21.link/complex">[complex]</a> and  [complex.special] add the following member functions
(changing <code>T</code> to concrete types as appropriate for the specializations).
</p>

<blockquote><pre>
void real(T);
void imag(T);
</pre></blockquote>

<p>
Add to 29.4.4 <a href="https://wg21.link/complex.members">[complex.members]</a>
</p>

<blockquote>
<pre>
T real() const;
</pre>
<blockquote><p>
<i>Returns:</i> the value of the real component
</p></blockquote>
<pre>
void real(T val);
</pre>
<blockquote><p>
Assigns <code>val</code> to the real component.
</p></blockquote>
<pre>
T imag() const;
</pre>
<blockquote><p>
<i>Returns:</i> the value of the imaginary component
</p></blockquote>
<pre>
void imag(T val);
</pre>
<blockquote><p>
Assigns <code>val</code> to the imaginary component.
</p></blockquote>
</blockquote>

<p><i>[Kona: The layout guarantee is absolutely necessary for C
  compatibility.  However, there was disagreement about the other part
  of this proposal: retrieving elements of the complex number as
  lvalues.  An alternative: continue to have <code>real()</code> and <code>imag()</code> return
  rvalues, but add <code>set_real()</code> and <code>set_imag()</code>.  Straw poll: return
  lvalues - 2, add setter functions - 5.  Related issue: do we want
  reinterpret_cast as the interface for converting a complex to an
  array of two reals, or do we want to provide a more explicit way of
  doing it?  Howard will try to resolve this issue for the next
  meeting.]</i></p>


<p><i>[pre-Sydney: Howard summarized the options in n1589.]</i></p>


<p><i>[
Bellevue:
]</i></p>


<blockquote><p>
Second half of proposed wording replaced and moved to Ready.
</p></blockquote>

<p><i>[
Pre-Sophia Antipolis, Howard adds:
]</i></p>


<blockquote><p>
Added the members to  [complex.special] and changed from Ready to Review.
</p></blockquote>

<p><i>[
Post-Sophia Antipolis:
]</i></p>


<blockquote><p>
Moved from WP back to Ready so that the "and  [complex.special]" in the proposed
resolution can be officially applied.
</p></blockquote>



<p><b>Rationale:</b></p>
<p>The LWG believes that C99 compatibility would be enough
justification for this change even without other considerations.  All
existing implementations already have the layout proposed here.</p>





</body>
</html>
