__has_cpp_attribute - not a function macro?

I am trying to enter an attribute [[deprecated]]in my codebase. However, not all compilers that I need to support support this syntax (the different method used by different compilers before standardization is described in the standardization proposal for attribute N2761 ). Thus, I am trying to conditionally compile this attribute using the macroscopic function first __has_cpp_attribute, if available, for example:

#if defined(__has_cpp_attribute) && __has_cpp_attribute(deprecated)
    #define DEPRECATED(msg) [[deprecated(msg)]]
#elif OTHER_COMPILER
    // ...
#endif

However, when compiling, I get errors when using the gcc version 4.9.2 (GCC)command line gcc -std=c++14 cpp.cpp:

cpp.cpp:1:56: error: missing binary operator before token "("
#if defined(__has_cpp_attribute) && __has_cpp_attribute(deprecated)

This error indicates that it is __has_cpp_attributedefined, but is not a macro function. What is the correct way to conditionally compile an attribute [[deprecated]]in gcc?

+4
2

GCC 4.9 __has_cpp_attribute, && , .

, foo ,

#if defined(foo) && foo(bar)

.

#if defined(__has_cpp_attribute) 
    #if __has_cpp_attribute(deprecated)
        #define DEPRECATED(msg) [[deprecated(msg)]]
    #endif
#elif OTHER_COMPILER
    // ...
#endif

, __has_cpp_attribute, , , __has_cpp_attribute . ( , , , .)

+10

TC __has_cpp_attribute, , __has_cpp_attribute, .

#if defined(__has_cpp_attribute)
#  define MY_HAS_CPP_ATTRIBUTE(attr) __has_cpp_attribute(attr)
#else
#  define MY_HAS_CPP_ATTRIBUTE(attr) (0)
#endif

#if MY_HAS_CPP_ATTRIBUTE(attr)
#  define MY_DEPRECATED [[deprecated(msg)]]
#else
#  define MY_DEPRECATED
#endif

, , __has_cpp_attribute. , defined(__has_cpp_attribute) defined(__has_cpp_attribute) , , defined(__has_cpp_attribute) , . __has_cpp_attribute .

, , __has_cpp_attribute, , , ; HEDLEY_DEPRECATED Hedley. GCC 4. 5+, ICC 13+, armcc 4. 1+ TI 7. 3+ , MSVC 13. 10+ Pelles 6. 50+ declspec IAR , , , , , , .

0

Source: https://habr.com/ru/post/1589484/


All Articles