Before start the lambdas, please check the function object
lambdas
It is new feature in c++11 and its is also called as “lambda expressions” or lambda function or lambda. It is unnamed function object, in other word it is shortcut of “making object of unnamed class with operator() and invoking operator()”, it can be defined inside statement and expression. It will be executed as instruction.
It consists of following parts:
- capture list [] : it is used to access the local variable(outside non static object) via either value or reference.
- optional parameter list () : It is used to get the arguments from calling object.
- optional mutable : It is used to modify the read only object, changes will be visible only in lambda.
- optional noexcept : It is used to specify that lambda will not emit exception.
- optional return type -> type : It is used to specify the return type of lambda.
- body {} : It is used to specify the code to be executed.
Example:
int x=1; std::cout<<"X:"<<x<<std::endl; auto res5 = [=](std::string str) mutable noexcept ->int {++x; std::cout<<"Lambda parameter:"<<str<<",X:"<<x<<std::endl; return 5;}; int res6 = res5("test"); //calling lambda std::cout<<"Res:"<<res6<<" ,X:"<<x<<std::endl; Output: X:1 Lambda parameter:test,X:2 Res:5 ,X:1
When will we use it (What is the Objective/purpose of lambda):
- When we want to pass the function logic as argument to algorithm.
- Use as Callback
Advantage:
- No need to think about function Name, just use it.
- lambda will be executed as instructions.
- Small and easy to Use.
Source code with example:
#include<iostream> int main() { std::cout<<"In Main"<<std::endl; //Empty lambda introducer //calling directly []{std::cout<<"In Lambda first"<<std::endl;}(); //Empty Lambda introducer auto res = []{std::cout<<"In Lambda Second"<<std::endl;}; //Object is calling res(); //Lambda introducer having capture with ref, we have read //and write access to outside non static object int x=0; auto res1 = [&x]{std::cout<<"In Lambda Third"<<std::endl;++x;}; //Object is calling res1(); std::cout<<"x:"<<x<<std::endl; //Lambda introducer having capture with value, we have read //only access to outside non static object auto res2 = [x]{std::cout<<"In Lambda Four and x:"<<x<<std::endl;}; //Object is calling res2(); std::cout<<"x:"<<x<<std::endl; //Lambda introducer having capture with value, we have read //only access to outside non static object BUT we can make it //Writable with mutable, this change is visible inside lambda auto res3 = [x]()mutable{++x;std::cout<<"In Lambda Five and x:"<<x<<std::endl;}; //Object is calling res3(); std::cout<<"x:"<<x<<std::endl; //Empty Lambda introduce with return type(perenthesis is required) auto res4 = []()->int{std::cout<<"In Lambda Six"<<std::endl; return 10;}; //Object is calling int output = res4(); std::cout<<"Output:"<<output<<std::endl; //Empty Lambda introducer auto res5 = [](std::string str){std::cout<<"In Lambda Seven, parameter:"<<str<<std::endl;}; //Object is calling res5("test"); return 0; }
Output:
In Main In Lambda first In Lambda Second In Lambda Third x:1 In Lambda Four and x:1 x:1 In Lambda Five and x:2 x:1 In Lambda Six Output:10 In Lambda Seven, parameter:test