Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 copy assignment for std::complex in g++ 4.5 - no match for 'operator+'

The code below fails to compile with g++ version 4.5.0 using the -std=c++0x switch. I get the following error message:

error: no match for 'operator+' in 'std::pow [with _Tp = float, _Up = int, typename __gnu_cxx::__promote_2<_Tp, _Up>::__type = double](((const std::complex<float>&)((const std::complex<float>*)(& x))), ((const int&)((const int*)(&2)))) + y'

I believe this relates to the Assignable requirement mentioned here. Should I define my own copy assignment operator for complex? If so, how?

#include <complex>
using namespace std;

int main(int argc, char *argv[]) {
  complex<float> x,y;
  x = pow(x,2);      // ok
  x = x        + y;  // ok
  x = pow(x,2) + y;  // error
  return 0;
}
like image 953
user2023370 Avatar asked Dec 12 '11 17:12

user2023370


1 Answers

[cmplx.over]/p3 specifies additional overloads for pow when complex is involved:

Function template pow shall have additional overloads sufficient to ensure, for a call with at least one argument of type complex<T>:

  1. If either argument has type complex<long double> or type long double, then both arguments are effectively cast to complex<long double>.

  2. Otherwise, if either argument has type complex<double>, double, or an integer type, then both arguments are effectively cast to complex<double>.

  3. Otherwise, if either argument has type complex<float> or float, then both arguments are effectively cast to complex<float>.

The 2 is being promoted to a double, and pow(complex<float>, double) returns a complex<double>.

like image 185
Howard Hinnant Avatar answered Sep 28 '22 09:09

Howard Hinnant