SlideShare a Scribd company logo
"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

More Related Content

Similar to "Capture" in lambda expression.

Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functionsmussawir20
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
Sasha Goldshtein
 
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.ppt
SandipPradhan23
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointers
kirthika jeyenth
 
Linq & lambda overview C#.net
Linq & lambda overview C#.netLinq & lambda overview C#.net
Linq & lambda overview C#.net
Dhairya Joshi
 
Java8
Java8Java8
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
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
KarthikSivagnanam2
 
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
Harmeet Singh(Taara)
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 

Similar to "Capture" in lambda expression. (20)

Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
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
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 

More from 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.pdf
Lahiru Dilshan
 
Degeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdfDegeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdf
Lahiru 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.pdf
Lahiru Dilshan
 
Open CASCADE for your project.pdf
Open CASCADE for your project.pdfOpen CASCADE for your project.pdf
Open CASCADE for your project.pdf
Lahiru Dilshan
 
Linkage mechanisms - Presentation
Linkage mechanisms - PresentationLinkage mechanisms - Presentation
Linkage mechanisms - Presentation
Lahiru Dilshan
 
Industrial Training Experience
Industrial Training ExperienceIndustrial Training Experience
Industrial Training Experience
Lahiru Dilshan
 
Small scale business analysis
Small scale business analysisSmall scale business analysis
Small scale business analysis
Lahiru 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 structure
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 structure
Lahiru 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 fan
Lahiru Dilshan
 
Payload safety and related human factors
Payload safety and related human factorsPayload safety and related human factors
Payload safety and related human factors
Lahiru 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 aircaft
Lahiru 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 aircrafts
Lahiru 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 INSPECTIONS
Lahiru Dilshan
 
Human factors - Maintenance and inspection
Human factors - Maintenance and inspectionHuman factors - Maintenance and inspection
Human factors - Maintenance and inspection
Lahiru Dilshan
 
Fire safety of passenger aircraft
Fire safety of passenger aircraftFire safety of passenger aircraft
Fire safety of passenger aircraft
Lahiru 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 aircraft
Lahiru 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 aircraft
Lahiru Dilshan
 

More from 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
 

Recently uploaded

Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 

Recently uploaded (20)

Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 

"Capture" in lambda expression.