473,385 Members | 1,712 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,385 software developers and data experts.

Evaluation order of comma operator

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

Dec 15 '06 #1
1 2640

mr*******@gmail.com 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

Dec 15 '06 #2

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

Similar topics

8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
2
by: Jan Engelhardt | last post by:
Hi, I was told that order of evaluation is unspecified for functions, i.e. int f = 0; print_results(modify(&f), modify(&f), modify(&f)); where i.e. modify() increases f by one. In my case...
13
by: subnet | last post by:
What does the standard say about this: #include <stdio.h> void somefunc(int a, int b, int c) { printf("%d %d %d\n", a, b, c); } int main(void) { int i = 5;
25
by: haroon | last post by:
Consider this function, void fun (int i, int j) { printf ("i = %d : j = %d", i, j); } I call it like this: fun (n++, n);
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
7
by: Peter | last post by:
I know the order of construction of member and base class objects. My question is the following: Is the order of evaluation of argument lists for these constructors also defined? E.g. can I...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.