#include #include #include #include #include using namespace std ; class Integer { public: int x1 ; Integer(int x1p) { x1 = x1p ; cout << "Object Created" << x1 << endl; } Integer(const Integer& param1) { x1 = param1.x1 ; cout << "Copy constructor: Object Created" << x1 << endl; } ~Integer() { cout << "Object Destroyed: " << x1 << endl; } }; int main() { int x1 = 10 ; int x2 = 10 ; //outside variable like x1 is not captured auto print1 = [](string str1){ cout << str1 << endl ; } ; print1( "Calling the lambda print function." ) ; //Trying to access the variable outside the //scope auto print2 = [](string str1){ cout << str1 << endl ; // cout << x1 << endl ; } ; print2( "Calling the lambda print2 function." ) ; auto print3 = [=](string str1){ cout << str1 << endl ; cout << x1 << endl ; //x1=5 ; Compiler error //x1 is read only } ; print3( "Calling the lambda print3 function." ) ; auto print4 = [&](string str1){ cout << str1 << endl ; x1=4 ; } ; print4( "Calling the lambda print4 function." ) ; cout << x1 << endl ; auto print5 = [x1](string str1){ cout << str1 << endl ; cout << x1 << endl ; //cout << x2 << endl ; //The "x2" is not accessible here. } ; print5( "Calling the lambda print5 function." ) ; Integer obj1( 10 ) ; //A copy is created inside the lambda //Copy constructor gets called. //When print6 goes out of scope then //the obj1 gets destroyed. auto print6 = [=](string str1){ cout << str1 << endl ; cout << obj1.x1 << endl ; //We cannot modify obj1 } ; print6( "Calling the lambda print6 function." ) ; //capture all the variables by reference //Copy constructor does not get called. cout << "-------------------" << endl ; auto print7 = [&](string str1){ cout << str1 << endl ; cout << obj1.x1 << endl ; obj1.x1 = 5 ; } ; print7( "Calling the lambda print7 function." ) ; cout << "-------------------" << endl ; //Allowing access to a single variable/object //Another copy of obj1 gets created here. auto print8 = [obj1](string str1){ cout << str1 << endl ; cout << obj1.x1 << endl ; // Other variables are not captured // cout << x1 << endl ; } ; print8( "Calling the lambda print8 function." ) ; auto print9 = [obj1, x1](string str1){ cout << str1 << endl ; cout << obj1.x1 << endl ; // Other variables are not captured cout << x1 << endl ; } ; print9( "Calling the lambda print9 function." ) ; cout << "-------------------" << endl ; //Passing a single variable by reference auto print10 = [&obj1](string str1){ cout << str1 << endl ; //Allowed obj1.x1 = 100 ; cout << obj1.x1 << endl ; } ; print10( "Calling the lambda print10 function." ) ; //Changes are preserved after the invocation cout << obj1.x1 << endl ; cout << "-------------------" << endl ; cout << "-------------------" << endl ; cout << "mix and match example" << endl ; //we can mix and match auto print11 = [&obj1, x1](string str1){ cout << str1 << endl ; cout << x1 << endl ; cout << obj1.x1 << endl ; } ; print11( "Calling the lambda print11 function." ) ; cout << "-------------------" << endl ; auto print12 = [&obj1, =](string str1){ cout << str1 << endl ; cout << x1 << endl ; //x1 = 13 ; compiler error //x1 is only captured by value obj1.x1 = 12 ; cout << obj1.x1 << endl ; } ; print12( "Calling the lambda print12 function." ) ; cout << "-------------------" << endl ; }