0
我寫下面的代碼並[讓我驚訝]它印: XX:1 ZZ:0
我原本以爲xx和zz都打印爲0.這是在linux和darwin上用gcc 4.2,4.4和4.7進行的嘗試。經過一番研究,我發現x = x ++是未定義的行爲,但我希望(x ++)先被執行並返回0.是否有人能夠闡明這種奇怪的編譯器行爲?
#include <iostream>
using namespace std;
int frwd(int aa) { return aa; }
int main() {
int xx = 0;
xx = (xx++);
cout << "xx:" << xx << endl;
int zz = 0;
zz = frwd(zz++);
cout << "zz:" << zz << endl;
}
完整的解釋:[未定義的行爲和順序點](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – 2012-07-27 06:18:48