__unused标志行为/用法(GCC with Objective C) [英] __unused Flag Behavior/Usage (GCC with Objective C)

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

问题描述

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



为什么这个编译没有警告/错误?看起来奇怪的是,我明确地告诉编译器我不会使用变量,那么当我使用它时,事情就像平常一样继续。

   - (void)viewDidLoad 
{
[super viewDidLoad];

[self foo:0];

$ b - (void)foo:(NSInteger)__ unused myInt
{
myInt ++;
NSLog(@myInt:%d,myInt); // Logs'1'
}

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

   - (void)foo:(NSInteger)__ unused myInt; 

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


解决方案

__ unused 宏(实际上扩展为 __属性__((未使用)) GCC属性)仅告诉编译器如果我不使用此变量。


未使用:该属性附加到变量,意味着该变量可能未被使用。 GCC不会为这个变量产生警告。 来源:gnu.gcc.org doc


因此,此GCC属性在您不需要警告时避免使用一个变量,当你使用你声称没有使用的变量时,不要触发一个
$ b




关于在你的最后一个例子中将变量名称之前或之后的属性,在你的情况下,它们都被接受和等价:为了兼容性的目的,编译器对该位置只是宽松的(正如你也可以同时写入 const int i int const i


<为了与为编译器版本编写的现有代码兼容,这些编译器版本没有在嵌套的声明器上实现属性,在放置属性资料来源:gnu.gcc.org doc



I just now learned about the __unused flag that can be used when compiling with GCC and the more I learn about it, the more questions I have...

Why does this compile without warning/error? It seems strange that I specifically tell the compiler I won't be using a variable, then when I use it, things proceed as normal.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self foo:0];
}

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

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)

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

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