use*_*370 6 c++ operator-overloading complextype c++11
下面的代码无法使用-std=c++0x交换机使用g ++版本4.5.0进行编译.我收到以下错误消息:
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'
Run Code Online (Sandbox Code Playgroud)
我认为这与此处提到的可分配要求有关.我应该为复杂定义我自己的复制赋值运算符吗?如果是这样,怎么样?
#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;
}
Run Code Online (Sandbox Code Playgroud)
How*_*ant 11
[cmplx.over]/p3指定涉及pow何时的其他重载complex:
函数模板pow应具有足够的额外重载,以确保对于具有至少一个类型的参数的调用
complex<T>:
如果任一参数具有类型
complex<long double>或类型long double,则两个参数都被有效地转换为complex<long double>.否则,如果任一参数具有type
complex<double>,double或整数类型,则两个参数都被有效地转换为complex<double>.否则,如果任一参数具有type
complex<float>或float,则两个参数都被有效地转换为complex<float>.
2被提升为双,并pow(complex<float>, double)返回a complex<double>.