473,414 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

weird behavior of algbraic evaluation in concatenated operators

hi there!

#include <iostream>
using namespace std;

void main(){

int i=0,
j=10;

//Case one (the 'common' case):
cout<< "i == " << i << endl
<< "j == " << j << endl;

//Case two (the 'oddity'):
//Should print 1 and 11 (but will not evaluate the former expression
// before the latter, although it is printing in the correct order.
cout<< "(i+=1) == " <<(i+=1)<< endl
<< "(j+=i) == " <<(j+=i)<< endl;
}

The ostream's operator<<() method is defined as to be passed only two
arguments a time, one of which must be the class, which is being
returned to allow concatenation.

(cout<<expr1)<<expr2;

But it seems as if the whole set of expressions is being evaluated,
_before_ becoming the arguments of the operator methods and, in
addition, is being evaluated from the right to the left (from the
stack?).

Is this a microsoft-specific way of dealing with concatenation?

Havv ei got zee opchions wronk?
Jul 22 '05 #1
4 1087
dejan budimir wrote:
hi there!

#include <iostream>
using namespace std;

void main(){
main() must return int.

int i=0,
j=10;

//Case one (the 'common' case):
cout<< "i == " << i << endl
<< "j == " << j << endl;

//Case two (the 'oddity'):
//Should print 1 and 11 (but will not evaluate the former expression
// before the latter, although it is printing in the correct order.
cout<< "(i+=1) == " <<(i+=1)<< endl
<< "(j+=i) == " <<(j+=i)<< endl;
This invokes undefined behaviour because both i and j are modified twice
without a sequence point in between. In theory, anything can happen.
}

The ostream's operator<<() method is defined as to be passed only two
arguments a time, one of which must be the class, which is being
returned to allow concatenation.

(cout<<expr1)<<expr2;

But it seems as if the whole set of expressions is being evaluated,
_before_ becoming the arguments of the operator methods and, in
addition, is being evaluated from the right to the left (from the
stack?).
Probably, but the C++ standard doesn't state any order of evaluation.
Is this a microsoft-specific way of dealing with concatenation?

Havv ei got zee opchions wronk?


--
"Time flies like an arrow. Fruit flies like a banana."
- Groucho Marx

Jul 22 '05 #2
thanks! that really was fast.
would be great to know what it actually is that "goes wrong", though.
but, since i'll have to learn assembly anyway, i guess it's just a
matter of banana.

have a nice one!
Jul 22 '05 #3
dejan budimir wrote:

thanks! that really was fast.
would be great to know what it actually is that "goes wrong", though.


What "goes wrong": The way you wrote your program.
The C++ language simply does not define what should happen in this case.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
Karl Heinz Buchegger wrote:
dejan budimir wrote:

thanks! that really was fast.
would be great to know what it actually is that "goes wrong", though.


What "goes wrong": The way you wrote your program.
The C++ language simply does not define what should happen in this case.

yes, thank you.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Karlo Basic | last post by:
Greetings! I'm wondering how do the expressions in the following piece of code evaluate and why: #include <iostream> using namespace std; int main() { int n = 5, p;
2
by: bq | last post by:
Hello, This post is really two questions. Question 1: What is the current status on a revision of ISO C++, specifically regarding plans for overloading composite operators? Some people in this...
2
by: Todd A. Anderson | last post by:
Hi...any help would be appreciated. Kind of a weird scenario but I have foo's that point to bars and whenever anybody tries to use a foo as a pointer I want it typed as a baz * and likewise if...
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
5
by: Jim Strathmeyer | last post by:
So I'm having some weird problems with file output. If I try to boil this problem down to a small, simple program to just show what problems I'm having, I can't get the same problematic behavior. I...
3
by: ashish.dobhal | last post by:
i=0; printf("%d %d",i++,i); I am writing the above piece of code and executing it on different platforms (cygwin, solaris and linux). On cywin and linux the output is "0 0", whereas on...
10
by: Jonathan | last post by:
Hi all, I'm having an argument with a co-worker about the difference between the & and && operators when applied to boolean operands in C#. His point of view is that the expression (false &...
12
by: Neroku | last post by:
I don't know if the following expression is UB: i=2; x = (i=3) * i; Since in C, evaluation order is unspecified, this expression is 'at least' unspecified, since we don't know which operand...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.