Search code examples
c++gccattributesgcc-attribute

What preprocessor condition should I check so as to use __attribute__((const))?


I've gotten a piece of code which applies __attribute__((const)) to some functions. Now, I'd rather not remove it when it's usable, but on the other hand, I do want to be more portable, so - I want to say

#if some condition here
#define ATTRIBUTE(an_attribute)  __attribute__((an_attribute))
#else
#define ATTRIBUTE(an_attribute)  
#endif

void foo(int x) ATTRIBUTE(const)

What should the condition be?

Notes:

  • I know that, with C++17, we have this as a proper C++ attribute; but I can't assume C++17 is used. In fact, let's assume it isn't to make things simple.
  • Extra points if you can also answer the question for __attribute__((pure)).

Solution

  • __attribute__ is a GNU extension. Only GNU compilers and compilers that claim to be GNU (such as Clang and ICC) support it. So you can do this:

    #ifdef __GNUC__
    # define ATTRIBUTE(x) __attribute__ (x)
    # else
    # define ATTRIBUTE(x)
    #endif
    

    const was introduced in GCC 2.5, and pure during GCC 2.96 development, so it does not make sense to test separate for their support: these compiler versions will not be able to compile current C++ code anyway. (__attribute__ itself was introduced with GCC 2.0. I think the C++ front end has always supported it.)