开发者

Do function calls within a function's argument list deepen the stack?

When calling F(argument_expression), is argument_expression evaluated before pushing the stack for F?

For example, when calling F(G(H(arg))), does the compiler first push the stack for H, evaluate H, pop, then push the stack for G, etc? Or does it first push the s开发者_如何学Pythontack for F, then for G, then for H, then pop back up 3 layers?

Also, is one way any faster than the other?


You've actually asked two orthogonal questions.

The concept of evaluation is pitched at the programmer's vantage point, and in this case, is well-defined by the C++ standard. Yes, the argument to a function is always evaluated before that function is called.

However, the standard does not specify how the stack should be managed. The compiler is free to take either of the approaches that you've suggested. Of course, it may take a third option, which is to directly inline one or more of your nested functions. There are probably other alternatives.


The parameters are fully evaluated before the called function can run, per spec.

I.e.: In your example, H(arg) will be fully evaluated before G(result of H(arg)) can run, etc.

At any given time you'll have one-level depth of the stack.


No, they are evaluated sequentially (only one frame-deep in this case).


It depends what do you mean by pushing H,G,F Essential it happens this way

The return value of F is pushed and a temp variable then G is pushed (its return value and a temp variable ) finally all H is pushed, evaluated and poped. then G and finally F.

for performance if you use argument expression it should be faster. since it should not contain any jumps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜