From 3b3bfc0eebfab8f33796490c5d0fa65cbc8c587f Mon Sep 17 00:00:00 2001 From: Gabriel Dos Reis Date: Sun, 11 Jan 2004 16:15:42 +0000 Subject: [PATCH] std_complex.h (std::complex<>::real): Return a reference. * include/std/std_complex.h (std::complex<>::real): Return a reference. Add non-const overload. (std::complex<>::real): Likewise. (std::real): Likewise. (std::imag): Likewise. (std::operator+): Tidy. (std::operator-): Likewise. (std::operator*): Likewise. (std::operator/): Likewise. (std::operator>>): Likewise. From-SVN: r75680 --- libstdc++-v3/ChangeLog | 13 ++ libstdc++-v3/include/std/std_complex.h | 171 +++++++++++++++++++------ 2 files changed, 148 insertions(+), 36 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1c425f13887..3b594cfb3bb 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,16 @@ +2004-01-11 Gabriel Dos Reis + + * include/std/std_complex.h (std::complex<>::real): Return a + reference. Add non-const overload. + (std::complex<>::real): Likewise. + (std::real): Likewise. + (std::imag): Likewise. + (std::operator+): Tidy. + (std::operator-): Likewise. + (std::operator*): Likewise. + (std::operator/): Likewise. + (std::operator>>): Likewise. + 2004-01-11 Paolo Carlini PR libstdc++/13582 diff --git a/libstdc++-v3/include/std/std_complex.h b/libstdc++-v3/include/std/std_complex.h index 4978d5a8f15..4fe80a55769 100644 --- a/libstdc++-v3/include/std/std_complex.h +++ b/libstdc++-v3/include/std/std_complex.h @@ -1,6 +1,6 @@ // The template and inlines for the -*- C++ -*- complex number classes. -// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 +// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 // Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free @@ -96,9 +96,11 @@ namespace std // complex (const complex<_Tp>&); template complex(const complex<_Up>&); - - _Tp real() const; - _Tp imag() const; + + _Tp& real(); + const _Tp& real() const; + _Tp& imag(); + const _Tp& imag() const; complex<_Tp>& operator=(const _Tp&); complex<_Tp>& operator+=(const _Tp&); @@ -121,15 +123,24 @@ namespace std complex<_Tp>& operator/=(const complex<_Up>&); private: - _Tp _M_real, _M_imag; + _Tp _M_real; + _Tp _M_imag; }; template - inline _Tp + inline _Tp& + complex<_Tp>::real() { return _M_real; } + + template + inline const _Tp& complex<_Tp>::real() const { return _M_real; } template - inline _Tp + inline _Tp& + complex<_Tp>::imag() { return _M_imag; } + + template + inline const _Tp& complex<_Tp>::imag() const { return _M_imag; } template @@ -253,62 +264,110 @@ namespace std template inline complex<_Tp> operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) += __y; } + { + complex<_Tp> __r = __x; + __r += __y; + return __r; + } template inline complex<_Tp> operator+(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) += __y; } + { + complex<_Tp> __r = __x; + __r.real() += __y; + return __r; + } template inline complex<_Tp> operator+(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__y) += __x; } + { + complex<_Tp> __r = __y; + __r.real() += __x; + return __r; + } template inline complex<_Tp> operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) -= __y; } + { + complex<_Tp> __r = __x; + __r -= __y; + return __r; + } template inline complex<_Tp> operator-(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) -= __y; } + { + complex<_Tp> __r = __x; + __r.real() -= __y; + return __r; + } template inline complex<_Tp> operator-(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) -= __y; } + { + complex<_Tp> __r(__x, -__y.imag()); + __r.real() -= __y.real(); + return __r; + } template inline complex<_Tp> operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) *= __y; } + { + complex<_Tp> __r = __x; + __r *= __y; + return __r; + } template inline complex<_Tp> operator*(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) *= __y; } + { + complex<_Tp> __r = __x; + __r *= __y; + return __r; + } template inline complex<_Tp> operator*(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__y) *= __x; } + { + complex<_Tp> __r = __y; + __r *= __x; + return __r; + } template inline complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) /= __y; } + { + complex<_Tp> __r = __x; + __r /= __y; + return __r; + } template inline complex<_Tp> operator/(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) /= __y; } + { + complex<_Tp> __r = __x; + __r /= __y; + return __r; + } template inline complex<_Tp> operator/(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) /= __y; } + { + complex<_Tp> __r = __x; + __r /= __y; + return __r; + } template inline complex<_Tp> @@ -369,7 +428,7 @@ namespace std __is.setstate(ios_base::failbit); } else if (__ch == ')') - __x = complex<_Tp>(__re_x, _Tp(0)); + __x = __re_x; else __is.setstate(ios_base::failbit); } @@ -377,7 +436,7 @@ namespace std { __is.putback(__ch); __is >> __re_x; - __x = complex<_Tp>(__re_x, _Tp(0)); + __x = __re_x; } return __is; } @@ -396,12 +455,22 @@ namespace std // Values template - inline _Tp + inline _Tp& + real(complex<_Tp>& __z) + { return __z.real(); } + + template + inline const _Tp& real(const complex<_Tp>& __z) { return __z.real(); } template - inline _Tp + inline _Tp& + imag(complex<_Tp>& __z) + { return __z.imag(); } + + template + inline const _Tp& imag(const complex<_Tp>& __z) { return __z.imag(); } @@ -605,8 +674,10 @@ namespace std explicit complex(const complex&); explicit complex(const complex&); - float real() const; - float imag() const; + float& real(); + const float& real() const; + float& imag(); + const float& imag() const; complex& operator=(float); complex& operator+=(float); @@ -638,11 +709,19 @@ namespace std friend class complex; }; - inline float + inline float& + complex::real() + { return __real__ _M_value; } + + inline const float& complex::real() const { return __real__ _M_value; } - inline float + inline float& + complex::imag() + { return __imag__ _M_value; } + + inline const float& complex::imag() const { return __imag__ _M_value; } @@ -751,9 +830,11 @@ namespace std #endif complex(const complex&); explicit complex(const complex&); - - double real() const; - double imag() const; + + double& real(); + const double& real() const; + double& imag(); + const double& imag() const; complex& operator=(double); complex& operator+=(double); @@ -784,11 +865,19 @@ namespace std friend class complex; }; - inline double + inline double& + complex::real() + { return __real__ _M_value; } + + inline const double& complex::real() const { return __real__ _M_value; } - inline double + inline double& + complex::imag() + { return __imag__ _M_value; } + + inline const double& complex::imag() const { return __imag__ _M_value; } @@ -898,8 +987,10 @@ namespace std complex(const complex&); complex(const complex&); - long double real() const; - long double imag() const; + long double& real(); + const long double& real() const; + long double& imag(); + const long double& imag() const; complex& operator= (long double); complex& operator+= (long double); @@ -937,11 +1028,19 @@ namespace std __imag__ _M_value = __i; } - inline long double + inline long double& + complex::real() + { return __real__ _M_value; } + + inline const long double& complex::real() const { return __real__ _M_value; } - inline long double + inline long double& + complex::imag() + { return __imag__ _M_value; } + + inline const long double& complex::imag() const { return __imag__ _M_value; }