gpt4 book ai didi

ios - __unused 标志行为/用法(带有 Objective-C 的 GCC)

转载 作者:IT老高 更新时间:2023-10-28 11:50:13 42 4
gpt4 key购买 nike

我刚刚了解了使用 GCC 编译时可以使用的 __unused 标志,我了解的越多,我的问题就越多...

为什么编译时没有警告/错误?我特地告诉编译器我不会使用变量,这似乎很奇怪,然后当我使用它时,一切都照常进行。

- (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: 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)

因此,这个 GCC 属性是为了在您不使用变量时避免警告,并且在您使用声称未使用的变量时不触发警告


关于在上一个示例中将属性放在变量名之前或之后,两者都被接受并且在你的情况下是等效的:编译器只是出于兼容性目的而对那个位置宽容(就像你也可以同时编写 const int iint 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)

关于ios - __unused 标志行为/用法(带有 Objective-C 的 GCC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19736107/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com