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