Reputation: 5947
I know the order that function parameters are evaluated is unspecified in C++, see below,
// The simple obvious one.
callFunc(getA(),getB());
Can be equivalent to this:
int a = getA();
int b = getB();
callFunc(a,b);
Or this:
int b = getB();
int a = getA();
callFunc(a,b);
This is perfect fine & I think most ppl know this.
But I have tried VC10, gcc 4.72 and they all evaluate b first (from right to left), meaning b got pushed into stack frame first then a.
I am just wondering which c++ compiler should I try to make the code above to evalute a first ? So a got pushed to stack before b.
Thanks
Upvotes: 3
Views: 202
Reputation: 126857
The parameter evaluation order substantially depends from the calling convention used for calling the given function - if parameters are pushed on the stack RTL it's usually more convenient to elaborate the rightmost parameters first.
According to this table, on x86 the only calling convention available on IA32 with LTR parameter order on the stack is fastcall
on Borland, that however passes the first three integer/pointer parameters in registers. So you should write a function that takes more than three integers, mark it as fastcall
and compile it with a Borland compiler; in that case probably the other parameters besides the first three should be evaluated in LTR order.
Going on other platforms probably you'll find other calling conventions with LTR parameter passing (and so probably LTR parameters evaluation).
Notice that the parameter passing order <=> parameter evaluation order are logically bound, but if for some reason the compiler finds that it's better to evaluate some parameter before the others there's nothing in the standard preventing it to do so.
Upvotes: 7