Note that there are some explanatory texts on larger screens.

plurals
  1. POBoxing the same enum member produces a larger integer when it's passed to a method
    text
    copied!<p>I'm using Clang's primitive-boxing feature to pack an enumeration member into <code>NSNumber</code></p> <p>The <a href="http://clang.llvm.org/docs/ObjectiveCLiterals.html#objc_boxed_expressions" rel="nofollow">Boxed Enums section</a> of the Clang doc about this says that the compiler boxes enumeration members into integers, unless the type is specified.</p> <p>Funnily enough, I get different sizes of integers depending on the way I'm passing the enumeration member to the method. I've been able to isolate the case down to the following code</p> <pre><code>typedef enum _MyEnum { MyEnumMember1 = 1000 } MyEnum; - (void)testEnumerationBoxing { NSNumber *numberA = [self testA]; NSNumber *numberB = [self testB:MyEnumMember1]; CFNumberType numberTypeA = CFNumberGetType((__bridge CFNumberRef) numberA); CFNumberType numberTypeB = CFNumberGetType((__bridge CFNumberRef) numberB); NSLog(@"CF Number type for A: %lu; B: %lu", numberTypeA, numberTypeB); } - (NSNumber *)testA { return @(MyEnumMember1); } - (NSNumber *)testB:(MyEnum)enumMember { return @(enumMember); } </code></pre> <p>The console output is </p> <blockquote> <p>CF Number type for A: 3; B: 4 </p> </blockquote> <p>(the first one is <code>kCFNumberSInt32Type</code>, the second one is <code>kCFNumberSInt64Type</code>)</p> <p>If I change declaration to <code>typedef enum _MyEnum : int</code> I see the same result for both: <code>kCFNumberSInt32Type</code>.</p> <p>Why does the size of the integer differ between the two methods of boxing?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload