gcc中的C ++ 11样式[[unused]]属性? [英] C++11-style [[unused]] attribute in gcc?

查看:985
本文介绍了gcc中的C ++ 11样式[[unused]]属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gcc / g ++ 4.9下我可以写:

  int x __attribute __((未使用))= f 

表示x有意未使用。



是否可以用C ++ 11 [[]] 属性符号来做到这一点?



我试过:

  int x [[unused]] = f 

但不起作用。



解决方案

是的,使用<$ c $像已经说过的未使用

$ c>不是标准指定的标准属性的一部分。



标准允许实现定义的属性太像是 __ attribute __ __ declspec 与新语法一起使用。如果编译器不识别属性(在MSVC上编译时的gcc属性作为示例),它将被忽略。 (可能带有警告)



对于gcc,您可以使用gnu前缀和C ++ 11属性语法: [[gnu :: unused ]] 而不是 __属性__((未使用))同样应该适用于其他gcc属性。



没有gnu前缀的示例



使用gnu前缀的示例


Under gcc/g++ 4.9 I can write:

int x __attribute__((unused)) = f();

to indicate that x is intentionally unused.

Is it possible to do this with the C++11 [[]] attribute notation somehow?

I tried:

int x [[unused]] = f();

but it doesn't work.

(Yes, I know it is an implementation-defined attribute.)

解决方案

Yes, use [[gnu::unused]]

Like already said unused isn't part of the standard attributes specified by the standard.

The standard allows implementation defined attributes too like the __attribute__ and __declspec ones to be used with the new syntax. If a compiler doesn't recognize an attribute (a gcc attribute when compiling on MSVC as example) it'll simply be ignored. (probably with a warning)

For gcc you can use the gnu prefix and the C++11 attribute syntax: [[gnu::unused]] instead of __attribute__((unused)) the same should apply for the other gcc attributes too.

example without gnu prefix

example with gnu prefix

这篇关于gcc中的C ++ 11样式[[unused]]属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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