Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NEWBIE question: Is this a bug in my compiler?

52 views
Skip to first unread message

mehrdad ghassempoory

unread,
Jun 22, 2016, 11:07:15 AM6/22/16
to
I have a very simple program shown below, I use gnu c++ version 4.8.4.
I expect the program to print:

0 2

but I get

0 0

however, if I use clang++ version 3.5, I get what I expected:

0 2

Is this a bug in gnu c++ or maybe I am confused about the way <<
operator works?
++++++++++++++++++++++++++++++++++++++++++++++++++
#include <iostream>

using namespace std;

int func(int& x)
{
x=2;
return 0;
}

int main()
{
int x;

x=0;
cout << func(x) << " " << x << endl;

return 0;
}
---------------------------------------------------

kfran...@gmail.com

unread,
Jun 22, 2016, 9:32:05 PM6/22/16
to
Hi Mehrdad!

On Wednesday, June 22, 2016 at 11:07:15 AM UTC-4, mehrdad ghassempoory wrote:
> I have a very simple program shown below, I use gnu c++ version 4.8.4.
> I expect the program to print:
>
> 0 2
>
> but I get
>
> 0 0
>
> however, if I use clang++ version 3.5, I get what I expected:
>
> 0 2
>
> Is this a bug in gnu c++

This is not a bug in g++. Both g++ and clang++ are right,
because (see below) your program has unspecified behavior,
which means that the standard gives the compiler a choice,
and g++ and clang++ are (apparently) making different choices.

> or maybe I am confused about the way <<
> operator works?

Because the library uses "operator overloading" to let
"<<" work with streams, "<<", in your case, is just a
function that takes two arguments.

The order of evaluation of arguments to functions is
unspecified by the standard, so either the first or the
second argument may be evaluated first. (This is not
changed by the fact that your function call is written
as an infix operator.)

> ++++++++++++++++++++++++++++++++++++++++++++++++++
> #include <iostream>
>
> using namespace std;
>
> int func(int& x)
> {
> x=2;
> return 0;
> }
>
> int main()
> {
> int x;
>
> x=0;
> cout << func(x) << " " << x << endl;

To see what is happening, let's rewrite this with explicit
function-call notation:

operator<< (
operator<< ( // this is the relevant function call
operator<< (
operator<< ( cout, func(x) ),
" ",
),
x
),
endl
);

The second "operator<<" (noted by the comment) is called
with two arguments. The first is the result of evaluating
the third "operator<<", and the second is x, itself.

If you evaluate the first argument first (as, apparently,
does clang++) you drill down and call func(x) which sets
x to 2 (and returns zero). You then evaluate x, which is
now 2. So you print out "0 2".

If you evaluate the second argument first (as, apparently,
does g++), you evaluate x, which is 0, and then call func(x),
which sets x to 2 -- but this does not change the value of
0 from the earlier evaluation of x -- and returns 0. So you
print of "0 0".

> ...

To recap: Both results, "0 2" and "0 0" are perfectly legal,
because they depend upon in which order the two arguments
of operator<< are evaluated, and this is explicitly stated
to be unspecified in the standard, with the choice left up
to the compiler.


Good luck.


K. Frank
0 new messages