From: Anubhav on
int& f(){
static int x = 0;
x++;
return x;
}

int main(){
int x = 0;
f() += 1;
f() = f() + 1;

cout << f();
}

What should be the output of the code shown?

VS gives 5 and gcc gives 6.

Regards,
Dabs.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Stefan van Kessel on
The code you posted relies upon the order of evaluation within the statement "f() = f() + 1;". Either the first or the second invocation of f() has to come first. If the first comes first, you have x = 4 + 1; (x is now 5) If the second comes first you have essentially ++x = 3 + 1. (x is now 4) Since I'm pretty sure the order of evaluation here is undefined, the behavior also is undefined. If you've got a copy of the standard at hand, look at 1.9.15.

On 8/7/2010 12:18 PM, Anubhav wrote:
> int& f(){
> static int x = 0;
> x++;
> return x;
> }
>
> int main(){
> int x = 0;
> f() += 1;
> f() = f() + 1;
>
> cout<< f();
> }
>
> What should be the output of the code shown?
>
> VS gives 5 and gcc gives 6.
>
> Regards,
> Dabs.
>


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bo Persson on
Anubhav wrote:
> int& f(){
> static int x = 0;
> x++;
> return x;
> }
>
> int main(){
> int x = 0;
> f() += 1;
> f() = f() + 1;
>
> cout << f();
> }
>
> What should be the output of the code shown?
>
> VS gives 5 and gcc gives 6.
>
> Regards,
> Dabs.

We don't know. In the third statement, the f's could be called in any
order. Either would be correct.


Bo Persson



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
Anubhav wrote:

> int& f(){
> static int x = 0;
> x++;
> return x;
> }
>
> int main(){
> int x = 0;
> f() += 1;
> f() = f() + 1;
>
> cout << f();
> }
>
> What should be the output of the code shown?
>
> VS gives 5 and gcc gives 6.
>

The output is unspecified, because the orderof value computation of the
operands of op= is unspecified. Lvalue computation of the two f()'s are
unsequenced. So how the "f()" value in "f() + 1" gets computed to can be
either

f() += 1; // after which f.x == 2
f() = f() + 1 // after which f.x == 5 or 4
cout << f() // after which f.x == 6 or 5


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Prasoon Saurav on
On Aug 7, 3:18 pm, Anubhav <rkld...(a)gmail.com> wrote:
> int& f(){
> static int x = 0;
> x++;
> return x;
>
> }
>
> int main(){
> int x = 0;
> f() += 1;
> f() = f() + 1;
>
> cout << f();
>
> }
>
> What should be the output of the code shown?
>
> VS gives 5 and gcc gives 6.
>
> Regards,
> Dabs.
>

As per my opinion your code invokes Undefined Behaviour because the
order of evaluation of arguments of assignment operator is unspecified
and both right hand side and left hand side f() refer to the same
static variable. So indirectly you are trying to modify the variable
more than once between two sequence points.

Somebody please correct me if I am wrong.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 |  Next  |  Last
Pages: 1 2 3
Prev: C/C++ query
Next: Set implementation in STL