简体   繁体   中英

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;
}

[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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM