#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.
上記のプログラムの出力はコンパイラに依存します。関数の引数が評価される順序は規定されていません。私がこれについて読んだ理由は、高度に最適化されたコードになる可能性があるからです。関数引数の評価の正確な順序を指定しないと、コンパイラは最適化されたコードを生成できませんか?
私の知る限り、評価の順序は、Java、C#、D などの言語で厳密に指定されています。