PR libstdc++/59568 fix error handling for std::complex stream extraction
[gcc.git] / libstdc++-v3 / include / std / complex
index 4c0d3fbe7db1c1aa74b26384322f115e60a92405..bfe10347bd3788b12e701fc68b3a6216a0cc7925 100644 (file)
@@ -1,8 +1,6 @@
 // The template and inlines for the -*- C++ -*- complex number classes.
 
-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-// 2006, 2007, 2008, 2009
-// Free Software Foundation, Inc.
+// Copyright (C) 1997-2017 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
 #include <cmath>
 #include <sstream>
 
-_GLIBCXX_BEGIN_NAMESPACE(std)
+// Get rid of a macro possibly defined in <complex.h>
+#undef complex
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   /**
    * @defgroup complex_numbers Complex Numbers
@@ -85,15 +88,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
   /// Return complex base 10 logarithm of @a z.
   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
-#ifndef __GXX_EXPERIMENTAL_CXX0X__
-  // DR 844.
   /// Return @a x to the @a y'th power.
   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
-#endif
   /// Return @a x to the @a y'th power.
   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
   /// Return @a x to the @a y'th power.
-  template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 
+  template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
                                           const complex<_Tp>&);
   /// Return @a x to the @a y'th power.
   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
@@ -107,8 +107,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
   /// Return complex hyperbolic tangent of @a z.
   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
-    
-    
+
+
   // 26.2.2  Primary template class complex
   /**
    *  Template to represent complex numbers.
@@ -123,57 +123,62 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     {
       /// Value typedef.
       typedef _Tp value_type;
-      
+
       ///  Default constructor.  First parameter is x, second parameter is y.
       ///  Unspecified parameters default to 0.
-      complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
+      _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
       : _M_real(__r), _M_imag(__i) { }
 
-      // Lets the compiler synthesize the copy constructor   
-      // complex (const complex<_Tp>&);
-      ///  Copy constructor.
+      // Let the compiler synthesize the copy constructor
+#if __cplusplus >= 201103L
+      constexpr complex(const complex&) = default;
+#endif
+
+      ///  Converting constructor.
       template<typename _Up>
-        complex(const complex<_Up>& __z)
+        _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
        : _M_real(__z.real()), _M_imag(__z.imag()) { }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // DR 387. std::complex over-encapsulated.
-      _Tp real() const
-      { return _M_real; }
+      _GLIBCXX_ABI_TAG_CXX11
+      constexpr _Tp
+      real() const { return _M_real; }
 
-      _Tp imag() const
-      { return _M_imag; }
+      _GLIBCXX_ABI_TAG_CXX11
+      constexpr _Tp
+      imag() const { return _M_imag; }
 #else
       ///  Return real part of complex number.
-      _Tp& real()
-      { return _M_real; }
+      _Tp&
+      real() { return _M_real; }
 
       ///  Return real part of complex number.
-      const _Tp& real() const
-      { return _M_real; }
+      const _Tp&
+      real() const { return _M_real; }
 
       ///  Return imaginary part of complex number.
-      _Tp& imag()
-      { return _M_imag; }
+      _Tp&
+      imag() { return _M_imag; }
 
       ///  Return imaginary part of complex number.
-      const _Tp& imag() const
-      { return _M_imag; }
+      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
+      real(_Tp __val) { _M_real = __val; }
 
-      void imag(_Tp __val)
-      { _M_imag = __val; }
+      void
+      imag(_Tp __val) { _M_imag = __val; }
 
-      /// Assign this complex number to scalar @a t.
+      /// Assign a scalar to this complex number.
       complex<_Tp>& operator=(const _Tp&);
-      
-      /// Add @a t to this complex number.
+
+      /// Add a scalar to this complex number.
       // 26.2.5/1
       complex<_Tp>&
       operator+=(const _Tp& __t)
@@ -182,7 +187,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        return *this;
       }
 
-      /// Subtract @a t from this complex number.
+      /// Subtract a scalar from this complex number.
       // 26.2.5/3
       complex<_Tp>&
       operator-=(const _Tp& __t)
@@ -191,31 +196,33 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        return *this;
       }
 
-      /// Multiply this complex number by @a t.
+      /// Multiply this complex number by a scalar.
       complex<_Tp>& operator*=(const _Tp&);
-      /// Divide this complex number by @a t.
+      /// Divide this complex number by a scalar.
       complex<_Tp>& operator/=(const _Tp&);
 
-      // Lets the compiler synthesize the
-      // copy and assignment operator
-      // complex<_Tp>& operator= (const complex<_Tp>&);
-      /// Assign this complex number to complex @a z.
+      // Let the compiler synthesize the copy assignment operator
+#if __cplusplus >= 201103L
+      complex& operator=(const complex&) = default;
+#endif
+
+      /// Assign another complex number to this one.
       template<typename _Up>
         complex<_Tp>& operator=(const complex<_Up>&);
-      /// Add @a z to this complex number.
+      /// Add another complex number to this one.
       template<typename _Up>
         complex<_Tp>& operator+=(const complex<_Up>&);
-      /// Subtract @a z from this complex number.
+      /// Subtract another complex number from this one.
       template<typename _Up>
         complex<_Tp>& operator-=(const complex<_Up>&);
-      /// Multiply this complex number by @a z.
+      /// Multiply this complex number by another.
       template<typename _Up>
         complex<_Tp>& operator*=(const complex<_Up>&);
-      /// Divide this complex number by @a z.
+      /// Divide this complex number by another.
       template<typename _Up>
         complex<_Tp>& operator/=(const complex<_Up>&);
 
-      const complex& __rep() const
+      _GLIBCXX_CONSTEXPR complex __rep() const
       { return *this; }
 
     private:
@@ -230,7 +237,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
      _M_real = __t;
      _M_imag = _Tp();
      return *this;
-    } 
+    }
 
   // 26.2.5/5
   template<typename _Tp>
@@ -310,7 +317,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       _M_real = __r / __n;
       return *this;
     }
-    
+
   // Operators:
   //@{
   ///  Return new complex value @a x plus @a y.
@@ -352,7 +359,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       __r -= __y;
       return __r;
     }
-    
+
   template<typename _Tp>
     inline complex<_Tp>
     operator-(const complex<_Tp>& __x, const _Tp& __y)
@@ -412,7 +419,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       __r /= __y;
       return __r;
     }
-    
+
   template<typename _Tp>
     inline complex<_Tp>
     operator/(const complex<_Tp>& __x, const _Tp& __y)
@@ -447,17 +454,17 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   //@{
   ///  Return true if @a x is equal to @a y.
   template<typename _Tp>
-    inline bool
+    inline _GLIBCXX_CONSTEXPR bool
     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
 
   template<typename _Tp>
-    inline bool
+    inline _GLIBCXX_CONSTEXPR bool
     operator==(const complex<_Tp>& __x, const _Tp& __y)
     { return __x.real() == __y && __x.imag() == _Tp(); }
 
   template<typename _Tp>
-    inline bool
+    inline _GLIBCXX_CONSTEXPR bool
     operator==(const _Tp& __x, const complex<_Tp>& __y)
     { return __x == __y.real() && _Tp() == __y.imag(); }
   //@}
@@ -465,17 +472,17 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   //@{
   ///  Return false if @a x is equal to @a y.
   template<typename _Tp>
-    inline bool
+    inline _GLIBCXX_CONSTEXPR bool
     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
 
   template<typename _Tp>
-    inline bool
+    inline _GLIBCXX_CONSTEXPR bool
     operator!=(const complex<_Tp>& __x, const _Tp& __y)
     { return __x.real() != __y || __x.imag() != _Tp(); }
 
   template<typename _Tp>
-    inline bool
+    inline _GLIBCXX_CONSTEXPR bool
     operator!=(const _Tp& __x, const complex<_Tp>& __y)
     { return __x != __y.real() || _Tp() != __y.imag(); }
   //@}
@@ -485,31 +492,52 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     basic_istream<_CharT, _Traits>&
     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
     {
-      _Tp __re_x, __im_x;
+      bool __fail = true;
       _CharT __ch;
-      __is >> __ch;
-      if (__ch == '(') 
+      if (__is >> __ch)
        {
-         __is >> __re_x >> __ch;
-         if (__ch == ',') 
+         if (_Traits::eq(__ch, __is.widen('(')))
            {
-             __is >> __im_x >> __ch;
-             if (__ch == ')') 
-               __x = complex<_Tp>(__re_x, __im_x);
-             else
-               __is.setstate(ios_base::failbit);
+             _Tp __u;
+             if (__is >> __u >> __ch)
+               {
+                 const _CharT __rparen = __is.widen(')');
+                 if (_Traits::eq(__ch, __rparen))
+                   {
+                     __x = __u;
+                     __fail = false;
+                   }
+                 else if (_Traits::eq(__ch, __is.widen(',')))
+                   {
+                     _Tp __v;
+                     if (__is >> __v >> __ch)
+                       {
+                         if (_Traits::eq(__ch, __rparen))
+                           {
+                             __x = complex<_Tp>(__u, __v);
+                             __fail = false;
+                           }
+                         else
+                           __is.putback(__ch);
+                       }
+                   }
+                 else
+                   __is.putback(__ch);
+               }
            }
-         else if (__ch == ')') 
-           __x = __re_x;
          else
-           __is.setstate(ios_base::failbit);
-       }
-      else 
-       {
-         __is.putback(__ch);
-         __is >> __re_x;
-         __x = __re_x;
+           {
+             __is.putback(__ch);
+             _Tp __u;
+             if (__is >> __u)
+               {
+                 __x = __u;
+                 __fail = false;
+               }
+           }
        }
+      if (__fail)
+       __is.setstate(ios_base::failbit);
       return __is;
     }
 
@@ -527,14 +555,14 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     }
 
   // Values
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template<typename _Tp>
-    inline _Tp
+    constexpr _Tp
     real(const complex<_Tp>& __z)
     { return __z.real(); }
-    
+
   template<typename _Tp>
-    inline _Tp
+    constexpr _Tp
     imag(const complex<_Tp>& __z)
     { return __z.imag(); }
 #else
@@ -542,17 +570,17 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     inline _Tp&
     real(complex<_Tp>& __z)
     { return __z.real(); }
-    
+
   template<typename _Tp>
     inline const _Tp&
     real(const complex<_Tp>& __z)
     { return __z.real(); }
-    
+
   template<typename _Tp>
     inline _Tp&
     imag(complex<_Tp>& __z)
     { return __z.imag(); }
-    
+
   template<typename _Tp>
     inline const _Tp&
     imag(const complex<_Tp>& __z)
@@ -569,7 +597,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       const _Tp __s = std::max(abs(__x), abs(__y));
       if (__s == _Tp())  // well ...
         return __s;
-      __x /= __s; 
+      __x /= __s;
       __y /= __s;
       return __s * sqrt(__x * __x + __y * __y);
     }
@@ -592,7 +620,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   template<typename _Tp>
     inline _Tp
     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
-#endif  
+#endif
 
 
   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
@@ -623,7 +651,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
   //     As defined, norm() is -not- a norm is the common mathematical
-  //     sens used in numerics.  The helper class _Norm_helper<> tries to
+  //     sense used in numerics.  The helper class _Norm_helper<> tries to
   //     distinguish between builtin floating point and the rest, so as
   //     to deliver an answer as close as possible to the real value.
   template<bool>
@@ -648,25 +676,28 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
           return __res * __res;
         }
     };
-  
+
   template<typename _Tp>
     inline _Tp
     norm(const complex<_Tp>& __z)
     {
-      return _Norm_helper<__is_floating<_Tp>::__value 
+      return _Norm_helper<__is_floating<_Tp>::__value
        && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
     }
 
   template<typename _Tp>
     inline complex<_Tp>
     polar(const _Tp& __rho, const _Tp& __theta)
-    { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
+    {
+      __glibcxx_assert( __rho >= 0 );
+      return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
+    }
 
   template<typename _Tp>
     inline complex<_Tp>
     conj(const complex<_Tp>& __z)
     { return complex<_Tp>(__z.real(), -__z.imag()); }
-  
+
   // Transcendentals
 
   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
@@ -733,7 +764,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   template<typename _Tp>
     inline complex<_Tp>
     __complex_exp(const complex<_Tp>& __z)
-    { return std::polar(exp(__z.real()), __z.imag()); }
+    { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
 
 #if _GLIBCXX_USE_C99_COMPLEX
   inline __complex__ float
@@ -794,7 +825,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     {
       const _Tp __x = __z.real();
       const _Tp __y = __z.imag();
-      return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 
+      return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
     }
 
 #if _GLIBCXX_USE_C99_COMPLEX
@@ -829,14 +860,14 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
 #if _GLIBCXX_USE_C99_COMPLEX
   inline __complex__ float
-  __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }      
+  __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
 
   inline __complex__ double
-  __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }      
+  __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
 
   inline __complex__ long double
   __complex_sinh(const __complex__ long double& __z)
-  { return __builtin_csinhl(__z); }      
+  { return __builtin_csinhl(__z); }
 
   template<typename _Tp>
     inline complex<_Tp>
@@ -892,7 +923,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 #endif
 
   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
-  
+
   template<typename _Tp>
     inline complex<_Tp>
     __complex_tan(const complex<_Tp>& __z)
@@ -920,7 +951,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
 
   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
-  
+
   template<typename _Tp>
     inline complex<_Tp>
     __complex_tanh(const complex<_Tp>& __z)
@@ -950,20 +981,42 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
   //                          raised to the __y-th power.  The branch
   //                          cut is on the negative axis.
-#ifndef __GXX_EXPERIMENTAL_CXX0X__
-  // _GLIBCXX_RESOLVE_LIB_DEFECTS
+  template<typename _Tp>
+    complex<_Tp>
+    __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
+    {
+      complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
+
+      while (__n >>= 1)
+        {
+          __x *= __x;
+          if (__n % 2)
+            __y *= __x;
+        }
+
+      return __y;
+    }
+
+  // In C++11 mode we used to implement the resolution of
   // DR 844. complex pow return type is ambiguous.
+  // thus the following overload was disabled in that mode.  However, doing
+  // that causes all sorts of issues, see, for example:
+  //   http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
+  // and also PR57974.
   template<typename _Tp>
     inline complex<_Tp>
     pow(const complex<_Tp>& __z, int __n)
-    { return std::__pow_helper(__z, __n); }
-#endif
+    {
+      return __n < 0
+       ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
+        : std::__complex_pow_unsigned(__z, __n);
+    }
 
   template<typename _Tp>
     complex<_Tp>
     pow(const complex<_Tp>& __x, const _Tp& __y)
     {
-#ifndef _GLIBCXX_USE_C99_COMPLEX
+#if ! _GLIBCXX_USE_C99_COMPLEX
       if (__x == _Tp())
        return _Tp();
 #endif
@@ -971,7 +1024,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
         return pow(__x.real(), __y);
 
       complex<_Tp> __t = std::log(__x);
-      return std::polar(exp(__y * __t.real()), __y * __t.imag());
+      return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
     }
 
   template<typename _Tp>
@@ -1008,90 +1061,95 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     inline complex<_Tp>
     pow(const _Tp& __x, const complex<_Tp>& __y)
     {
-      return __x > _Tp() ? std::polar(pow(__x, __y.real()),
-                                     __y.imag() * log(__x))
+      return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
+                                          __y.imag() * log(__x))
                         : std::pow(complex<_Tp>(__x), __y);
     }
 
-  // 26.2.3  complex specializations
-  // complex<float> specialization
+  /// 26.2.3  complex specializations
+  /// complex<float> specialization
   template<>
     struct complex<float>
     {
       typedef float value_type;
       typedef __complex__ float _ComplexT;
 
-      complex(_ComplexT __z) : _M_value(__z) { }
+      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
 
-      complex(float __r = 0.0f, float __i = 0.0f)
+      _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
+#if __cplusplus >= 201103L
+      : _M_value{ __r, __i } { }
+#else
       {
        __real__ _M_value = __r;
        __imag__ _M_value = __i;
       }
+#endif
 
-      explicit complex(const complex<double>&);
-      explicit complex(const complex<long double>&);   
+      explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
+      explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // DR 387. std::complex over-encapsulated.
-      float real() const
-      { return __real__ _M_value; }
+      __attribute ((__abi_tag__ ("cxx11")))
+      constexpr float
+      real() const { return __real__ _M_value; }
 
-      float imag() const
-      { return __imag__ _M_value; }
+      __attribute ((__abi_tag__ ("cxx11")))
+      constexpr float
+      imag() const { return __imag__ _M_value; }
 #else
-      float& real()
-      { return __real__ _M_value; }
+      float&
+      real() { return __real__ _M_value; }
 
-      const float& real() const
-      { return __real__ _M_value; }      
+      const float&
+      real() const { return __real__ _M_value; }
 
-      float& imag()
-      { return __imag__ _M_value; }
+      float&
+      imag() { return __imag__ _M_value; }
 
-      const float& imag() const
-      { return __imag__ _M_value; }
+      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
+      real(float __val) { __real__ _M_value = __val; }
 
-      void imag(float __val)
-      { __imag__ _M_value = __val; }
+      void
+      imag(float __val) { __imag__ _M_value = __val; }
 
-      complex<float>&
+      complex&
       operator=(float __f)
       {
-       __real__ _M_value = __f;
-       __imag__ _M_value = 0.0f;
+       _M_value = __f;
        return *this;
       }
 
-      complex<float>&
+      complex&
       operator+=(float __f)
       {
-       __real__ _M_value += __f;
+       _M_value += __f;
        return *this;
       }
 
-      complex<float>&
+      complex&
       operator-=(float __f)
       {
-       __real__ _M_value -= __f;
+       _M_value -= __f;
        return *this;
       }
 
-      complex<float>&
+      complex&
       operator*=(float __f)
       {
        _M_value *= __f;
        return *this;
       }
 
-      complex<float>&
+      complex&
       operator/=(float __f)
       {
        _M_value /= __f;
@@ -1103,7 +1161,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       // complex& operator=(const complex&);
 
       template<typename _Tp>
-        complex<float>&
+        complex&
         operator=(const complex<_Tp>&  __z)
        {
          __real__ _M_value = __z.real();
@@ -1112,7 +1170,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<float>&
+        complex&
         operator+=(const complex<_Tp>& __z)
        {
          __real__ _M_value += __z.real();
@@ -1121,7 +1179,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<class _Tp>
-        complex<float>&
+        complex&
         operator-=(const complex<_Tp>& __z)
        {
          __real__ _M_value -= __z.real();
@@ -1130,7 +1188,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<class _Tp>
-        complex<float>&
+        complex&
         operator*=(const complex<_Tp>& __z)
        {
          _ComplexT __t;
@@ -1141,7 +1199,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<class _Tp>
-        complex<float>&
+        complex&
         operator/=(const complex<_Tp>& __z)
        {
          _ComplexT __t;
@@ -1151,93 +1209,98 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
          return *this;
        }
 
-      const _ComplexT& __rep() const { return _M_value; }
+      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
 
     private:
       _ComplexT _M_value;
     };
 
-  // 26.2.3  complex specializations
-  // complex<double> specialization
+  /// 26.2.3  complex specializations
+  /// complex<double> specialization
   template<>
     struct complex<double>
     {
       typedef double value_type;
       typedef __complex__ double _ComplexT;
 
-      complex(_ComplexT __z) : _M_value(__z) { }
+      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
 
-      complex(double __r = 0.0, double __i = 0.0)
+      _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
+#if __cplusplus >= 201103L
+      : _M_value{ __r, __i } { }
+#else
       {
        __real__ _M_value = __r;
        __imag__ _M_value = __i;
       }
+#endif
 
-      complex(const complex<float>& __z)
+      _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
       : _M_value(__z.__rep()) { }
 
-      explicit complex(const complex<long double>&);   
+      explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // DR 387. std::complex over-encapsulated.
-      double real() const
-      { return __real__ _M_value; }
+      __attribute ((__abi_tag__ ("cxx11")))
+      constexpr double
+      real() const { return __real__ _M_value; }
 
-      double imag() const
-      { return __imag__ _M_value; }
+      __attribute ((__abi_tag__ ("cxx11")))
+      constexpr double
+      imag() const { return __imag__ _M_value; }
 #else
-      double& real()
-      { return __real__ _M_value; }
+      double&
+      real() { return __real__ _M_value; }
 
-      const double& real() const
-      { return __real__ _M_value; }
+      const double&
+      real() const { return __real__ _M_value; }
 
-      double& imag()
-      { return __imag__ _M_value; }
+      double&
+      imag() { return __imag__ _M_value; }
 
-      const double& imag() const
-      { return __imag__ _M_value; }
+      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
+      real(double __val) { __real__ _M_value = __val; }
 
-      void imag(double __val)
-      { __imag__ _M_value = __val; }
+      void
+      imag(double __val) { __imag__ _M_value = __val; }
 
-      complex<double>&
+      complex&
       operator=(double __d)
       {
-       __real__ _M_value = __d;
-       __imag__ _M_value = 0.0;
+       _M_value = __d;
        return *this;
       }
 
-      complex<double>&
+      complex&
       operator+=(double __d)
       {
-       __real__ _M_value += __d;
+       _M_value += __d;
        return *this;
       }
-       
-      complex<double>&
+
+      complex&
       operator-=(double __d)
       {
-       __real__ _M_value -= __d;
+       _M_value -= __d;
        return *this;
       }
 
-      complex<double>&
+      complex&
       operator*=(double __d)
       {
        _M_value *= __d;
        return *this;
       }
 
-      complex<double>&
+      complex&
       operator/=(double __d)
       {
        _M_value /= __d;
@@ -1248,7 +1311,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       // complex& operator=(const complex&);
 
       template<typename _Tp>
-        complex<double>&
+        complex&
         operator=(const complex<_Tp>& __z)
        {
          __real__ _M_value = __z.real();
@@ -1257,7 +1320,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<double>&
+        complex&
         operator+=(const complex<_Tp>& __z)
        {
          __real__ _M_value += __z.real();
@@ -1266,7 +1329,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<double>&
+        complex&
         operator-=(const complex<_Tp>& __z)
        {
          __real__ _M_value -= __z.real();
@@ -1275,7 +1338,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<double>&
+        complex&
         operator*=(const complex<_Tp>& __z)
        {
          _ComplexT __t;
@@ -1286,7 +1349,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<double>&
+        complex&
         operator/=(const complex<_Tp>& __z)
        {
          _ComplexT __t;
@@ -1296,94 +1359,100 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
          return *this;
        }
 
-      const _ComplexT& __rep() const { return _M_value; }
+      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
 
     private:
       _ComplexT _M_value;
     };
 
-  // 26.2.3  complex specializations
-  // complex<long double> specialization
+  /// 26.2.3  complex specializations
+  /// complex<long double> specialization
   template<>
     struct complex<long double>
     {
       typedef long double value_type;
       typedef __complex__ long double _ComplexT;
 
-      complex(_ComplexT __z) : _M_value(__z) { }
+      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
 
-      complex(long double __r = 0.0L, long double __i = 0.0L)
+      _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
+                                long double __i = 0.0L)
+#if __cplusplus >= 201103L
+      : _M_value{ __r, __i } { }
+#else
       {
        __real__ _M_value = __r;
        __imag__ _M_value = __i;
       }
+#endif
 
-      complex(const complex<float>& __z)
+      _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
       : _M_value(__z.__rep()) { }
 
-      complex(const complex<double>& __z)
+      _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
       : _M_value(__z.__rep()) { }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // DR 387. std::complex over-encapsulated.
-      long double real() const
-      { return __real__ _M_value; }
+      __attribute ((__abi_tag__ ("cxx11")))
+      constexpr long double
+      real() const { return __real__ _M_value; }
 
-      long double imag() const
-      { return __imag__ _M_value; }
+      __attribute ((__abi_tag__ ("cxx11")))
+      constexpr long double
+      imag() const { return __imag__ _M_value; }
 #else
-      long double& real()
-      { return __real__ _M_value; }
+      long double&
+      real() { return __real__ _M_value; }
 
-      const long double& real() const
-      { return __real__ _M_value; }
+      const long double&
+      real() const { return __real__ _M_value; }
 
-      long double& imag()
-      { return __imag__ _M_value; }
+      long double&
+      imag() { return __imag__ _M_value; }
 
-      const long double& imag() const
-      { return __imag__ _M_value; }
+      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
+      real(long double __val) { __real__ _M_value = __val; }
 
-      void imag(long double __val)
-      { __imag__ _M_value = __val; }
+      void
+      imag(long double __val) { __imag__ _M_value = __val; }
 
-      complex<long double>&
+      complex&
       operator=(long double __r)
       {
-       __real__ _M_value = __r;
-       __imag__ _M_value = 0.0L;
+       _M_value = __r;
        return *this;
       }
 
-      complex<long double>&
+      complex&
       operator+=(long double __r)
       {
-       __real__ _M_value += __r;
+       _M_value += __r;
        return *this;
       }
 
-      complex<long double>&
+      complex&
       operator-=(long double __r)
       {
-       __real__ _M_value -= __r;
+       _M_value -= __r;
        return *this;
       }
 
-      complex<long double>&
+      complex&
       operator*=(long double __r)
       {
        _M_value *= __r;
        return *this;
       }
 
-      complex<long double>&
+      complex&
       operator/=(long double __r)
       {
        _M_value /= __r;
@@ -1394,7 +1463,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       // complex& operator=(const complex&);
 
       template<typename _Tp>
-        complex<long double>&
+        complex&
         operator=(const complex<_Tp>& __z)
        {
          __real__ _M_value = __z.real();
@@ -1403,7 +1472,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<long double>&
+        complex&
        operator+=(const complex<_Tp>& __z)
        {
          __real__ _M_value += __z.real();
@@ -1412,7 +1481,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<long double>&
+        complex&
        operator-=(const complex<_Tp>& __z)
        {
          __real__ _M_value -= __z.real();
@@ -1421,7 +1490,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<long double>&
+        complex&
        operator*=(const complex<_Tp>& __z)
        {
          _ComplexT __t;
@@ -1432,7 +1501,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 
       template<typename _Tp>
-        complex<long double>&
+        complex&
        operator/=(const complex<_Tp>& __z)
        {
          _ComplexT __t;
@@ -1442,7 +1511,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
          return *this;
        }
 
-      const _ComplexT& __rep() const { return _M_value; }
+      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
 
     private:
       _ComplexT _M_value;
@@ -1450,15 +1519,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   // These bits have to be at the end of this file, so that the
   // specializations have all been defined.
-  inline
+  inline _GLIBCXX_CONSTEXPR
   complex<float>::complex(const complex<double>& __z)
   : _M_value(__z.__rep()) { }
 
-  inline
+  inline _GLIBCXX_CONSTEXPR
   complex<float>::complex(const complex<long double>& __z)
   : _M_value(__z.__rep()) { }
 
-  inline
+  inline _GLIBCXX_CONSTEXPR
   complex<double>::complex(const complex<long double>& __z)
   : _M_value(__z.__rep()) { }
 
@@ -1485,9 +1554,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   // @} group complex_numbers
 
-_GLIBCXX_END_NAMESPACE
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
 
-_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
+namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   // See ext/type_traits.h for the primary template.
   template<typename _Tp, typename _Up>
@@ -1503,7 +1575,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
     public:
       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
     };
-  
+
   template<typename _Tp, typename _Up>
     struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
     {
@@ -1511,27 +1583,325 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
     };
 
-_GLIBCXX_END_NAMESPACE
-
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
-#  if defined(_GLIBCXX_INCLUDE_AS_TR1)
-#    error C++0x header cannot be included from TR1 header
-#  endif
-#  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
-#    include <tr1_impl/complex>
-#  else
-#    define _GLIBCXX_INCLUDE_AS_CXX0X
-#    define _GLIBCXX_BEGIN_NAMESPACE_TR1
-#    define _GLIBCXX_END_NAMESPACE_TR1
-#    define _GLIBCXX_TR1
-#    include <tr1_impl/complex>
-#    undef _GLIBCXX_TR1
-#    undef _GLIBCXX_END_NAMESPACE_TR1
-#    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
-#    undef _GLIBCXX_INCLUDE_AS_CXX0X
-#  endif
-
-_GLIBCXX_BEGIN_NAMESPACE(std)
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
+
+#if __cplusplus >= 201103L
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  // Forward declarations.
+  template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
+  template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
+  template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
+
+  template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
+  template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
+  template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
+  // DR 595.
+  template<typename _Tp> _Tp               fabs(const std::complex<_Tp>&);
+
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    __complex_acos(const std::complex<_Tp>& __z)
+    {
+      const std::complex<_Tp> __t = std::asin(__z);
+      const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
+      return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
+    }
+
+#if _GLIBCXX_USE_C99_COMPLEX_TR1
+  inline __complex__ float
+  __complex_acos(__complex__ float __z)
+  { return __builtin_cacosf(__z); }
+
+  inline __complex__ double
+  __complex_acos(__complex__ double __z)
+  { return __builtin_cacos(__z); }
+
+  inline __complex__ long double
+  __complex_acos(const __complex__ long double& __z)
+  { return __builtin_cacosl(__z); }
+
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    acos(const std::complex<_Tp>& __z)
+    { return __complex_acos(__z.__rep()); }
+#else
+  /// acos(__z) [8.1.2].
+  //  Effects:  Behaves the same as C99 function cacos, defined
+  //            in subclause 7.3.5.1.
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    acos(const std::complex<_Tp>& __z)
+    { return __complex_acos(__z); }
+#endif
+
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    __complex_asin(const std::complex<_Tp>& __z)
+    {
+      std::complex<_Tp> __t(-__z.imag(), __z.real());
+      __t = std::asinh(__t);
+      return std::complex<_Tp>(__t.imag(), -__t.real());
+    }
+
+#if _GLIBCXX_USE_C99_COMPLEX_TR1
+  inline __complex__ float
+  __complex_asin(__complex__ float __z)
+  { return __builtin_casinf(__z); }
+
+  inline __complex__ double
+  __complex_asin(__complex__ double __z)
+  { return __builtin_casin(__z); }
+
+  inline __complex__ long double
+  __complex_asin(const __complex__ long double& __z)
+  { return __builtin_casinl(__z); }
+
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    asin(const std::complex<_Tp>& __z)
+    { return __complex_asin(__z.__rep()); }
+#else
+  /// asin(__z) [8.1.3].
+  //  Effects:  Behaves the same as C99 function casin, defined
+  //            in subclause 7.3.5.2.
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    asin(const std::complex<_Tp>& __z)
+    { return __complex_asin(__z); }
+#endif
+
+  template<typename _Tp>
+    std::complex<_Tp>
+    __complex_atan(const std::complex<_Tp>& __z)
+    {
+      const _Tp __r2 = __z.real() * __z.real();
+      const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
+
+      _Tp __num = __z.imag() + _Tp(1.0);
+      _Tp __den = __z.imag() - _Tp(1.0);
+
+      __num = __r2 + __num * __num;
+      __den = __r2 + __den * __den;
+
+      return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
+                              _Tp(0.25) * log(__num / __den));
+    }
+
+#if _GLIBCXX_USE_C99_COMPLEX_TR1
+  inline __complex__ float
+  __complex_atan(__complex__ float __z)
+  { return __builtin_catanf(__z); }
+
+  inline __complex__ double
+  __complex_atan(__complex__ double __z)
+  { return __builtin_catan(__z); }
+
+  inline __complex__ long double
+  __complex_atan(const __complex__ long double& __z)
+  { return __builtin_catanl(__z); }
+
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    atan(const std::complex<_Tp>& __z)
+    { return __complex_atan(__z.__rep()); }
+#else
+  /// atan(__z) [8.1.4].
+  //  Effects:  Behaves the same as C99 function catan, defined
+  //            in subclause 7.3.5.3.
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    atan(const std::complex<_Tp>& __z)
+    { return __complex_atan(__z); }
+#endif
+
+  template<typename _Tp>
+    std::complex<_Tp>
+    __complex_acosh(const std::complex<_Tp>& __z)
+    {
+      // Kahan's formula.
+      return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
+                                + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
+    }
+
+#if _GLIBCXX_USE_C99_COMPLEX_TR1
+  inline __complex__ float
+  __complex_acosh(__complex__ float __z)
+  { return __builtin_cacoshf(__z); }
+
+  inline __complex__ double
+  __complex_acosh(__complex__ double __z)
+  { return __builtin_cacosh(__z); }
+
+  inline __complex__ long double
+  __complex_acosh(const __complex__ long double& __z)
+  { return __builtin_cacoshl(__z); }
+
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    acosh(const std::complex<_Tp>& __z)
+    { return __complex_acosh(__z.__rep()); }
+#else
+  /// acosh(__z) [8.1.5].
+  //  Effects:  Behaves the same as C99 function cacosh, defined
+  //            in subclause 7.3.6.1.
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    acosh(const std::complex<_Tp>& __z)
+    { return __complex_acosh(__z); }
+#endif
+
+  template<typename _Tp>
+    std::complex<_Tp>
+    __complex_asinh(const std::complex<_Tp>& __z)
+    {
+      std::complex<_Tp> __t((__z.real() - __z.imag())
+                           * (__z.real() + __z.imag()) + _Tp(1.0),
+                           _Tp(2.0) * __z.real() * __z.imag());
+      __t = std::sqrt(__t);
+
+      return std::log(__t + __z);
+    }
+
+#if _GLIBCXX_USE_C99_COMPLEX_TR1
+  inline __complex__ float
+  __complex_asinh(__complex__ float __z)
+  { return __builtin_casinhf(__z); }
+
+  inline __complex__ double
+  __complex_asinh(__complex__ double __z)
+  { return __builtin_casinh(__z); }
+
+  inline __complex__ long double
+  __complex_asinh(const __complex__ long double& __z)
+  { return __builtin_casinhl(__z); }
+
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    asinh(const std::complex<_Tp>& __z)
+    { return __complex_asinh(__z.__rep()); }
+#else
+  /// asinh(__z) [8.1.6].
+  //  Effects:  Behaves the same as C99 function casin, defined
+  //            in subclause 7.3.6.2.
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    asinh(const std::complex<_Tp>& __z)
+    { return __complex_asinh(__z); }
+#endif
+
+  template<typename _Tp>
+    std::complex<_Tp>
+    __complex_atanh(const std::complex<_Tp>& __z)
+    {
+      const _Tp __i2 = __z.imag() * __z.imag();
+      const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
+
+      _Tp __num = _Tp(1.0) + __z.real();
+      _Tp __den = _Tp(1.0) - __z.real();
+
+      __num = __i2 + __num * __num;
+      __den = __i2 + __den * __den;
+
+      return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
+                              _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
+    }
+
+#if _GLIBCXX_USE_C99_COMPLEX_TR1
+  inline __complex__ float
+  __complex_atanh(__complex__ float __z)
+  { return __builtin_catanhf(__z); }
+
+  inline __complex__ double
+  __complex_atanh(__complex__ double __z)
+  { return __builtin_catanh(__z); }
+
+  inline __complex__ long double
+  __complex_atanh(const __complex__ long double& __z)
+  { return __builtin_catanhl(__z); }
+
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    atanh(const std::complex<_Tp>& __z)
+    { return __complex_atanh(__z.__rep()); }
+#else
+  /// atanh(__z) [8.1.7].
+  //  Effects:  Behaves the same as C99 function catanh, defined
+  //            in subclause 7.3.6.3.
+  template<typename _Tp>
+    inline std::complex<_Tp>
+    atanh(const std::complex<_Tp>& __z)
+    { return __complex_atanh(__z); }
+#endif
+
+  template<typename _Tp>
+    inline _Tp
+    /// fabs(__z) [8.1.8].
+    //  Effects:  Behaves the same as C99 function cabs, defined
+    //            in subclause 7.3.8.1.
+    fabs(const std::complex<_Tp>& __z)
+    { return std::abs(__z); }
+
+  /// Additional overloads [8.1.9].
+  template<typename _Tp>
+    inline typename __gnu_cxx::__promote<_Tp>::__type
+    arg(_Tp __x)
+    {
+      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
+#if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
+      return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
+                              : __type();
+#else
+      return std::arg(std::complex<__type>(__x));
+#endif
+    }
+
+  template<typename _Tp>
+    _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
+    imag(_Tp)
+    { return _Tp(); }
+
+  template<typename _Tp>
+    inline typename __gnu_cxx::__promote<_Tp>::__type
+    norm(_Tp __x)
+    {
+      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
+      return __type(__x) * __type(__x);
+    }
+
+  template<typename _Tp>
+    _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
+    real(_Tp __x)
+    { return __x; }
+
+  template<typename _Tp, typename _Up>
+    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
+    pow(const std::complex<_Tp>& __x, const _Up& __y)
+    {
+      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
+      return std::pow(std::complex<__type>(__x), __type(__y));
+    }
+
+  template<typename _Tp, typename _Up>
+    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
+    pow(const _Tp& __x, const std::complex<_Up>& __y)
+    {
+      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
+      return std::pow(__type(__x), std::complex<__type>(__y));
+    }
+
+  template<typename _Tp, typename _Up>
+    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
+    pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
+    {
+      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
+      return std::pow(std::complex<__type>(__x),
+                     std::complex<__type>(__y));
+    }
 
   // Forward declarations.
   // DR 781.
@@ -1580,8 +1950,55 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       return std::proj(std::complex<__type>(__x));
     }
 
-_GLIBCXX_END_NAMESPACE
+  template<typename _Tp>
+    inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
+    conj(_Tp __x)
+    {
+      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
+      return std::complex<__type>(__x, -__type());
+    }
 
-#endif
+#if __cplusplus > 201103L
+
+inline namespace literals {
+inline namespace complex_literals {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wliteral-suffix"
+#define __cpp_lib_complex_udls 201309
+
+  constexpr std::complex<float>
+  operator""if(long double __num)
+  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
+
+  constexpr std::complex<float>
+  operator""if(unsigned long long __num)
+  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
+
+  constexpr std::complex<double>
+  operator""i(long double __num)
+  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
+
+  constexpr std::complex<double>
+  operator""i(unsigned long long __num)
+  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
+
+  constexpr std::complex<long double>
+  operator""il(long double __num)
+  { return std::complex<long double>{0.0L, __num}; }
+
+  constexpr std::complex<long double>
+  operator""il(unsigned long long __num)
+  { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
+
+#pragma GCC diagnostic pop
+} // inline namespace complex_literals
+} // inline namespace literals
+
+#endif // C++14
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
+
+#endif  // C++11
 
-#endif /* _GLIBCXX_COMPLEX */
+#endif  /* _GLIBCXX_COMPLEX */