Hello everyone,
I am currently following the C++ Basics course but I think there is a lack of explanation about them.
Particularly in the code that is provided in this part. The code is the following one.
I added std::cout to print what the lambda function is doing.
My problem is that I do not understand how b is defined and where its value is coming from.
Could someone explain it to me please?
Thank you in advance.
Code:
#include
#include
std::function<int(int)> makeLambda(int a){
return [a](int b){
std::cout << "a : " << a << std::endl;
std::cout << "b : " << b << std::endl;
std::cout << "a + b : " << a + b << std::endl;
return a + b; };
}
int main(){
auto add5 = makeLambda(5);
auto add10 = makeLambda(10);
add5(10) == add10(5);
}
Result:
a : 5
b : 10
a + b : 15
a : 10
b : 5
a + b : 15