__未使用的标志行为/用法(带有Objective-C的GCC) [英] __unused Flag Behavior/Usage (GCC with Objective-C)

查看:66
本文介绍了__未使用的标志行为/用法(带有Objective-C的GCC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解了__unused标志,可以在使用GCC进行编译时使用它,而且我学到的越多,我遇到的问题就越多...

为什么在没有警告/错误的情况下进行编译?我专门告诉编译器我将不会使用变量,这似乎很奇怪,然后当我使用它时,一切照常进行.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self foo:0];
}

- (void)foo:(NSInteger)__unused myInt
{
    myInt++;
    NSLog(@"myInt: %d", myInt);  // Logs '1'
}

此外,以下两个方法签名之间有什么区别?

- (void)foo:(NSInteger)__unused myInt;

- (void)foo:(NSInteger)myInt __unused;

解决方案

__unused宏(实际上已扩展为__attribute__((unused)) GCC属性)仅告诉编译器如果我不警告我"不要使用此变量".

unused:此属性附加到变量,表示该变量可能未被使用. GCC不会对此变量发出警告. (来源:gnu.gcc.org文档)

因此,当您不使用变量时,此GCC属性是避免警告,而在您声明未使用的变量时,不要触发. /p>


关于在最后一个示例中将属性放在变量名之前或之后,在您的情况下,两者都被接受并等效:为了兼容性起见,编译器只宽容该位置(相当大的原因是您也可以写两个int const i)

为了与为未在嵌套声明器上实现属性的编译器版本编写的现有代码兼容,在放置属性(

Also, what is the difference between the following two method signatures?

- (void)foo:(NSInteger)__unused myInt;

- (void)foo:(NSInteger)myInt __unused;

The __unused macro (which is in fact expanded to the __attribute__((unused)) GCC attribute) only tells the compiler "don't warn me if I don't use this variable".

unused: This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC does not produce a warning for this variable. (Source: gnu.gcc.org doc)

So this GCC attribute is to avoid a warning when you don't use a variable, and NOT to trigger one when you use the variable you claimed unused.


As regard to putting the attribute before or after the variable name in your last example, both are accepted and equivalent in your case: the compiler is just lenient about that placement for compatibility purposes (quite as you can also write both const int i or int const i)

For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes (Source: gnu.gcc.org doc)

这篇关于__未使用的标志行为/用法(带有Objective-C的GCC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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