473,406 Members | 2,633 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,406 software developers and data experts.

Question regarding the << operator

Hi all,

I have a question regarding the << operator.
I wrote a simple queue class, which contains add(char), del() and empty() functions.
They works fine.
Now I want its output.
If I write the following way, it gives me the correct output.

while(!q1.empty()){ // This prints "abc"
cout<<q1.del();
}

However, if I write the following way, it gives me the wrong output sequence.
cout<< q.del() << q.del() << q.del(); // This prints "cba"

Can someone explain how the operator is working in these cases?
Please refer to the following main.
Thanks.

J

************************************************** ********************8
int main()
{
queue q;
q.add('a');
q.add('b');
q.add('c');

cout<< q.del() << q.del() << q.del(); // This prints "cba" <-- Wrong!
cout<<"\n";

queue q1;
q1.add('a');
q1.add('b');
q1.add('c');

while(!q1.empty()){ // This prints "abc"
cout<<q1.del();
}
cout<<"\n";
return 0;
}
Jul 22 '05 #1
5 1279
On 13 Jun 2004 20:43:40 -0700 in comp.lang.c++, ju**********@yahoo.com
(Juri) wrote,

However, if I write the following way, it gives me the wrong output sequence.
cout<< q.del() << q.del() << q.del(); // This prints "cba"


Note that the three calls to q.del() can occur in any order at the
convenience of the compiler, and the results held as temporary values.
After that, your operator<< does nothing remarkable.

If you want to control the order of evaluation, you must use some
language construct that introduces a "sequence point". The simplest
would be to make them three separate statements.

Jul 22 '05 #2
Juri wrote in news:74**************************@posting.google.c om in
comp.lang.c++:

I have a question regarding the << operator.
I wrote a simple queue class, which contains add(char), del() and
empty() functions. They works fine.
Now I want its output.
If I write the following way, it gives me the correct output.

while(!q1.empty()){ // This prints "abc"
cout<<q1.del();
}

However, if I write the following way, it gives me the wrong output
sequence. cout<< q.del() << q.del() << q.del(); // This prints "cba"


The compiler is free to make each of the three 'q.del()' calls in *any*
order it sees fit. The reason its doing it in what you may think of as
/reverse/ order is probably because its faster to do so.

The only solution is to write:

cout << q.del();
cout << q.del();
cout << q.del();

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
Juri wrote in news:74**************************@posting.google.c om in
comp.lang.c++:

I have a question regarding the << operator.
I wrote a simple queue class, which contains add(char), del() and
empty() functions. They works fine.
Now I want its output.
If I write the following way, it gives me the correct output.

while(!q1.empty()){ // This prints "abc"
cout<<q1.del();
}

However, if I write the following way, it gives me the wrong output
sequence. cout<< q.del() << q.del() << q.del(); // This prints "cba"


The compiler is free to make each of the three 'q.del()' calls in *any*
order it sees fit. The reason its doing it in what you may think of as
/reverse/ order is probably because its faster to do so.

The only solution is to write:

cout << q.del();
cout << q.del();
cout << q.del();

Rob.
--
http://www.victim-prime.dsl.pipex.com/


I disagree. The << associates from left to right. What you expected to
happen should have happened. This seems like a bug.
Jul 22 '05 #4
Partho Bhowmick wrote:
"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
Juri wrote in news:74**************************@posting.google.c om in
comp.lang.c++:

I have a question regarding the << operator.
I wrote a simple queue class, which contains add(char), del() and
empty() functions. They works fine.
Now I want its output.
If I write the following way, it gives me the correct output.

while(!q1.empty()){ // This prints "abc"
cout<<q1.del();
}

However, if I write the following way, it gives me the wrong output
sequence. cout<< q.del() << q.del() << q.del(); // This prints "cba"


The compiler is free to make each of the three 'q.del()' calls in *any*
order it sees fit. The reason its doing it in what you may think of as
/reverse/ order is probably because its faster to do so.

The only solution is to write:

cout << q.del();
cout << q.del();
cout << q.del();

Rob.
--
http://www.victim-prime.dsl.pipex.com/

I disagree. The << associates from left to right. What you expected to
happen should have happened. This seems like a bug.

No, it's the specified behaviour. The expression involving overloaded
operator << is not the same as built-in arithmetic shift. Consider:

blah << one << two << three;

is in fact

operator << (operator << ( operator << (blah, one) , two ) , three)
^^^^ a
^^^^^^^^^^^^^^^^^^^^^^^ b
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ c

The order of evaluation between (a) and (one) is _unspecified_, the
order of evaluation between (b) and (two) is _unspecified_ , and the
order of evaluation between (c) and (three) is _unspecified_. That
means that (one), (two), and (three) can be evaluated in _any_order_.

That's why if the results of their execution (side effects) depend on
the order of evaluation, it is not recommended to have them in the
same expression.

Victor
Jul 22 '05 #5
Partho Bhowmick wrote in news:gQlzc.85653$pg6.5724
@newssvr25.news.prodigy.com in comp.lang.c++:
The only solution is to write:

cout << q.del();
cout << q.del();
cout << q.del();

I disagree. The << associates from left to right. What you expected to
happen should have happened. This seems like a bug.


Association has nothing to do with it, association only effects how the
compiler interprets the expression:

a op b op c

If 'op' is left to right becomes: ((a op b) op c)
If 'op' is right to left becomes: (a op (b op c))

Simple example:

int i = 1;

int f( int j ) { return j; }

int main()
{
i = i << f( 1 ) << f( 2 );
}

The compiler can rewrite this as:

int main()
{
int r1 = f( 1 );
int r2 = f( 2 );

int r3 = i << r1;
i = r3 << r2;
}

or it could rewrite it like this:

int main()
{
int r2 = f( 2 );
int r1 = f( 1 );

int r3 = i << r1;
i = r3 << r2;
}

However the Association rules prevent the compiler from rewriting it as:

int main()
{
int r1 = f( 1 );
int r2 = f( 2 );

int r3 = r1 << r2;
i = i << r1;
// effectivly i = i << ( f( 1 ) << f( 2 ) );
}
Note that in this simple case, because every thing can be done in
registers, just about every compiler is going to do what we
"intuitivly" expect:

int main()
{
register int r = i;
r <<= f( 1 );
r <<= f( 2 );
i = r;
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

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

Similar topics

16
by: Axel Dahmen | last post by:
Hi, I always thought that "1em" equals one line height. But if I replace <br><hr><br> by <hr style="margin: 1em 0;">
4
by: velthuijsen | last post by:
I was trying out the binary operators and did the following: class mv { private: int* Storage; int StSize; public: mv(int Size = 0);
1
by: jy836 | last post by:
Hey again. Thank you to everyone who responded to my question in the "Newbie database question" thread. I've got a new problem, though. ;-) I linked to an Access 2000 database. This database has...
20
by: darklight | last post by:
Just picked up on c programming again and doing Kernighan & Ritchie c programming the question is this why does the program below not work; #include <stdio.h> /* count characters in input:...
10
by: onkar | last post by:
#include<stdio.h> int main(void){ printf("%d %d\n",32<<1,32<<0); printf("%d %d\n",32<<-1,32<<-0); <----------------------------------see here printf("%d %d\n",32>>1,32>>0); printf("%d...
35
by: Steve JORDI | last post by:
Hi, I'm trying to implement a singleton in PHP4 but it doesn't seem to work. The object is recreated each time I call it. The goal of the class is to keep a variable up to date. It's used to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.