Learn How To Use Generic Lambdas In Modern C++
Lambda Expressions allow users to write an inline expression that can be used for short snippets of code in your C++ app which are not going to be reused and don’t require naming. The Lambda Expression construct is introduced in C++ 11 and further developed in the C++14, C++17, and C++20 standards. In C++14, lambda expressions are improved by the generalized lambda (generic lambda) or initialized lambda feature, and in this post, we remember what lambda is and explain what a generic lambda is, and how we can use it. What is a lambda expression in modern C++? A Lambda Expression defines an anonymous function or a closure at the point where it is used. You can think of a lambda expression as an unnamed function (that’s why it’s called “anonymous”). Lambda expressions help make code cleaner, and more concise and allow you to see behavior inline where it’s defined instead of referring to an external method, like a function. Lambda Expressions are an expression that returns a function object, and they are assignable to a variable whose data type is usually auto and defines a function object. The syntax for a lambda expression consists of specific punctuation with = [ ] ( ) { … } series. If you are new to lambdas or want to know more about it, please check these two posts that we released before. How to use a lambda expression in modern C++ Simple Syntax of Lambda Expression is; Datatype Lambda Expression = [Capture Clause] (Parameter List) -> Return Type { Body } Now let’s see this syntax in an example. We will define a lambda expression to combine datatypes; int add_integers = []( int a, int b ) { return a + b; }; and we can use lambda as below, int x = add_integers( 10, 20 ); What is the generic lambda expression in modern C++? Before C++14, lambda function parameters need to be declared with concrete types, as in the given example above. C++14 has a new generic lambda feature that allows lambda function parameters to be declared with the auto-type specifier. Generalized lambdas can be defined with the auto keyword that comes with C++11 before. Let’s take the same integer example above, we can define a generic lambda with the auto keyword as below, auto add_things = []( auto a, auto b ) { return a + b; }; Generic lambdas are essentially templated functor lambdas. In example, the code above is equivalent to this code, struct { template auto operator()( T x, U y ) const { return x + y; } } add_things{}; How to use generic lambdas in modern C++ We can use this lambda with int type, int x = add_things( 10, 20 ); or we can use with float type, float x = add_things( 10.f, 20.f ); or we can use it with bool, char, double, long long double,…etc types. This is why it is called as ‘generalized‘, it is a general form that we can use with any types. Very useful and powerful. Note that, auto type deduction is added into C++14, and generic lambdas feature follow the rules of template argument deduction. Is there an example of how to use generic lambdas […]
