Question about postfix ++ precedence

M

mrpermuted

Hello,

Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.

Thanks,
Glen
 
V

Victor Bazarov

Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.

Precedence controls the order of application. Postfix has side effect
(changing the value of the operand) which is only observable at the
next sequence point. The result of the postfix increment or decrement
is the value of the operand _before_ it's applied. No contradiction.

V
 
O

Old Wolf

Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.

Because precedence and order-of-evaluation are unrelated.
 
M

mrpermuted

Because precedence and order-of-evaluation are unrelated.

Ok... what's the difference, then?

Glen
 
V

Victor Bazarov

Ok... what's the difference, then?

Glen

If you don't concern yourself with such things like overflow,
precedence determines the logic, while order of execution is
at the liberty of the optimizer.

unsigned a, b, c; // no overflow
...
unsigned d = a + b - c;

'+' and '-' have the same precedence and while they are usually
grouped from left to right, the compiler is free to evaluate the
above expression as if it were written

unsigned d = a + (b - c);

or

unsigned d = (a - c) + b;

In that context we say that the order of execution is unspecified.

That's how I see it anyway. A proper book on on computer science
must explain it better.

V
 
Z

Zara

Hello,

Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.

Thanks,
Glen

With an example:

a=b+c++;

Operator precedence tells the compiler to interpret that line as

a= b+ (c++); // YES

and not something like

a=(b+c)++; // NO

But order of evaluation is unspecified. The expression could be
evaluated as:

Either:
-- calculate b+c
-- increment c
-- store calculation in a

Or:

-- calculate b+c
-- store calculation in a
-- increment c

whichever fits better the compiler, depending on lots of things, such
as optimization level, surrounding operations... Same compiler may
compile the expression in many different ways, as long as the
calculation is correct.

Regards,

Zara
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,271
Latest member
BuyAtenaLabsCBD

Latest Threads

Top