c++ 如果“foo”是一个参考变量,[/ foo] {…}捕获和[foo] {…}捕获是否有区别?

对于foo宣布为Foo& foo = …; lambdas的捕获值和引用引用语义之间有什么不同吗?
最佳答案
是的,有区别.

§5.1.2[expr.prim.lambda] p14

An entity is captured by copy if it is implicitly captured and the capture-default is = or if it is explicitly captured with a capture that does not include an &. For each entity captured by copy, an unnamed nonstatic data member is declared in the closure type. The declaration order of these members is unspecified. The type of such a data member is the type of the corresponding captured entity if the entity is not a reference to an object, or the referenced type otherwise.

因此,如果您捕获了一个通过值命名引用的标识符,则可以获取引用对象的副本.

转载注明原文:c++ 如果“foo”是一个参考变量,[/ foo] {…}捕获和[foo] {…}捕获是否有区别? - 代码日志