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>
:
If either argument has type
complex<long double>
or typelong double
, then both arguments are effectively cast tocomplex<long double>
.Otherwise, if either argument has type
complex<double>
,double
, or an integer type, then both arguments are effectively cast tocomplex<double>
.Otherwise, if either argument has type
complex<float>
orfloat
, then both arguments are effectively cast tocomplex<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.