Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Evaluation order of comma operator

Reply
Thread Tools

Evaluation order of comma operator

 
 
mr.gsingh@gmail.com
Guest
Posts: n/a
 
      12-15-2006
My program looks something like this:

int x = 23;
if ( (func(x), func1(x), func2(x)) <= (foo(x), foo1(x), foo2(x)))
{
std::cout << "............";
}
else
{
std::cout<< "..............";
}

where func(x), func1(x) and func2(x) return reference to 'x' and foo(),
foo1() and foo2() return integer. I understand this is bad programming
but I am doing this to understand the execution semantics of comma
operator.

g++ gives me the follwoing execution order:

Function foo.
Function foo1.
Function func.
Function func1.
Function func2.
Function foo2.

This looks weird to me. I was expecting s'thing like

func, func1, func2, foo, foo1, foo2

Any explanations for this behavior will be highly appreciated.

Thanks
Gagan

 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      12-15-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> My program looks something like this:
>
> int x = 23;
> if ( (func(x), func1(x), func2(x)) <= (foo(x), foo1(x), foo2(x)))
> {
> std::cout << "............";
> }
> else
> {
> std::cout<< "..............";
> }
>
> where func(x), func1(x) and func2(x) return reference to 'x' and foo(),
> foo1() and foo2() return integer. I understand this is bad programming
> but I am doing this to understand the execution semantics of comma
> operator.
>
> g++ gives me the follwoing execution order:
>
> Function foo.
> Function foo1.
> Function func.
> Function func1.
> Function func2.
> Function foo2.
>
> This looks weird to me. I was expecting s'thing like


It's one allowed possibility.

> func, func1, func2, foo, foo1, foo2


That's another allowed possibility.

> Any explanations for this behavior will be highly appreciated.


The compiler is required to evaluate the left-hand operand of the comma
operator before the right hand. So in your code, foo(x) must be
evaluated before foo1(x), which must be evaluated before foo2(x). And
similar for func(x), funx1(x) and func2(x). Other than that there is no
requirement or guarantee on the order of evaluation of your functions.
So

foo, foo1, foo2, func, func1, func2
would be equally acceptable.

foo, foo1, func, func2, foo2, func1
would not be allowed. But only because func2 happens before func1.
Everything else about that order is possible.

Gavin Deane

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Operator evaluation order boltar2003@boltar.world C++ 16 05-15-2011 01:24 PM
Evaluation order and operator precedance Frédéric C++ 4 12-06-2007 04:09 PM
Parameter evaluation order on operator invocations andreas ames C++ 3 10-23-2006 04:34 PM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM
Order of evaluation -- clash w/sequentiel expression operator Jan Engelhardt C Programming 3 08-20-2003 09:07 PM



Advertisments