• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: typedef enums are now unsigned?!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: typedef enums are now unsigned?!


  • Subject: Re: typedef enums are now unsigned?!
  • From: Clark Cox <email@hidden>
  • Date: Sun, 8 Mar 2009 12:06:59 -0700

On Sun, Mar 8, 2009 at 12:03 PM, Clark Cox <email@hidden> wrote:
> On Sun, Mar 8, 2009 at 10:36 AM, Darcy Brockbank <email@hidden> wrote:
>>
>> Clark Cox wrote:
>>>
>>> This is simply incorrect. From the C standard:
>>>
>>> "Each enumerated type shall be compatible with char, a signed integer
>>> type, or an unsigned integer type. The choice of type is
>>> implementation-defined,
>>> but shall be capable of representing the values of all the members of the
>>> enumeration."
>>>
>>> That is, the underlying type can be *any* integer type, not just int.
>>
>> You missed the relevant parts of the standard you're quoting. What you are
>> referring to are the options available to the compiler after it has
>> considered the
>> enum in question.
>>
>> Valid values of the enum are explicitly stated in items (2) and (3) which
>> are
>> directly above item (4) which you are quoting. The relevant section is
>> 6.7.2.2
>> if anyone would like to read.
>>
>> Item (2):
>>
>> "The expression that defines the value of an enumeration constant shall be an
>> integer
>> constant expression that has a value representable as an int. "
>>
>> Note: *not* an unsigned integer, but an int. -1 is a valid choice in an
>> enum.
>>
>> Item (3):
>>
>> "The identifiers in an enumerator list are declared as constants that have
>> type int and
>> may appear wherever such are permitted."
>
>
> [snip]
>
> The enumeration "constants" are of type int, the enumeration "type" is not.
>
>>
>> Note: enum type is "int". Period.
>>
>> The salient portion of Item (4) is:
>>
>> "The choice of type is implementation-defined, *****but***** shall be capable
>> of
>> representing the values of all the members of the enumeration."
>
> If -1 is not a member, then this doesn't apply.

To put this more clearly:

Given:

enum Foo {
  A = 1,
  B = 2
};

You can make the following assumptions:

sizeof(A) == sizeof(int)
sizeof(B) == sizeof(int)

You cannot assume that enum Foo is signed, nor can you assume that it
is any larger than a char.

--
Clark S. Cox III
email@hidden
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >typedef enums are now unsigned?! (From: Nick Zitzmann <email@hidden>)
 >Re: typedef enums are now unsigned?! (From: Clark Cox <email@hidden>)
 >Re: typedef enums are now unsigned?! (From: Darcy Brockbank <email@hidden>)
 >Re: typedef enums are now unsigned?! (From: Clark Cox <email@hidden>)

  • Prev by Date: Re: typedef enums are now unsigned?!
  • Next by Date: Re: Info.plist preprocessing in single-file tool? [SOLVED]
  • Previous by thread: Re: typedef enums are now unsigned?!
  • Next by thread: Working with debug and release versions of open source dynamic libraries
  • Index(es):
    • Date
    • Thread