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 above code is same as below code..
#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 is similar as passing the value as an argument? or capture has some special meaning?
Aucun commentaire:
Enregistrer un commentaire