Lambda通过引用捕获参考变量 [英] Lambda capture reference variable by reference

查看:92
本文介绍了Lambda通过引用捕获参考变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于lambda,我想通过引用捕获已被外部引用保存的东西。假定引用的值超过了lambda,但没有超过创建lamdba的范围。

For a lambda, I'd like to capture something by reference which was held in the outer scope by reference already. Assume that the referenced value outlives the lambda, but not the scope in which the lamdba gets created.

我知道,如果lambda通过值捕获参考变量 ,则将复制参考对象。我想避免使用此副本。

I know that if a lambda captures a reference variable by value, the referenced object will be copied. I'd like to avoid this copy.

但是如果我通过引用捕获引用变量会发生什么呢?如果原始参考变量在执行lambda之前超出范围怎么办?这样安全吗?换句话说:是在引用后面的 object 还是在lambda中引用的 reference变量

But what happens if I capture a reference variable by reference? What if the original reference variable will get out of scope before executing the lambda? Is this safe? In other words: is the object behind the reference referenced or is the reference variable referenced in the lambda?

auto f() {
    const auto & myRef = g();
    return [&]{ myRef.doSomething(); };
}

f()();  // Safe?


推荐答案

是的,捕获按引用的对象是引用对象的生存期,而不是用于获取该对象的任何中间引用的生存期。您可以将引用视为别名,而不是实际变量。 (并且在类型系统中,对引用的引用与对常规变量的区别。)引用对原始对象具有别名,并且独立于用于对对象进行别名的其他别名(它们对相同的对象进行别名的事实除外)。

===== EDIT =====

根据对此SO问题(由 dyp 指出)的答案可能还不是很清楚。在该语言的其余部分中,对引用的引用的概念没有意义,并且从引用创建的引用成为该引用的对等体,但是显然,该标准在这种情况下含糊不清,并且被lambda捕获在某种意义上,引用可能是次要的,具体取决于从中捕获引用的堆栈帧。 (SO答案引用中的显式修饰语专门指出了所引用的实体,从表面上看,这表明只要原始对象存在,此用法是安全的,但是绑定机制可能暗示捕获链非常重要。)

According to the answer given to this SO question (pointed out by dyp), it appears that this may not be entirely clear. Throughout the rest of the language, the concept of a "reference to a reference" doesn't make sense and a reference created from a reference becomes a peer of that reference, but apparently the standard is somewhat ambiguous about this case and lambda-captured references may in some sense be secondary, dependent on the stack frame from which they were captured. (The explicit verbiage the SO answer quotes specifically calls out the referenced entity, which would on the face indicate that this usage is safe as long as the original object lives, but the binding mechanism may implicate the capture chain as being significant.)

我希望在C ++ 14/17中对此进行澄清,并且我希望对其进行澄清以保证这种用法的合法性。特别是,我认为C ++ 14/17通过表达式捕获变量的能力将使通过堆栈框架指针简单捕获范围变得更加困难,而最明智的捕获机制通常是捕获特定实体个别地。 (如果通过引用捕获实际的本地对象,也许可以允许堆栈帧捕获,因为如果在任何情况下在范围之外调用lambda,都会导致UB。)

I would hope that this is clarified in C++14/17, and I would prefer it to be clarified to guarantee legality for this usage. In particular, I think the C++14/17 ability to capture a variable via an expression will make it more difficult to simply capture a scope via a stack-frame pointer and the most sensible capture mechanism would be to generally capture the specific entities individually. (Perhaps a stack-frame-capture could be permitted if an actual local object is captured by reference, since this would result in UB if the lambda is called outside the scope in any event.)

在我们得到澄清之前,这可能无法移植。

Until we get some clarification, this may not be portable.

这篇关于Lambda通过引用捕获参考变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆