Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex literal 'i' used in function argument

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?

like image 667
Louen Avatar asked Aug 08 '18 09:08

Louen


1 Answers

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.

like image 163
user7860670 Avatar answered Nov 09 '22 03:11

user7860670