__has_cpp_attribute不是'类似函数'的宏?

Mue*_*ito 4 c++ macros gcc c++14

我试图将该[[deprecated]]属性引入我的代码库.但是,并非所有我需要支持的编译器都支持这种语法(标准化之前不同编译器使用的各种方法在属性标准化提议N2761中描述).因此,我试图在此属性中有条件地编译,首先使用__has_cpp_attribute类似宏的函数,如果它可用,如下所示:

#if defined(__has_cpp_attribute) && __has_cpp_attribute(deprecated)
    #define DEPRECATED(msg) [[deprecated(msg)]]
#elif OTHER_COMPILER
    // ...
#endif
Run Code Online (Sandbox Code Playgroud)

但是,在编译我正在使用的gcc version 4.9.2 (GCC)命令行时,我遇到错误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)
Run Code Online (Sandbox Code Playgroud)

此错误似乎表明__has_cpp_attribute已定义,但它不是宏功能.[[deprecated]]在gcc中有条件地编译属性的正确方法是什么?

T.C*_*.C. 12

GCC 4.9没有__has_cpp_attribute,并且短路行为&&不会扩展到允许无效结构跟随它.

也就是说,如果foo没有定义,

#if defined(foo) && foo(bar)
Run Code Online (Sandbox Code Playgroud)

无效.

你想要的是什么

#if defined(__has_cpp_attribute) 
    #if __has_cpp_attribute(deprecated)
        #define DEPRECATED(msg) [[deprecated(msg)]]
    #endif
#elif OTHER_COMPILER
    // ...
#endif
Run Code Online (Sandbox Code Playgroud)

因此,__has_cpp_attribute如果__has_cpp_attribute未定义,则条件使用在跳过的组中.(在跳过的组中,预处理指令仅通过指令的名称处理;其余的标记将被忽略.)