函数参数中使用的复杂文字'i' [英] Complex literal 'i' used in function argument
问题描述
考虑以下代码:
std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;
第二行无法编译为:错误:与"operator +"不匹配(操作数类型为"std :: complex< double>"和"__complex__ double")
当在函数调用中使用复杂文字时,例如,
This also shows up when using the complex literal in function calls, e.g.
std::exp<std::complex<double>>( 1.0i * 3.14159 );
为什么复杂字面量 1.0i
无法转换为 std :: complex< double>
?
How come the complex literal 1.0i
is not convertible to std::complex<double>
?
我是否必须使用 1.0i
显式构造 std :: complex
?
Do I have to explicitly construct a std::complex
with 1.0i
?
推荐答案
您应使用-std = c ++ 14
(无GNU ext)重新编译,以避免 i
后缀为 gcc扩展名
You should recompile with --std=c++14
(no GNU ext) to avoid conflict of i
suffix with gcc extension
ISO C ++ 14库也定义了"i"后缀,因此包含
< complex>
标头的C ++ 14代码不能对GNU扩展使用"i"."j"后缀仍然具有GNU含义.
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.
这篇关于函数参数中使用的复杂文字'i'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!