如何不指定函数参数评估的确切顺序有助于C& C ++编译器生成优化代码? [英] How not specify an exact order of evaluation of function argument helps C & C++ compiler to generate optimized code?

查看:76
本文介绍了如何不指定函数参数评估的确切顺序有助于C& C ++编译器生成优化代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
int foo() {
    std::cout<<"foo() is called\n";
    return 9;
}
int bar() {
    std::cout<<"bar() is called\n";
    return 18;
}
int main() {
    std::cout<<foo()<<' '<<bar()<<' '<<'\n';
}
// Above program's behaviour is unspecified
// clang++ evaluates function arguments from left to right: http://melpon.org/wandbox/permlink/STnvMm1YVrrSRSsB
// g++ & MSVC++ evaluates function arguments from right to left
// so either foo() or bar() can be called first depending upon compiler.

以上程序的输出取决于编译器.函数参数的评估顺序为未指定.我读到的原因是它可以导致高度优化的代码. 如何不指定函数参数的确切评估顺序有助于编译器生成优化代码?

Output of above program is compiler dependent. Order in which function arguments are evaluated is unspecified. The reason I've read about this is that it can result in highly optimized code. How not specify an exact order of evaluation of function argument helps compiler to generate optimized code?

AFAIK,评估顺序严格用Java,C#,D等语言指定.

AFAIK, the order of evaluation is strictly specified in languages such as Java, C#, D etc.

推荐答案

我认为问题的全部前提是错误的:

I think the whole premise of the question is wrong:

如何不指定函数参数的确切评估顺序有助于C&用C ++编译器生成优化的代码?

How not specify an exact order of evaluation of function argument helps C & C++ compiler to generate optimized code?

这与优化代码无关(尽管它确实允许这样做).这是关于不惩罚编译器,因为底层硬件具有某些ABI约束.

It is not about optimizing code (though it does allow that). It is about not penalizing compilers because the underlying hardware has certain ABI constraints.

某些系统依赖于参数以相反顺序被推入堆栈,而其他系统则依赖于向前顺序. C ++可以在各种系统上运行,并且有各种约束.如果您在语言级别执行命令,则将需要某些系统支付罚款以执行该命令.

Some systems depend on parameters being pushed to stack in reverse order while others depend on forward order. C++ runs on all kinds of systems with all kinds on constraints. If you enforce an order at the language level you will require some systems to pay a penalty to enforce that order.

C ++的第一条规则是如果您不使用它,那么就不必为此付费".因此,执行命令将违反C ++的主要指令.

The first rule of C++ is "If you don't use it then you should not have to pay for it". So enforcing an order would be a violation of the prime directive of C++.

这篇关于如何不指定函数参数评估的确切顺序有助于C&amp; C ++编译器生成优化代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆