Re: typedef enums are now unsigned?!
Re: typedef enums are now unsigned?!
- Subject: Re: typedef enums are now unsigned?!
- From: Clark Cox <email@hidden>
- Date: Sat, 7 Mar 2009 23:26:21 -0800
On Fri, Mar 6, 2009 at 5:53 PM, Nick Zitzmann <email@hidden> wrote:
> So I did RTFM, which in this case is "The C Programming Language" (ANSI
> edition), and I'm wondering...
>
> While I was working on a project today, I found something strange. We have
> an ObjC method in the project that returns a value of typedef enum when
> called. TCPL doesn't say anything about typedef enums, but it does say that
> enums are always of type int,
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-deļ¬ned,
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.
> so I thought this meant it was OK to return -1
> in the method if an error occurs.
The only way that you can guarantee that you are allowed to assign -1
to an enum type is to have one of its members have a value less than
or equal to -1.
> Well, on PPC and X86 the method was indeed returning -1 when an error
> occurred, but on PPC64 and X86-64, it was instead returning 2^32-1, which is
> SOP for a method returning an unsigned int, not an int. This, of course,
> threw off various checks for -1, and flew in the face of what it says in
> TCPL.
>
> I was able to work around this by changing the results of an error from -1
> to INT_MAX, but this raises several questions: Why did this change in the
> 64-bit compilers, where is this change documented, and how can this behavior
> be controlled?
This change doesn't have to be documented, because you were relying on
something that wasn't guaranteed by the language in the first place.
--
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