SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
"CAPTURE"
IN
LAMBDA
EXPRESSION
C++
Lahiru Dilshan
What is a
lambda
expression
A lambda expression is an
anonymous, inline function.
It is used to create a local
function, mainly for passing as
an argument to a function call
or as a return value.
Defining a
lambda
expression
auto func = []()
{
// function body
};
lambda introducer
parameter list
name
"Capture" in
a lambda
expression
Capture makes variable in the local scope
available for use in the body of the lambda
expression.
Write the name of the desired variable inside
[] of the lamda expression.
By default, variables are captured by value.
"Capture" in
a lambda
expression
Capture by value
#include <iostream>
using namespace std;
int main()
{
int x{10};
auto func = [x]()
{
cout << x << endl;
};
func();
}
Output: 10
"Capture" in
a lambda
expression
Capture by reference
#include <iostream>
using namespace std;
int main()
{
int x{10};
auto func = [&x]()
{
x++;
};
func();
cout << x << endl;
}
Output: 11
"Capture" in
a lambda
expression
To capture all local variables by value
#include <iostream>
using namespace std;
int main()
{
int x{10}, y{20};
auto func = [=]()
{
cout << x << " " << y << endl;
};
func();
}
Output: 10 20
"Capture" in
a lambda
expression
To capture all local variables by reference
#include <iostream>
using namespace std;
int main()
{
int x{10}, y{20};
auto func = [&]()
{
x++; y++;
};
func();
cout << x << " " << y << endl;
}
Output: 11 21
"Capture" and
objects
Lambda expressions can be used in a member
function to capture the data members of the
object.
The data members are captured through a
reference to the object.
"Capture" and
objects
#include <iostream>
using namespace std;
class Student
{
public:
int age{ 10 };
public:
void printAge()
{
auto lambdaAge = [this]()
{
cout << age << endl;
};
lambdaAge();
}
};
int main()
{
Student s;
s.printAge();
}
Output: 10
STAY CALM
AND
KEEP CODING!
Lahiru Dilshan

Contenu connexe

Similaire à "Capture" in lambda expression.

Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Meghaj Mallick
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptSandipPradhan23
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointerskirthika jeyenth
 
Linq & lambda overview C#.net
Linq & lambda overview C#.netLinq & lambda overview C#.net
Linq & lambda overview C#.netDhairya Joshi
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()daewon jeong
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhHarmeet Singh(Taara)
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxDeepasCSE
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 

Similaire à "Capture" in lambda expression. (20)

Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointers
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Linq & lambda overview C#.net
Linq & lambda overview C#.netLinq & lambda overview C#.net
Linq & lambda overview C#.net
 
Java8
Java8Java8
Java8
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Lecture5
Lecture5Lecture5
Lecture5
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 

Plus de Lahiru Dilshan

CAD vs CAM vs CAE software.pdf
CAD vs CAM vs CAE software.pdfCAD vs CAM vs CAE software.pdf
CAD vs CAM vs CAE software.pdfLahiru Dilshan
 
Degeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdfDegeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdfLahiru Dilshan
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++Lahiru Dilshan
 
What does Buffer in C++ means.pdf
What does Buffer in C++ means.pdfWhat does Buffer in C++ means.pdf
What does Buffer in C++ means.pdfLahiru Dilshan
 
Open CASCADE for your project.pdf
Open CASCADE for your project.pdfOpen CASCADE for your project.pdf
Open CASCADE for your project.pdfLahiru Dilshan
 
Linkage mechanisms - Presentation
Linkage mechanisms - PresentationLinkage mechanisms - Presentation
Linkage mechanisms - PresentationLahiru Dilshan
 
Industrial Training Experience
Industrial Training ExperienceIndustrial Training Experience
Industrial Training ExperienceLahiru Dilshan
 
Small scale business analysis
Small scale business analysisSmall scale business analysis
Small scale business analysisLahiru Dilshan
 
Computational and experimental investigation of aerodynamics of flapping aero...
Computational and experimental investigation of aerodynamics of flapping aero...Computational and experimental investigation of aerodynamics of flapping aero...
Computational and experimental investigation of aerodynamics of flapping aero...Lahiru Dilshan
 
Experimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureExperimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureLahiru Dilshan
 
Experimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureExperimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureLahiru Dilshan
 
Transient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fanTransient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fanLahiru Dilshan
 
Payload safety and related human factors
Payload safety and related human factorsPayload safety and related human factors
Payload safety and related human factorsLahiru Dilshan
 
Human factors consideration in emergency evacuation for commercial aircaft
Human factors consideration in emergency evacuation for commercial aircaftHuman factors consideration in emergency evacuation for commercial aircaft
Human factors consideration in emergency evacuation for commercial aircaftLahiru Dilshan
 
Human factors in payload safety of fighter aircrafts
Human factors in payload safety of fighter aircraftsHuman factors in payload safety of fighter aircrafts
Human factors in payload safety of fighter aircraftsLahiru Dilshan
 
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONSHUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONSLahiru Dilshan
 
Human factors - Maintenance and inspection
Human factors - Maintenance and inspectionHuman factors - Maintenance and inspection
Human factors - Maintenance and inspectionLahiru Dilshan
 
Fire safety of passenger aircraft
Fire safety of passenger aircraftFire safety of passenger aircraft
Fire safety of passenger aircraftLahiru Dilshan
 
Displays and controls arrangement of military aircraft
Displays and controls arrangement of military aircraftDisplays and controls arrangement of military aircraft
Displays and controls arrangement of military aircraftLahiru Dilshan
 
Considerations of human factors on commercial aircraft
Considerations of human factors on commercial aircraftConsiderations of human factors on commercial aircraft
Considerations of human factors on commercial aircraftLahiru Dilshan
 

Plus de Lahiru Dilshan (20)

CAD vs CAM vs CAE software.pdf
CAD vs CAM vs CAE software.pdfCAD vs CAM vs CAE software.pdf
CAD vs CAM vs CAE software.pdf
 
Degeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdfDegeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdf
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
What does Buffer in C++ means.pdf
What does Buffer in C++ means.pdfWhat does Buffer in C++ means.pdf
What does Buffer in C++ means.pdf
 
Open CASCADE for your project.pdf
Open CASCADE for your project.pdfOpen CASCADE for your project.pdf
Open CASCADE for your project.pdf
 
Linkage mechanisms - Presentation
Linkage mechanisms - PresentationLinkage mechanisms - Presentation
Linkage mechanisms - Presentation
 
Industrial Training Experience
Industrial Training ExperienceIndustrial Training Experience
Industrial Training Experience
 
Small scale business analysis
Small scale business analysisSmall scale business analysis
Small scale business analysis
 
Computational and experimental investigation of aerodynamics of flapping aero...
Computational and experimental investigation of aerodynamics of flapping aero...Computational and experimental investigation of aerodynamics of flapping aero...
Computational and experimental investigation of aerodynamics of flapping aero...
 
Experimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureExperimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structure
 
Experimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureExperimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structure
 
Transient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fanTransient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fan
 
Payload safety and related human factors
Payload safety and related human factorsPayload safety and related human factors
Payload safety and related human factors
 
Human factors consideration in emergency evacuation for commercial aircaft
Human factors consideration in emergency evacuation for commercial aircaftHuman factors consideration in emergency evacuation for commercial aircaft
Human factors consideration in emergency evacuation for commercial aircaft
 
Human factors in payload safety of fighter aircrafts
Human factors in payload safety of fighter aircraftsHuman factors in payload safety of fighter aircrafts
Human factors in payload safety of fighter aircrafts
 
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONSHUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
 
Human factors - Maintenance and inspection
Human factors - Maintenance and inspectionHuman factors - Maintenance and inspection
Human factors - Maintenance and inspection
 
Fire safety of passenger aircraft
Fire safety of passenger aircraftFire safety of passenger aircraft
Fire safety of passenger aircraft
 
Displays and controls arrangement of military aircraft
Displays and controls arrangement of military aircraftDisplays and controls arrangement of military aircraft
Displays and controls arrangement of military aircraft
 
Considerations of human factors on commercial aircraft
Considerations of human factors on commercial aircraftConsiderations of human factors on commercial aircraft
Considerations of human factors on commercial aircraft
 

Dernier

home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 

Dernier (20)

home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 

"Capture" in lambda expression.