函数参数中使用的复杂文字 'i'

Complex literal 'i' used in function argument

似乎有问题,在 C++ 中使用 literal istd::complex

考虑以下代码:

std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;

第二行编译失败: error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’)

在函数调用中使用复杂文字时也会出现这种情况,例如

std::exp<std::complex<double>>( 1.0i * 3.14159 );

为什么复杂文字 1.0i 不能转换为 std::complex<double>

我是否必须用 1.0i 显式构建 std::complex

您应该使用 --std=c++14(无 GNU ext)重新编译以避免 i 后缀与 gcc extension

的冲突

The ISO C++14 library also defines the ‘i’ suffix, so C++14 code that includes the <complex> header cannot use ‘i’ for the GNU extension. The ‘j’ suffix still has the GNU meaning.