-3
此代碼無法通過編譯。唯一的區別是返回類型。 Foo1的返回類型是用戶定義的結構,Foo2的類型是int。參考返回值時出錯
struct test
{
};
test Foo1()
{
return test();
}
int Foo2()
{
return 0;
}
int main()
{
test& r1 = Foo1(); //ok
int& r2 = Foo2(); //no but why? Is it the C++ standard way?
return 0;
}
對於我來說,彙集關於G ++ - 4.7既不線是好的 – Brian
鐺++:'錯誤:對'test'類型的非常量左值引用不能綁定到類型爲'test'的臨時類型,兩行上的錯誤都是相同的。你的編譯器是什麼? – Mat
兩行都是錯誤的:你不能將一個非const的左值引用綁定到臨時值。 – juanchopanza