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

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

问题描述

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

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天全站免登陆