• 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: (no subject)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: (no subject)


  • Subject: Re: (no subject)
  • From: Steve Checkoway <email@hidden>
  • Date: Fri, 24 Feb 2006 15:41:27 -0800


On Feb 24, 2006, at 2:17 PM, Brant Sears wrote:

Hi. I have a question about some code that is OK in gcc 3.3, but gcc 4 doesn't like.

for (NSCellAttribute ca = NSCellDisabled; ca <=NSCellAllowsMixedState; ++(int)ca) {
[myCell setCellAttribute:ca to:[modelCell cellAttribute:ca ]];
}


The warning that I'm getting states that "ca" is not really an lvalue. I don't understand this. Why isn't "ca" an lvalue in the above code fragment?
It's not an lvalue because you're casting it to an int. If you're using c++, you can cast it to an int& but there's a comment in some code I have that says:

/*
* Safely add an integer to an enum.
*
* This is illegal:
*
* ((int&)val) += iAmt;
*
* It breaks aliasing rules; the compiler is allowed to assume that "val" doesn't
* change (unless it's declared volatile), and in some cases, you'll end up getting
* old values for "val" following the add. (What's probably really happening is
* that the memory location is being added to, but the value is stored in a register,
* and breaking aliasing rules means the compiler doesn't know that the register
* value is invalid.)
*
* Always do these conversions through a union.
*/


and the code is (more or less)
template<typename t>
static inline void enum_add( T &val, int iAmt )
{
	union conv { T value; int i; } c;
	c.value = val;
	c.i += iAmt;
	val = c.value;
}

Try just using an int instead of NSCellAttribute and casting it to NSCellAttribute if the compiler complains.

Is it just philosophically wrong to try to iterate through an enum like this?
Yes. If you really want you can use the above code with enum_add( ca, 1 ).

- Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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: 
 >(no subject) (From: "Brant Sears" <email@hidden>)

  • Prev by Date: Re: Runs on PPC, crashes on Intel
  • Next by Date: Minor but amazingly annoying bug: text coloring
  • Previous by thread: (no subject)
  • Next by thread: (no subject)
  • Index(es):
    • Date
    • Thread