3

I want to call a C function of a complex argument, declared as

double _Complex cerf ( double _Complex z );

from C++. The following works:

#include<complex.h>
double _Complex z = 1.0;
cerf( z );

However, I fail to assign an imaginary part to z:

double _Complex z = 1.0 + _Complex_I * 2.0;

does not compile under g++: "unable to find numeric literal operator ‘operator""iF’". Other expressions I tried lead to other, similarly cryptic error messages.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Joachim W
  • 7,290
  • 5
  • 31
  • 59

1 Answers1

4

C++ and C is not completely compatible with each other, especially for C99 features, which may or may not present in C++, and complex number is one of them. C++ and C compatibility

If you are using GCC, you can use the flag -fext-numeric-literals to enable support for _Complex.

Working example

SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • Thanks. Using g++ with `-fext-numeric-literals`, the posted code compiles cleanly. – Joachim W Jan 20 '15 at 07:50
  • Note that the std::complex<> template deliberately has a binary layout making it compatible with _Complex in C99. This means you can substitute one for the other depending on whether you are compiling as C99 or C++11 (or later). See for example: http://en.cppreference.com/w/cpp/numeric/complex If using the gcc extension you can mix them freely (but not portably) and reinterpret_cast between them. – Bruce Adams Aug 18 '16 at 15:10