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?
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?