我正在尝试介绍[[deprecated]]
属性到我的代码库中。然而,并不是所有我需要支持的编译器都支持这种语法(标准化之前不同编译器使用的各种方法在属性标准化提案 N2761)。因此,我尝试使用此属性有条件地编译__has_cpp_attribute
首先是类似宏的函数(如果可用),如下所示:
#if defined(__has_cpp_attribute) && __has_cpp_attribute(deprecated)
#define DEPRECATED(msg) [[deprecated(msg)]]
#elif OTHER_COMPILER
// ...
#endif
但是,我在编译时遇到错误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)
这个错误似乎表明__has_cpp_attribute
已定义,但它不是宏函数。有条件编译的正确方法是什么[[deprecated]]
gcc 中的属性?
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
没有定义。 (当处于被跳过的组中时,预处理指令仅通过指令名称进行处理;其余标记将被忽略。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)