gpt4 book ai didi

c++ - __has_cpp_attribute 不是 'function-like' 宏?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:09:54 28 4
gpt4 key购买 nike

我正在尝试将 [[deprecated]] 属性引入我的代码库。然而,并不是所有我需要支持的编译器都支持这种语法(在 attribute standardization proposal 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,但它不是一个宏函数。在 gcc 中有条件地编译 [[deprecated]] 属性的正确方法是什么?

最佳答案

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 则跳过该组。 (在被跳过的组中时,预处理指令仅通过指令名称进行处理;其余标记将被忽略。)

关于c++ - __has_cpp_attribute 不是 'function-like' 宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30402943/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com