• 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: James Walker <email@hidden>
  • Date: Fri, 06 Mar 2009 19:01:37 -0800

Nick Zitzmann 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, so I thought this meant it was OK to return -1 in the method if an error occurs.

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?

The authoritative FM is a language standard. The 2003 C++ standard says:

"An expression of arithmetic or enumeration type can be converted to an enumeration type explicitly. The value is unchanged if it is in the range of enumeration values of the enumeration type; otherwise the resulting enumeration value is unspecified."

I suppose that by "unspecified", it means implementation-defined, hence they can change it in 64 bit compilers and not document it.

If the enumeration is in your code, you can just add -1 to the enumeration, and you should be fine. I often put an explicit "invalid" enumeration constant in my enumerations for just such purposes.
--
James W. Walker, Innoventive Software LLC
<http://www.frameforge3d.com/>
_______________________________________________
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>)

  • Prev by Date: Working with debug and release versions of open source dynamic libraries
  • Next by Date: Double forward slashes in .xcconfig-defined build settings
  • Previous by thread: typedef enums are now unsigned?!
  • Next by thread: Re: typedef enums are now unsigned?!
  • Index(es):
    • Date
    • Thread