Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between capture and passing an argument in lambda functions

I understand the lambda function and the purpose of it in c++ 11. But i do not understand the difference between "Capturing the value" and "Passing an argument". For Instance..

#include <iostream> #include <functional> using namespace std;  int add(int a,int b){     return a+b; }  int main(int argc, char** argv){      function <int(int,int)> cppstyle;     cppstyle = add;      auto l = [] (function <int(int,int)> f,int a, int b) {return f(a,b);};      cout << l(cppstyle,10,30) <<"\n";    } 

The output of the code above is same as the code below..

#include <iostream> #include <functional> using namespace std;  int add(int a,int b){     return a+b; }  int main(int argc, char** argv){      function <int(int,int)> cppstyle;     cppstyle = add;      auto l = [cppstyle] (int a, int b) {return cppstyle(a,b);};      cout << l(10,30) <<"\n";     } 

Is "capturing a value" similar to "passing a value as an argument"? or capture has some special meaning?

like image 786
01000001 Avatar asked Jun 24 '15 22:06

01000001


People also ask

What does it mean to lambda capture this?

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.

How many arguments can be passed to lambda?

You can use as many arguments as you want in a lambda function, but it can have only one expression. This expression is evaluated and returned as a result.

How many ways we can capture the external variables in the lambda expression?

Explanation: There are three ways in which we can capture the external variables inside the lambda expression namely capture by reference, capture by value and capture by both that is mixed capture.


1 Answers

The difference between a captured argument and a passing argument could be seen with an analogy. Consider the following function object:

struct Capture {   int &i;   int const j; public:   Capture(int &_i, int &_j) : i(_i), j(_j) {}   int operator()(int const a, int const b) {     i *= j;     return a * b;   } }; 

In function object class Capture there are two member variables i and j. There's also overloaded operator() which takes two input arguments. Now consider the following lambda:

int i, j; [&i, j](int const a, int const b) {   i *= j;   return a * b; }; 

The member variables of class Capture are in analogy with the lambda capture (i.e., [&i, j]), whereas input arguments of overloaded operator() a and b are in analogy with input arguments a and b of the lambda shown above.

That is, if you consider a lambda as a function object, its capture is the state of the function object (i.e., its member variables) whereas its input arguments would be the input arguments of the overloaded operator().

like image 123
101010 Avatar answered Oct 18 '22 17:10

101010