mercredi 6 décembre 2017

Add implicit promotion to complex class, last step

I would like to allow implicit conversion when summing complex numbers. For example:

 complex<double> a; 
 complex<long double> b;
 int i;

 auto sum = a + b; // (1)
 auto SUM = a + i; // (2)

I have the code that enable conversion (1) thanks to answer implicit type promotion in summing two complex<> In order to enable also the (2) conversion I used enable_if_t

  template <typename T, typename U>
  auto operator +(const ::std::complex<T> &a,     std::enable_if_t<std::is_arithmetic<U>::value, U>  &b) 
  {
   typedef decltype(::std::declval<T>() + ::std::declval<U>()) comcomp_t;
   typedef ::std::complex<comcomp_t> result_t;
   return ::std::operator +(result_t{a}, result_t{b});
  }

However, I got a compilation error saying "couldn't deduce template paramter 'U'. I guess my comprehension of SFINAE is very shallow. Any help would be highly appreciated. Thanks

Aucun commentaire:

Enregistrer un commentaire