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

The code below will not compile with g ++ version 4.5.0 using the -std=c++0x switch. The following error message appears:

 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 is due to the assignment 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; } 
+6
source share
1 answer

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

The pow function template must have additional overloads sufficient to ensure that for a call with at least one argument of type complex<T> :

  • If either argument is of type complex<long double> or type long double , then both arguments are effectively mapped to complex<long double> .

  • Otherwise, if any argument is of type complex<double> , double or integer type, then both arguments are effectively passed to complex<double> .

  • Otherwise, if either argument is of type complex<float> or float , then both arguments are effectively passed to complex<float> .

2 advances to double, and pow(complex<float>, double) returns a complex<double> .

+11
source

Source: https://habr.com/ru/post/903611/


All Articles