[英]C++ lambda capture single struct field by value
I have a large struct and want to capture a field out of it by value.我有一个很大的结构,想按值从其中捕获一个字段。
The below code apparently WORKS, but I was confused and led to believe by this (from SO) that it shouldn't.下面的代码显然可以工作,但我很困惑,并因此(来自 SO)相信它不应该。 Have I misunderstood that post, or will this fail in some way I do not see?我是否误解了那个帖子,或者这会以某种我看不到的方式失败?
I am using C++14.我正在使用 C++14。
struct Foo { int Bar; /* ... */ };
Foo foo;
IAcceptStdFunction( [Bar = foo.Bar]{ /* can use Bar */ } );
You can use such Bar
from inside the lambda without any troubles.您可以在 lambda 内部使用这样的Bar
而不会遇到任何麻烦。 Just note that it's a copy of the original foo.Bar
, not a reference.请注意,它是原始foo.Bar
的副本,而不是参考。
The post you linked talks about a situation when we want to look what a specific lambda has captured without actually calling it, some kind of introspection.您链接的帖子谈到了一种情况,当我们想查看特定的 lambda 在没有实际调用它的情况下捕获了什么,某种内省。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.