__has_cpp_attribute不是“类似于函数"的宏吗? [英] __has_cpp_attribute not a 'function-like' macro?

查看:40
本文介绍了__has_cpp_attribute不是“类似于函数"的宏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 [[[deprecated]] 属性引入我的代码库.但是,并不是我需要支持的所有编译器都支持此语法(

I am attempting to introduce the [[deprecated]] attribute into my codebase. However, not all the compilers I am required to support have support for this syntax yet (the various method used by different compilers before standardization are described in the attribute standardization proposal N2761). Thus, I am attempting to conditionally compile in this attribute, using the __has_cpp_attribute macro-like function first, if it is available, like so:

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

但是,我在使用 gcc版本4.9.2(GCC),命令行 gcc -std = c ++ 14 cpp.cpp 时遇到错误.代码>:

However, I'm getting errors when compiling this I am using 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)

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

This error seems to indicate that __has_cpp_attribute is defined, but that it is not a macro function. What is the proper way to conditionally compile the [[deprecated]] attribute in gcc?

推荐答案

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

GCC 4.9 doesn't have __has_cpp_attribute, and the short-circuiting behavior of && does not extend to allowing invalid constructs to follow it.

也就是说,如果未定义 foo

That is to say, if foo isn't defined,

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

so that the condition using __has_cpp_attribute is in a group that is skipped if __has_cpp_attribute is not defined. (When in a group that is skipped, preprocessing directives are only processed through the directive's name; the remaining tokens are ignored.)

这篇关于__has_cpp_attribute不是“类似于函数"的宏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆