Sign in to follow this  

strange but it works

This topic is 4201 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

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

This topic is 4201 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this