LAMBDA Expressions in C++

Cengizhan Varlı
6 min readFeb 14, 2023

--

LAMBDA Expressions was added to C++ with modern C++. It is available in many other OOP Languages.

It is The protagonist of functional programming.

Lambda is an expression that corresponds to a temporary object of the class type that the compiler writes and writes the code of a class.

Lambda expressions are a feature in C++ that allow you to define and create anonymous functions in place, without having to explicitly declare a named function. They are commonly used to write more concise and readable code, and to pass functions as arguments to other functions.

SYNTAX

So How is it works?

When you define a lambda expression in C++, the compiler generates a unique type for the lambda expression based on its parameter list, return type, and captures. This type is called the “closure type” of the lambda expression.

When the lambda expression is called, the compiler generates an instance of the closure type, which contains the captured variables and the function call operator (i.e., the body of the lambda expression). The closure type is typically an anonymous class that overloads the function call operator, so the lambda expression can be called like any other function.

If compiler encounters an expression that like follows

[](){};

It creates a temporary class and writes a operator() function to inside that of as bellows;

The Class that written by compiler is called “Clouse Type” and created temporary object is called “Clouse Object

As above example, How does The Compiler write code for it?

As follows;

Of course, a function with a parameter and a return type can be written as follows;

OUTPUT

The code written by the compiler is as follows

Further, we can also learn what the compiler names the temporary class as follows.

OUTPUT

As shown, name of created temporary class type by compiler is “Z4mainEUliE_”

So, Are The Class type names same for lambda expressions constructed in the same way as below?

Of course, answer is “NO”.

As seen below, They all have different name.

OUTPUT

Even if expressions are same, Compiler creates different temporary object for each lambda expression.

NOTE:

If The lambda expression have not a parameter, yo don’t need to type ().

Frequent Usage

We can send the lambda expressions to function template. It is most used this way.

OUTPUT

In the above example, we write a template function. This function calls the function it takes as a parameter.

The code you provided defines a templated function func() that takes a single parameter “f” of some templated type ”T”. The function body calls f() to execute the code that ”f” represents. The "T" type must be a function object that has a void return type and no parameters.

In the main function, the func() function is called three times with three different lambda expressions as arguments. The first lambda expression prints "Hello" to the standard output, and the second lambda expression prints "Merhaba" to the standard output. The code calls the func() function with these two lambda expressions as arguments, and each lambda expression is executed in turn by the func() function.

So, when the code is executed, it first calls “func([]{std::cout<<”Hello”;})” , which calls the func() function with a lambda expression that prints "Hello". The func() function then executes the lambda expression, which prints "Hello" to the standard output. Then, the code calls “func([]{std::cout<<”Merhaba”;}) ”, which calls the func() function with a lambda expression that prints "Merhaba". The func() function then executes the second lambda expression, which prints "Merhaba" to the standard output. The last expression is also same.

Another Example:

OUTPUT

The func() function we wrote has 2 template parameters. This function calls the function that comes by means of the first parameter as passing the second parameter. We can see the output as above.

Direct function call with lambda expression can become a named object.

To receive and use the temporary object that the compiler creates with the lambda locally ;

the function can be used in the rest of the code by detecting with auto.

Output

Mutuble Keyword

First of all, lets we remember const member function;

A const member function is a member function of a class that is declared with the const keyword. When a member function is declared as const, it means that it promises not to modify the state of the object on which it is called.

As know, A Lambda expression like above is equal a temporary class like below.

If we want to remove const expression from operator() member function, we can use mutable keyword.

If we use mutable keyword like this;

The above lambda expression equals the following temporary object.

So what does this change? As it is known, if the function is a const member function, it cannot change the variables of the class. If the class had an mx value and the function changed this value, the function being const would cause the compiler to throw an error.

As shown, there is a error for The above codes ,But the following code is legal.

In such cases, we can use mutable. So is it possible to create an mx like above with lambda?

YES.

TRAILLING RETURN TYPE

The trailing return type for lambda expressions is a C++ feature that allows you to specify the return type of a lambda function using a trailing “->” followed by the return type. This is useful when the return type is complex or depends on the argument types.

Here’s an example of a lambda expression that uses a trailing return type:

In this example, the lambda expression takes an integer argument x and returns the reciprocal of x as a double. The “->” is used to specify the return type of the lambda expression, which is double.

The trailing return type is particularly useful when the return type depends on the types of the arguments to the lambda expression. For example:

OUTPUT

--

--

No responses yet