Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:24:13+00:00 2026-06-10T07:24:13+00:00

I’m using Clang’s primitive-boxing feature to pack an enumeration member into NSNumber The Boxed

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:24:15+00:00Added an answer on June 10, 2026 at 7:24 am

    I consider this case as being described in the link you provided:

    Boxing a value of enum type will result in a NSNumber pointer with a
    creation method according to the underlying type of the enum, which
    can be a fixed underlying type or a compiler-defined integer type
    capable of representing the values of all the members of the
    enumeration:

    typedef enum : unsigned char { Red, Green, Blue } Color;
    Color col => Red;
    NSNumber *nsCol = @(col); // => [NSNumber numberWithUnsignedChar:]
    

    but the details of the promotions in the libraries are not covered, and that is where the difference in expectations is introduced.

    -testA ends up calling +[NSNumber numberWithInt:]

    -testB ends up calling +[NSNumber numberWithUnsignedInt:]

    So the abstracted ‘promotion’ you see is because CFNumber (and consequently NSNumber) do not actually support unsigned values at this time (see constants of CFNumberType enums) — based on the output you are seeing, one would then assume NSNumber implementations 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, NSNumber declares 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 a uint16_t typed enum will be stored as a 32 bit int (via numberWithUnsignedShort:), and a int32_t is also a 32 bit int (via numberWithInt:). Although, in the case of -testA the 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.