Jump to content
  • Advertisement
Sign in to follow this  
Japanese

strange but it works

This topic is 4446 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i++++; fails and I understand why but ++++i; works, shouldn't it fail also We are still trying to increment non-lvalue....what say?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Japanese
i++++; fails and I understand why

but ++++i; works, shouldn't it fail also

We are still trying to increment non-lvalue....what say?


Fomr the Holy One, section 5.3.2 (Increment and decrement), §1:
Quote:
The operand of prefix ++ is modified by adding 1 <...>. The operand shall be a modifiable lvalue. <...> The value is the new value of the operand; it is a lvalue. <...>


Emphasis is mine.

HTH,

Share this post


Link to post
Share on other sites
Plz see this

http://groups.google.com/group/comp.lang.c/browse_thread/thread/8a430046f8ba405c/8672a98fab1e946b?lnk=st&q=why+i%2B%2B%2B%2B+is+invalid&rnum=2&hl=en#8672a98fab1e946b

Share this post


Link to post
Share on other sites
Ok, I see a non-hyperlinked URL with no indication why you would post it, or for that matter why I would bother visiting it. Do you have some problem following the discussion in that thread? Do you believe that what that thread said is in contradiction with what was said in this thread? Or do you just like posting links to random URLs?

Share this post


Link to post
Share on other sites
No. It's not a random link. If you read the post of the last person in that link it contradicts what you say.

Here it is
Quote:

No, it isn't. This will not even compile. (Well, technically,
theoretically, it might. As long as the error is diagnosed, the
compiler can do whatever it likes with this broken code. But in the
real world, compilers will cease compilation.) The expression ++++i
parses as ++(++i), which, of course, is nonsensical. You cannot
increment a non-lvalue. (An lvalue is an object. i is an object, and
therefore an lvalue. But ++i is not an object, and therefore not an
lvalue. Thus it cannot be applied to the ++ operator again.)
Similarly, i++++ is an error.

Share this post


Link to post
Share on other sites
His basic argument is ok, but the definition of lvalue is messed up. '++i' increments i and yields the *new* value, by reference; thus it refers again to i, which is still a new value. '++i' increments i, yielding the *old* value; since 'i' is changed, this old value must necessarily be yielded by a temporary, and temporaries aren't lvalues.

Share this post


Link to post
Share on other sites
Quote:
Original post by Japanese
No. It's not a random link. If you read the post of the last person in that link it contradicts what you say.

Let's see here: all the compilers I know accept ++++i and the C++ Standard says that ++i is an lvalue. I'm going to go out on a limb and say that the guy you quoted is wrong.

Quote:
Original post by Zahlman
... and temporaries aren't lvalues.

Some temporaries can be lvalues. Ex: the return value of a function that returns a non-const class type by value is both an lvalue and a temporary. For this reason i++++ can be a valid expression if i is of a class type.

Share this post


Link to post
Share on other sites
Note that while ++++i; is syntactically valid it is only semantically valid if i is an instance of a user-defined type with an operator++(), due to sequence points, as discussed briefly in GotW #78.

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
For this reason i++++ can be a valid expression if i is of a class type.


that depends on how the operator is overloaded in the class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!