operator++ execution order differs between scalar types and classes

  • Thread starter Zeguudo Zegudissimo
  • Start date
Z

Zeguudo Zegudissimo

Hi,

Can anyone explain please why gcc (and possibly other compilers too)
treats scalar types and classes differently in terms of the order in
which their operator++ is executed?

For example, the following code:

int x(0), y(0);
y = x++ + x++;
printf("x=%d y=%d\n", x, y);

yields output:
x=2 y=0

as if the code had been:

int x(0), y(0);
y = x + x;
x++;
x++;
printf("x=%d y=%d\n", x, y);


On the other hand, if I replace 'int' with a class that has all
relevant ctors and operators (including operator++) defined:

MyIntClass x(0), y(0);
y = x++ + x++;
printf("x=%d y=%d\n", x, y);

the exactly the same code results in different output:

x=2 y=1

as if the code had been:

int x(0), y(0);
y = x;
x++
y += x;
x++;
printf("x=%d y=%d\n", x, y);


The latter is what I would expect from scalar types too (considering C+
+ operator precedence rules). What causes the difference? Is it a bug
in gcc or it is intended by C++ standard?
 
F

Francesco S. Carta

Hi,

Can anyone explain please why gcc (and possibly other compilers too)
treats scalar types and classes differently in terms of the order in
which their operator++ is executed?

For example, the following code:

int x(0), y(0);
y = x++ + x++;

Same object modified twice without any intervening sequence point, hence
undefined behavior in C++ (and in C too, for that matter), so all the
bets are off. The compiler could do anything and everything.
printf("x=%d y=%d\n", x, y);

yields output:
x=2 y=0

as if the code had been:

int x(0), y(0);
y = x + x;
x++;
x++;
printf("x=%d y=%d\n", x, y);


On the other hand, if I replace 'int' with a class that has all
relevant ctors and operators (including operator++) defined:

MyIntClass x(0), y(0);
y = x++ + x++;
printf("x=%d y=%d\n", x, y);

the exactly the same code results in different output:

x=2 y=1

as if the code had been:

int x(0), y(0);
y = x;
x++
y += x;
x++;
printf("x=%d y=%d\n", x, y);


The latter is what I would expect from scalar types too (considering C+
+ operator precedence rules). What causes the difference? Is it a bug
in gcc or it is intended by C++ standard?

Intended as "whatever", the C++ standard doesn't care about what
implementations do in case of undefined behavior.
 
G

gwowen

y = x++ + x++;

As soon as you do this, the compiler can do what the hell it likes,
regardless of what you think *should* happen. If you modify anything
twice (or more) without an intervening sequence point (which means,
approximately, "within the same statement"[0]) the results are not
defined. Don't do it.

[0] Pedants: I know this is not accurate, but its close enough for
beginners.
 
F

Francesco S. Carta

Same object modified twice without any intervening sequence point, hence
undefined behavior in C++ (and in C too, for that matter), so all the
bets are off. The compiler could do anything and everything.

Just to clear out another thing that seems not clear from your question:
the postfix ++ operator returns the value of its operand _before_
performing the increase.

For instance:

int x, y, z;
x = y = z = 0;
x = y++ + z++;

after executing the above (which is a well defined chunk of code), the
following will be all true:

x == 0
y == 1
z == 1

that's because this line:

x = y++ + z++;

must have the same effect of these three separate statements:

x = y + z;
++y;
++z;

Using the prefix ++ operator, this line:

x = ++y + ++z;

will yield:

x == 2
y == 1
z == 1

because the prefix ++ operator returns the value of its operand _after_
performing the increase.

Any decent C++ reference or FAQ will explain you all of the above to
greater extent.

Seeing that you post a call to printf() in c.l.c++, I think you could
take advantage of throwing an eye to:

http://www.parashift.com/c++-faq/

in order to correctly walk on the C++ path.
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top