I have a class foo
that has bar
as a member variable.
In another member function of the class, I'm writing a lambda function:
[bar](void){}
But I can't include bar
in the capture list. Why is that?
To capture the member variables inside lambda function, capture the “this” pointer by value i.e. std::for_each(vec. begin(), vec. end(), [this](int element){ //.... }
The capture clause is used to (indirectly) give a lambda access to variables available in the surrounding scope that it normally would not have access to. All we need to do is list the entities we want to access from within the lambda as part of the capture clause.
The lambda is capturing an outside variable. A lambda is a syntax for creating a class. Capturing a variable means that variable is passed to the constructor for that class. A lambda can specify whether it's passed by reference or by value.
Lambdas always capture objects, and they can do so by value or by reference.
Only objects with automatic storage duration can be captured by a lambda in C++11 (i.e. local variables and function parameters). If you want the effect of capturing a non-static
class data member, you can either capture the this
pointer as in Danvil's answer:
auto f = [this]{ std::cout << a << std::endl; };
or cache the data member's value in a local variable and capture that:
auto a = this->a; auto f = [a]{ std::cout << a << std::endl; };
which will be much more concise in C++14:
auto f = [a = this->a]{ std::cout << a << std::endl; };
The choice between these two options depends on whether you want to store the value a
has right now or if you want to retrieve the value a
has when the lambda is called. Note that in the case where this
is captured you must ensure that the lifetime of the pointer object encloses the lifetime of the lambda a call to the lambda after the object is destroyed has undefined behavior. The more simple case that captures a copy of a
is completely self-contained and has no such lifetime issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With