__unused Flag Behavior/Usage (GCC with Objective-C)

前端 未结 2 466
暖寄归人
暖寄归人 2020-12-14 06:56

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

相关标签:
2条回答
  • 2020-12-14 07:24

    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)

    0 讨论(0)
  • 2020-12-14 07:26

    As of Xcode 7.3.1, there is currently no difference between:

    - (void)foo:(NSInteger)__unused myInt;// [syntax 1]
    - (void)foo:(NSInteger __unused)myInt;// [syntax 2]
    - (void)foo:(__unused NSInteger)myInt;// [syntax 3]
    

    But the following doesn't work:

    - (void)foo:(NSInteger)myInt __unused;// [doesn't work]
    

    For usage on this, Apple recommends first syntax. (information was partially taken from this answer)

    But there is difference between:

    __unused NSString *foo, *bar;  // [case a] attribute applies to foo and bar
    NSString *foo __unused, *bar;  // [case b] attribute applies to foo only
    
    NSString * __unused foo, *bar; // [case c] attribute applies to foo only
    NSString __unused *foo, *bar;  // [case d] attribute applies to foo and bar
    CFStringRef __unused foo, bar; // [case e] attribute applies to foo and bar
    

    If we want __unused to apply to all, syntax [a] is my personal best as it leaves no ambiguity.

    If we want __unused to apply to one, syntax [b] is my personal best as it leaves no ambiguity.

    The latter three solutions are acceptable but confusing in my opinion. (information was partially taken from this answer)

    0 讨论(0)
提交回复
热议问题