Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the word capture mean in the context of lambdas?

Tags:

c++

c++11

lambda

Can somebody please provide some insights on this? Is the lambda capturing external variables, or is the outside world capturing values produced by the lambdas? What does it mean for a certain variable to be captured?

like image 887
Baron Yugovich Avatar asked Aug 13 '15 04:08

Baron Yugovich


People also ask

What is capture clause in lambda function in C++?

A capture clause of lambda definition is used to specify which variables are captured and whether they are captured by reference or by value. An empty capture closure [ ], indicates that no variables are used by lambda which means it can only access variables that are local to it.

How do you capture variables in lambda?

Capture clause A lambda can introduce new variables in its body (in C++14), and it can also access, or capture, variables from the surrounding scope. A lambda begins with the capture clause. It specifies which variables are captured, and whether the capture is by value or by reference.

What is the point of lambdas?

Lambda functions are intended as a shorthand for defining functions that can come in handy to write concise code without wasting multiple lines defining a function. They are also known as anonymous functions, since they do not have a name unless assigned one.

What does a lambda expression specify how is it used?

A lambda expression is an anonymous function that provides a concise and functional syntax, which is used to write anonymous methods. It is based on the function programming concept and used to create delegates or expression tree types.


3 Answers

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. For example:

[&] { x += 1; }       // capture by reference
[=] { return x + 1; } // capture by value

The first produces a class roughly like this:

class foo { 
    int &x;
public:
    foo(int &x) : x(x) {}
    void operator()() const { x += 1; }
};

The second produces a class something like this:

class bar {
    int x;
public:
    bar(int x) : x(x) {}
    int operator()() const { return x + 1; }
};

As with most uses of references, capturing by reference can create a dangling reference if the closure (the object of the class created by the lambda expression) out-lives the object that was captured.

like image 137
Jerry Coffin Avatar answered Oct 09 '22 11:10

Jerry Coffin


Jerry Coffin gave you detailed response,I agree that lambda is syntax for creating a class.There are bunch of options about the way variables are captured and how.List of options:

[]  Capture nothing (or, a scorched earth strategy?)
[&] Capture any referenced variable by reference
[=] Capture any referenced variable by making a copy
[=, &foo]   Capture any referenced variable by making a copy, but capture variable foo by reference
[bar]   Capture bar by making a copy; don't copy anything else
[this]  Capture the this pointer of the enclosing class
like image 29
Richard Rublev Avatar answered Oct 09 '22 11:10

Richard Rublev


Lambda captures variables which it otherwise will not have access to. One can specify how A lambda should capture a variable .i.e value,reference . This has been explained really well here In lambda functions syntax, what purpose does a 'capture list' serve?

like image 3
Sheetal Angra Avatar answered Oct 09 '22 11:10

Sheetal Angra