https://gcc.gnu.org/g:33cb72fafe25c5688ae26b4a4491f4486edf75d0

commit r16-3436-g33cb72fafe25c5688ae26b4a4491f4486edf75d0
Author: Weslley da Silva Pereira <weslley.spere...@gmail.com>
Date:   Tue Aug 26 13:23:11 2025 +0100

    libstdc++: Remove implicit type conversions in std::complex
    
    The current implementation of `complex<_Tp>` assumes that int
    `int` is implicitly convertible to `_Tp`, e.g., when using
    `complex<_Tp>(1)`.
    
    This patch transforms the implicit conversions into explicit type casts.
    As a result, `std::complex` is now able to support more types. One
    example is the type `Eigen::Half` from
    https://eigen.tuxfamily.org/dox-devel/Half_8h_source.html which does not
    implement implicit type conversions.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/complex (polar, __complex_sqrt, pow)
            (__complex_pow_unsigned): Use explicit conversions from int to
            the complex value_type.

Diff:
---
 libstdc++-v3/include/std/complex | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/libstdc++-v3/include/std/complex b/libstdc++-v3/include/std/complex
index d9d2d8afda89..4765425174ff 100644
--- a/libstdc++-v3/include/std/complex
+++ b/libstdc++-v3/include/std/complex
@@ -96,7 +96,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp>
     _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
   ///  Return complex with magnitude @a rho and angle @a theta.
-  template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
+  template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = _Tp(0));
 
   // Transcendentals:
   /// Return complex cosine of @a z.
@@ -1038,7 +1038,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     inline complex<_Tp>
     polar(const _Tp& __rho, const _Tp& __theta)
     {
-      __glibcxx_assert( __rho >= 0 );
+      __glibcxx_assert( __rho >= _Tp(0) );
       return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
     }
 
@@ -1238,13 +1238,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       if (__x == _Tp())
         {
-          _Tp __t = sqrt(abs(__y) / 2);
+         _Tp __t = sqrt(abs(__y) / _Tp(2));
           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
         }
       else
         {
-          _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
-          _Tp __u = __t / 2;
+          _Tp __t = sqrt(_Tp(2) * (std::abs(__z) + abs(__x)));
+          _Tp __u = __t / _Tp(2);
           return __x > _Tp()
             ? complex<_Tp>(__u, __y / __t)
             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
@@ -1334,7 +1334,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     complex<_Tp>
     __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
     {
-      complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
+      complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(_Tp(1));
 
       while (__n >>= 1)
         {
@@ -1357,7 +1357,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     pow(const complex<_Tp>& __z, int __n)
     {
       return __n < 0
-       ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
+       ? complex<_Tp>(_Tp(1)) / std::__complex_pow_unsigned(__z,
+                                                            -(unsigned)__n)
         : std::__complex_pow_unsigned(__z, __n);
     }

Reply via email to