Complex literal i used in function argument

  • A+

There seems to be a problem, using the literal i in C++ with std::complex.

Consider the following code:

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

The second line fails to compile with: error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__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 ); 

How come the complex literal 1.0i is not convertible to std::complex<double> ?

Do I have to explicitly construct a std::complex with 1.0i ?


You should recompile with --std=c++14 (no GNU ext) to avoid conflict of i suffix with 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.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: