This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] Implement DR 387 (Ready)


Hi,

tested x86_64-linux, committed to mainline.

Paolo.

////////////////////
2008-05-25  Paolo Carlini  <paolo.carlini@oracle.com>

	* include/std/complex (complex<>::real(_Tp), complex<>::imag(_Tp),
	complex<float>::real(float), complex<float>::imag(float),
	complex<double>::real(double), complex<double>::imag(double),
	complex<long double>::real(long double),
	complex<long double>::imag(long double)): Add per DR 387.
	(complex<>::real(), complex<>::imag(), complex<float>::real(),
	complex<float>::imag(), complex<double>::real(),
	complex<double>::imag(), complex<long double>::real(),
	complex<long double>::imag(long double)): Adjust in C++0x mode.
	(real(complex<>&), imag(complex<>&), real(const complex<>&),
	imag(const complex<>&)): Likewise.
	* testsuite/26_numerics/complex/dr387.cc: New.
	* doc/xml/manual/intro.xml: Add an entry for DR 387.
Index: doc/xml/manual/intro.xml
===================================================================
--- doc/xml/manual/intro.xml	(revision 135869)
+++ doc/xml/manual/intro.xml	(working copy)
@@ -475,19 +475,21 @@
     <varlistentry><term><ulink url="../ext/lwg-defects.html#303">303</ulink>:
         <emphasis>Bitset input operator underspecified</emphasis>
     </term>
-    <listitem><para>Basically, compare the input character to <code>is.widen(0)</code>
-        and <code>is.widen(1)</code>.
+    <listitem><para>Basically, compare the input character to 
+                    <code>is.widen(0)</code> and <code>is.widen(1)</code>.
     </para></listitem></varlistentry>
 
     <varlistentry><term><ulink url="../ext/lwg-defects.html#305">305</ulink>:
-        <emphasis>Default behavior of codecvt&lt;wchar_t, char, mbstate_t&gt;::length()</emphasis>
+        <emphasis>Default behavior of codecvt&lt;wchar_t, char, 
+                  mbstate_t&gt;::length()</emphasis>
     </term>
-    <listitem><para>Do not specify what <code>codecvt&lt;wchar_t, char, mbstate_t&gt;::do_length</code>
-        must return.
+    <listitem><para>Do not specify what <code>codecvt&lt;wchar_t, char, 
+                    mbstate_t&gt;::do_length</code> must return.
     </para></listitem></varlistentry>
 
     <varlistentry><term><ulink url="../ext/lwg-defects.html#328">328</ulink>:
-        <emphasis>Bad sprintf format modifier in money_put&lt;&gt;::do_put()</emphasis>
+        <emphasis>Bad sprintf format modifier in 
+                  money_put&lt;&gt;::do_put()</emphasis>
     </term>
     <listitem><para>Change the format string to &quot;%.0Lf&quot;.
     </para></listitem></varlistentry>
@@ -498,8 +500,18 @@
     <listitem><para>Add const overloads of <code>is_open</code>.
     </para></listitem></varlistentry>
 
+    <varlistentry><term><ulink url="../ext/lwg-active.html#387">387</ulink>:
+        <emphasis>std::complex over-encapsulated</emphasis>
+    </term>
+    <listitem><para>Add the <code>real(T)</code> and <code>imag(T)</code>
+                    members;  in C++0x mode, also adjust the existing 
+                    <code>real()</code> and <code>imag()</code> members and 
+                    free functions.
+    </para></listitem></varlistentry>
+
     <varlistentry><term><ulink url="../ext/lwg-defects.html#389">389</ulink>:
-        <emphasis>Const overload of valarray::operator[] returns by value</emphasis>
+        <emphasis>Const overload of valarray::operator[] returns 
+                  by value</emphasis>
     </term>
     <listitem><para>Change it to return a <code>const T&amp;</code>.
     </para></listitem></varlistentry>
Index: include/std/complex
===================================================================
--- include/std/complex	(revision 135872)
+++ include/std/complex	(working copy)
@@ -131,6 +131,15 @@
         complex(const complex<_Up>& __z)
 	: _M_real(__z.real()), _M_imag(__z.imag()) { }
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 387. std::complex over-encapsulated.
+      _Tp real() const
+      { return _M_real; }
+
+      _Tp imag() const
+      { return _M_imag; }
+#else
       ///  Return real part of complex number.
       _Tp& real()
       { return _M_real; }
@@ -146,6 +155,15 @@
       ///  Return imaginary part of complex number.
       const _Tp& imag() const
       { return _M_imag; }
+#endif
+
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 387. std::complex over-encapsulated.
+      void real(_Tp __val)
+      { _M_real = __val; }
+
+      void imag(_Tp __val)
+      { _M_imag = __val; }
 
       /// Assign this complex number to scalar @a t.
       complex<_Tp>& operator=(const _Tp&);
@@ -504,6 +522,17 @@
     }
 
   // Values
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+  template<typename _Tp>
+    inline _Tp
+    real(const complex<_Tp>& __z)
+    { return __z.real(); }
+    
+  template<typename _Tp>
+    inline _Tp
+    imag(const complex<_Tp>& __z)
+    { return __z.imag(); }
+#else
   template<typename _Tp>
     inline _Tp&
     real(complex<_Tp>& __z)
@@ -523,6 +552,7 @@
     inline const _Tp&
     imag(const complex<_Tp>& __z)
     { return __z.imag(); }
+#endif
 
   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
   template<typename _Tp>
@@ -993,6 +1023,15 @@
       explicit complex(const complex<double>&);
       explicit complex(const complex<long double>&);	
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 387. std::complex over-encapsulated.
+      float real() const
+      { return __real__ _M_value; }
+
+      float imag() const
+      { return __imag__ _M_value; }
+#else
       float& real()
       { return __real__ _M_value; }
 
@@ -1004,6 +1043,15 @@
 
       const float& imag() const
       { return __imag__ _M_value; }
+#endif
+
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 387. std::complex over-encapsulated.
+      void real(float __val)
+      { __real__ _M_value = __val; }
+
+      void imag(float __val)
+      { __imag__ _M_value = __val; }
 
       complex<float>&
       operator=(float __f)
@@ -1121,6 +1169,15 @@
 
       explicit complex(const complex<long double>&);	
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 387. std::complex over-encapsulated.
+      double real() const
+      { return __real__ _M_value; }
+
+      double imag() const
+      { return __imag__ _M_value; }
+#else
       double& real()
       { return __real__ _M_value; }
 
@@ -1132,6 +1189,15 @@
 
       const double& imag() const
       { return __imag__ _M_value; }
+#endif
+
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 387. std::complex over-encapsulated.
+      void real(double __val)
+      { __real__ _M_value = __val; }
+
+      void imag(double __val)
+      { __imag__ _M_value = __val; }
 
       complex<double>&
       operator=(double __d)
@@ -1249,6 +1315,15 @@
       complex(const complex<double>& __z)
       : _M_value(__z.__rep()) { }
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 387. std::complex over-encapsulated.
+      long double real() const
+      { return __real__ _M_value; }
+
+      long double imag() const
+      { return __imag__ _M_value; }
+#else
       long double& real()
       { return __real__ _M_value; }
 
@@ -1260,6 +1335,15 @@
 
       const long double& imag() const
       { return __imag__ _M_value; }
+#endif
+
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // DR 387. std::complex over-encapsulated.
+      void real(long double __val)
+      { __real__ _M_value = __val; }
+
+      void imag(long double __val)
+      { __imag__ _M_value = __val; }
 
       complex<long double>&
       operator=(long double __r)
Index: testsuite/26_numerics/complex/dr387.cc
===================================================================
--- testsuite/26_numerics/complex/dr387.cc	(revision 0)
+++ testsuite/26_numerics/complex/dr387.cc	(revision 0)
@@ -0,0 +1,52 @@
+// 2008-05-22  Paolo Carlini  <paolo.carlini@oracle.com>
+//
+// Copyright (C) 2008 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+#include <complex>
+#include <testsuite_hooks.h>
+
+// DR 387. std::complex over-encapsulated.
+template<typename T>
+  void
+  do_test()
+  {
+    bool test __attribute__((unused)) = true;
+
+    const T r = 1.0;
+    const T i = -1.0;
+    const T v = 0.0;
+
+    std::complex<T> z1(r, i);
+    z1.real(v);
+    VERIFY( z1.real() == v );
+    VERIFY( z1.imag() == i );
+
+    std::complex<T> z2(r, i);
+    z2.imag(v);
+    VERIFY( z2.real() == r );
+    VERIFY( z2.imag() == v );
+  }
+
+int main()
+{
+  do_test<float>();
+  do_test<double>();
+  do_test<long double>();
+  return 0;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]