Test for GNU extension

337 Views Asked by At

I want to make a macro to return the real part of a complex number (which will work with double, float and long double types). The GNU C extension __real__ seems to fit the bill (although it is not portable unfortunately). I am trying the following:

#include <complex.h>
#if defined(__real__)
#define MYREAL(z) (__real__ z)
#endif

However it seems that the __real__ extension is not defined as a usual macro, so the defined(__real__) test fails, even though it is available. Does anyone know how to test for the existence of __real__ to make a proper macro for this?

Also, if anyone knows of a portable way to do this, I'd be interested in that solution as well.

2

There are 2 best solutions below

0
On BEST ANSWER

Per manual:

To test for the availability of these features in conditional compilation, check for a predefined macro __GNUC__, which is always defined under GCC.

Hence:

#ifdef __GNUC__

#define MYREAL(z) (__real__(z))

#endif
0
On

Also, if anyone knows of a portable way to do this, I'd be interested in that solution as well.

That would be the creal() macro in <tgmath.h>, which works for all complex types.