Generic Lambdas in C++14

In this article, we will discuss about Generic Lambdas in C++14.

Introduction to Generic Lambdas in C++14

Lambdas were introduced in C++11. A Lambdas is also known as an Anonymous Function, because it does not have any name. It accepts parameters and returns a value. It has a function body, but it does not have a name. These are generally defined inline, and used when we don’t need a reusable function. But there is a problem with the lambda function. We need to provide the type of the parameters while defining its body.

Suppose this is a lambda function,

#include <iostream>

int main() 
{
    auto add = [](int a, int b) {
        return a + b;
    };

    int result = add(5, 10);

    std::cout << "Result : " << result << "\n";
    return 0;
}

Output:

Result : 15

It accepts two in int values and returns a sum of those in integers. So return value will be in int.

But now, we need another lambda function that accepts two double values and it should add them and return them. Then in that case, we need to define a separate lambda function. Now this is a problem of code duplication. To solve this issue, Generic lambdas, were introducing in C++14.

Let’s see the syntax of generic lambda,

#include <iostream>

int main() 
{
    auto add = [](auto a, auto b) {
        return a + b;
    };

    auto result = add(5.7, 10.1);

    std::cout << "Result : " << result << "\n";
    return 0;
}

Output:

Result : 15.8

In this, we can provide the auto as a type of the parameters. The actual type of these parameters will be reduced based on the values passed as arguments, when this lambda function is called.

So we have stored this Lambda function in a variable add, and while invoking this lambda function, we have passed two double values, 5.5, and 10.4. It’ll add those values and return a double value. Suppose if we pass two integer values like five and 10 then the type of A and B will be reduced as integer, and it’ll add those ints and return an int value.

And now suppose we pass two string values in this like “Hi ” and “there”, then the type of a and b will be deduced as strings. It’ll connate these two strings and return a string value.

#include <iostream>
#include <string>

int main() 
{
    auto add = [](auto a, auto b) {
        return a + b;
    };

    std::string first = "sample ";
    std::string second = "text";

    auto result = add(first, second);

    std::cout << "Result : " << result << "\n";
    return 0;
}

Output:

Result : sample text

Like here, we have created the similar Lambda function, but instead of passing in two doubles, we have passed two string values. So it’ll concatenate these two strings and return a string value.

Why Generic Lambda were introduced in C++14?

Generic Lambda is useful when we are planning to use generic lambda functions with generic algorithms like s std::sort(). Suppose we have a vector of ints. Now we want to sort this vector of ints in a decreasing order. For this we need to pass a comparator. Inside this comparator. It’ll accept two parameters and return true if the first parameter is greater than the second. If we use this comparator, then it’ll sort the vector in decreasing order. Let’see the complete example,

#include <iostream>
#include <vector>
#include <algorithm>

int main() 
{
    auto comp = [](auto a, auto b) {
        return a > b;
    };

    std::vector<int> vec {11, 34, 55, 66, 12};


    std::sort(vec.begin(), vec.end(), comp);

    for(auto elem: vec)
    {
        std::cout<< elem << ", ";
    }

    std::cout<<std::endl;
    return 0;
}

Output:

66, 55, 34, 12, 11, 

It sorted the vector in decreasing order. So, here we have used a Generic Lambda function.

Now suppose instead of vector of ints, we have a vector of strings like this, then we can use the same Generic Lambda function as a comparator to sort this vector of strings in decreasing order.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main() 
{
    auto comp = [](auto a, auto b) {
        return a > b;
    };

    std::vector<std::string> vec {"here", "is", "sample", "text", "this"};


    std::sort(vec.begin(), vec.end(), comp);

    for(auto elem: vec)
    {
        std::cout<< elem << ", ";
    }

    std::cout<<std::endl;
    return 0;
}

Output:

this, text, sample, is, here,

It sorted the vector of strings in decreasing order.

Summary

Generic Lambda functions are useful when we want to reuse a Lambda function at different places. Sometimes it can be used with the integers or double or string or with any custom objects. Another advantage is many times in the big projects we use namespace. So sometimes parameter type becomes very large. So to solve these problems, we can use the auto. So in C++14 generic Lambdas were introduced to enhance the functionality of a Lambda function.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top