<< question

R

Ralf Goertz

(second repost, there seem to be problems)

Hi,

this might be g++-related but it raises a general question.

#include <iostream>

using namespace std;


bool getValue(int& val){
val=int(42);
return true;
}

int main(){
int i(0);
cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)
}


Compiled with optimization (-O3) this yields the output
42 1 value 42

Without optimization it gives
42 1 value 0

I had expected
0 1 value 42

Do I have to read the line (*) from right to left? But that seems odd
since "endl" is at the end of the output.

Ralf
 
B

Bo Persson

Ralf said:
(second repost, there seem to be problems)

Hi,

this might be g++-related but it raises a general question.

#include <iostream>

using namespace std;


bool getValue(int& val){
val=int(42);
return true;
}

int main(){
int i(0);
cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)
}


Compiled with optimization (-O3) this yields the output


Without optimization it gives


I had expected


Do I have to read the line (*) from right to left? But that seems
odd since "endl" is at the end of the output.

You don't know from which end you have to read it - perhaps from the
middle out?

The language just doesn't say that the parameters have to be evaluated
in any specific order. Like you have seen, the compiler can make
different choices for different compiles.


Bo Persson
 
J

Juha Nieminen

Ralf said:
bool getValue(int& val){
val=int(42);
return true;
}

int main(){
int i(0);
cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)

Because your getValue() function has side effects, the results of that
last expression are undefined because there's more than one reference to
the value being modified in the same expression. This is because the
compiler is allowed to optimize that kind of expression in any way it
wants. The result is only guaranteed after the ';'.

If you want it to work "correctly", divide each reference to 'i' into
its own expression:

cout << i << " ";
cout << getValue(i);
cout << " value " << i << endl;
 
F

Fraser Ross

"Bo Persson"
The language just doesn't say that the parameters have to be evaluated
in any specific order. Like you have seen, the compiler can make
different choices for different compiles.

If you are meaning expressions when you said parameters then thats true.
If you meant the 2 function parameters of operator<< then that is
irrelevant.

Fraser.
 
S

Salt_Peter

(second repost, there seem to be problems)

Hi,

this might be g++-related but it raises a general question.

#include <iostream>

using namespace std;

bool getValue(int& val){
val=int(42);
return true;

}

int main(){
int i(0);
cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)

}

Compiled with optimization (-O3) this yields the output


Without optimization it gives


I had expected


Do I have to read the line (*) from right to left? But that seems odd
since "endl" is at the end of the output.

Ralf


You can read it upside down or any way you like. The language doesn't
specify the order of evaluation. Thats the programmer's
responsability. What result you get on this compiler may not match
what another might do or what another version might do.

Its up to you to supply the (required) sequence points.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top