[英]Does the output depend on the compiler?
我有一個代碼。 代碼打印1236
(g++ 7.5.0)
output 是否取決於編譯器? (例如 output 可以是3216
)
#include <bits/stdc++.h>
using namespace std;
int foo(int& x) {
std::cout << ++x;
return x;
}
int main() {
int i = 0;
cout << foo(i) + foo(i) + foo(i) << endl; // 1236
}
不,output不依賴於編譯器(模bits/stdc++.h
/stdc++.h 廢話)。 三個調用foo(i)
的計算順序未指定,但這不影響 output:function 調用未交錯,因此某些調用會將i
遞增到 1,打印並返回它(作為副本),那么另外兩個將分配、打印並返回 2,最后一個將執行 3,並且它們的返回值總和為 6。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.