考虑一下这段代码:
#include <iostream> #include <complex> int main() { std::complex<double> z1 = 5; std::cout << z1 - 1 << "\n"; // must change to z1 - 1.0 to compile std::complex<int> z2 = 5; std::cout << z2 - 1.0 << "\n"; // must change to z2 - 1 to compile }
这会产生编译错误,因为在表达式z1 – 1或z2 – 1.0中找不到类型的运算符.另一方面,更改这些表达式以使基本类型匹配工作正常.
天然地,对于z1 – 1,我希望int 1被提升为double,并且期望z2 – 1.0中基本类型为int的z2被提升为复数< double>.这是怎么回事?