#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.
上述程序的输出取决于编译器。未指定评估函数参数的顺序。我读到这个的原因是它可以产生高度优化的代码。不指定函数参数的准确求值顺序如何帮助编译器生成优化代码?
AFAIK,评估顺序在 Java、C#、D 等语言中严格指定。