[英]C++ lambda: good reference vs. bad reference
BAD_increment_by
返回一個閉包,其中包含對y
的引用,但是當該函數返回時y
超出范圍,因此它包含一個懸空引用。 任何試圖調用該閉包的行為都是未定義的行為。
出於以下原因,這很糟糕:
int& BAD_copy(int x) { return x; }
不好。
我想嘗試以另一種方式解釋@Barry的回答。
讓我們寫下BAD_increment_by
內部發生的事情。
y
。 y
引用。 這將創建一個稱為y
的引用。 好吧,它們恰好具有相同的名稱,這令人困惑...讓我們將第一個稱為“值y
”,將第二個稱為“引用y
”。 y
。 BAD_increment_by
返回。 從BAD_increment_by
返回BAD_increment_by
,
y
不再存在。 y
仍指向值y
... y
不存在! 參考y
指向不存在的東西! 當有人調用lambda時,
y
。 y
是引用,因此我們將重定向到值y
。 y
或者只是我的幻覺? 結論是:調用lambda時,將使用懸掛的引用。 該行為是不確定的。
您可以將lambda視為定義了operator()
的類的簡寫,以便可以將其視為函數。
auto BAD_increment_by(int y)
{
return [&](int x){return x+y;};
}
可以視為以下方面的簡寫:
struct BadClosure
{
int& y;
BadClosure(int& y) // y is the only variable in scope
: y(y) // So the `&` only captures 'y'
{}
auto operator()(int x){return x+y;}
};
auto BAD_increment_by(int y)
{
return BadClosure(y);
}
因此,如果我使用以上內容:
int main()
{
// This object now has a reference to
// parameter 'y' from the function `BAD_increment_by()`
//
// But this function has exited.
// So the reference held by this object is no longer
// valid (ie it is a dangling reference).
auto addTwo = BAD_increment_by(2);
// Now use it
// Internally this access the member y
// which is a reference to the parameter 'y'
// from a function that is no longer executing.
std::cout << addTwo(5) << "\n";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.