Mixing post-(inc/dec)rement on the same variable

R

Ravi

In C++ (and also in C), if I write:

++x--
++(x--)

i get the error: lvalue required as increment operand

However (++x)-- compiles. I am confused.
 
A

Alf P. Steinbach /Usenet

* Ravi, on 19.07.2010 15:51:
In C++ (and also in C), if I write:

++x--
++(x--)

i get the error: lvalue required as increment operand

Which means at least your compiler parses the first as if it were the second.

Not sure what the standards say, because it does not matter.

For you should never modify a variable twice between sequence points, that's
Undefined Behavior.

However (++x)-- compiles. I am confused.

No problem, just Don't Do It. :)

Technically, in C++98 prefix '++x' produces 'x' as an lvalue, i.e. a reference,
while 'x--' produces the original value as an rvalue, like 42; you can't change 42.

But the technical explanation doesn't matter because also '(++x)--' is UB, even
if it compiles.


Cheers & hth.,

- Alf
 
S

SG

* Ravi, on 19.07.2010 15:51:


Which means at least your compiler parses the first as if it were the second.
Not sure what the standards say, because it does not matter.

It does matter. Perhaps not with built-ins like int but for class-
types with overloaded operators. Postfix operators always bind
stronger than prefix operators. This is a simple rule one should keep
in mind. Another example: &x[5] is equivalent to &(x[5]) and not (&x)
[5].

Cheers!
SG
 
C

cpp4ever

In C++ (and also in C), if I write:

++x--
++(x--)

i get the error: lvalue required as increment operand

However (++x)-- compiles. I am confused.

Postfix operator returns a value and not an lvalue, whereas the prefix
operator will return an lvalue,for which the operator requires.

A word of warning with regard to these prefix/postfix operators, never
use them on a variable within an expression, if that variable appears
more than once in the expression.

// BAD b is used twice in left-hand expression
r = ((b++) + sqrt(a*a - 4*a*b))/(2*a);

// EVEN WORSE as the operator is used twice on b in one expression
r = (++b) + (++b);

Unfortunately, how these actually behave between sequence points is
compiler dependent, hence for the second example with b=3, will the
result be 7 or 8? Preincrement done during expression evaluation, or
before the expression evaluation, and this is compiler dependent.

IMHO it's better not to use them as part of any expression calculation.

HTH

cpp4ever
 
A

Alf P. Steinbach /Usenet

* SG, on 19.07.2010 16:23:
* Ravi, on 19.07.2010 15:51:


Which means at least your compiler parses the first as if it were the second.
Not sure what the standards say, because it does not matter.

It does matter. Perhaps not with built-ins like int but for class-
types with overloaded operators. Postfix operators always bind
stronger than prefix operators. This is a simple rule one should keep
in mind. Another example:&x[5] is equivalent to&(x[5]) and not (&x)
[5].

Well, when u phrase it that way... :)

I've probably encountered that summary rule before & forgotten it.

It's a good one.


Cheers,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top