a.f(exp1).f(exp2), what's the evaluation order of exp1 and exp2?

Q

Qi

Hi all,

Below is some code to test the evaluation order in a function
calling chain.
On both VC and GCC, b(2) is called before b(1).
(but f(b(1)) is called before f(b(2)), as I expected).

My question is, is there any standard about the evaluation order
of b(1) and b(2)?
I guess it's up to the compiler to decide the order so the order
is not guaranteed left to right or right to left?
Or to say, can, or can not, I give any assumption on the order?

Thanks


struct A
{
A & f(int n) {
return *this;
}
};

int b(int n)
{
cout << n << endl;

return n;
}

void test()
{
A a;
a.f(b(1)).f(b(2));
}
 
Q

Qi

This is covered by the C++ standard, which says (in C++2003 this is in
5.2.2/8):

"The order of evaluation of arguments is unspecified. All side effects of
argument expression evaluations take effect before the function is
entered".

Thanks.
Get clear now.
 

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,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top