C++C++11C++14C++17Code SnippetLearn C++

Learn To Use Powerful Lambda Expressions In C++ On Windows

silhouette of tree near body of water during golden hour

Lambda Expressions are an expression that returns a function object, it comes with C++11 standards and used C++14, C++17, C++20 standards, it is well explained here. Lambda expressions are widely used in C++, C#, Groovy, Java, Python, Ruby languages too.

The Greek letter Lambda (λ) refers to an anonymous function, it means chosen since it is equated with something nameless and expression term means it is required since the code can be evaluated and will return a value. 

A Lambda Expression is assignable to a variable whose data type is usually auto and defines a function object.

Syntax for a lambda expression consists of specific punctuation with = [ ] ( ) { … } series. 

Simple Syntax of Lambda Expression is;

Syntax of Lambda Expression (C++20) version is;

Here;
Datatype is its type like int, float, class, etc. and mostly auto term is used
Lambda Expression is a definition by the user
Capture Clause has variables that are visible in the body, capture can happen by value or reference and it can be empty
Parameter List can be empty or omitted
Return Type is a data type returned by the body, optional, normally deduced
Body contains the programming statements to execute and it can be empty

Now let’s see this syntax in an example. We will define a lambda expression to combine datatypes;

This can be used on any datatypes as given below;

In a modern way, we can use UnicodeString in C++ Builder VCL or FMX projects. This code will support worldwide languages. Here below we used the Memo component as an output;

Find out more about Lambda Expressions in the Embarcadero DocWiki.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, and Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++

Worth reading...
Learn How To Build A Modern C++ “Hello World” Example For Windows