Search code examples
cgcccomplex-numbersgcc-extensions

Test for GNU extension


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.


Solution

  • 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