I’m using Clang’s primitive-boxing feature to pack an enumeration member into NSNumber
The Boxed Enums section of the Clang doc about this says that the compiler boxes enumeration members into integers, unless the type is specified.
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
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);
}
The console output is
CF Number type for A: 3; B: 4
(the first one is kCFNumberSInt32Type, the second one is kCFNumberSInt64Type)
If I change declaration to typedef enum _MyEnum : int I see the same result for both: kCFNumberSInt32Type.
Why does the size of the integer differ between the two methods of boxing?
I consider this case as being described in the link you provided:
but the details of the promotions in the libraries are not covered, and that is where the difference in expectations is introduced.
-testAends up calling+[NSNumber numberWithInt:]-testBends up calling+[NSNumber numberWithUnsignedInt:]So the abstracted ‘promotion’ you see is because
CFNumber(and consequentlyNSNumber) do not actually support unsigned values at this time (see constants ofCFNumberTypeenums) — based on the output you are seeing, one would then assumeNSNumberimplementations simply promote to the next signed type capable of representing all values in the case of an unsigned initializer of constructor — apparently without testing the value to see if any ‘width minimization’ can be applied.Of course,
NSNumberdeclares constructors and initializers which take unsigned types as parameters, but the internal representation of an unsigned integer is actually stored as a signed integer representation.The compiler appears to call appropriate/exact convenience constructors when promoting the boxed literal to an
NSNumber. For example auint16_ttyped enum will be stored as a 32 bit int (vianumberWithUnsignedShort:), and a int32_t is also a 32 bit int (vianumberWithInt:). Although, in the case of-testAthe value is also known, so a more appropriate constructor could also be called there — so the compiler is only width-minimizing based on type, not type and value. when the type of an enum is unspecified or specified as an unsigned type, then you may see promotions like this.