Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a Lambda Function as a parameter [duplicate]

I'm having some issues working with c++ recently, its basically this:

Inside a function (lets say int main), I declared a variable Y = 5, and I have this lambda function that receives a value and adds up Y;

My problem is: I need to pass this lambda function to an already existent function, so it can be called inside the other function.

I tried a couple of things, but none of them worked as I intended (some not even worked):

double receives( double (*f)(double) )
{
    return f(5);
}

int main() 
{

    int y = 5;

    auto fun = [](double x) {
      return x + y;
    };

    cout << receives(&fun);

    return 0;
}

Another problem is that I cant change my receives function signature, the parameter must be double (*f)(double) because of the remainder of the code. I also need the lambda function to carry over the y value, without using additional parameters.

Anyone can help me on this?

like image 958
João Batista Avatar asked Apr 20 '20 18:04

João Batista


People also ask

How do you pass a lambda function as a parameter?

Passing Lambda Expressions as Arguments You can pass lambda expressions as arguments to a function. If you have to pass a lambda expression as a parameter, the parameter type should be able to hold it. If you pass an integer as an argument to a function, you must have an int or Integer parameter.

Can lambda take multiple arguments?

A lambda function can have as many arguments as you need to use, but the body must be one single expression.

Can Python lambda take two arguments?

In Python, a lambda function is a single-line function declared with no name, which can have any number of arguments, but it can only have one expression.

Can lambda have multiple parameters Java?

The lambda expressions are easy and contain three parts like parameters (method arguments), arrow operator (->) and expressions (method body). The lambda expressions can be categorized into three types: no parameter lambda expressions, single parameter lambda expressions and multiple parameters lambda expressions.

How to pass a lambda expression as a parameter to another method?

In order to pass Lambda expression as a Method Parameter, the type of method parameter that receives must be of type of the Functional Interface. This is the easiest example on how to pass a Lambda as a parameter to another method. 1.

What is a lambda expression in Java?

A lambda expression is an anonymous or unnamed method in Java. It doesn't execute on its own and used to implement methods that are declared in a functional interface. If we want to pass a lambda expression as a method parameter in java, the type of method parameter that receives must be of functional interface type.

When to accept a lambda function by value?

Accept a Lambda By Value If It Captures Only Small Amounts of Data For many of the STL’s functions, it can be expected that a lambda should only capture small amounts of data. But even if it requires to capture a lot of data, the cost of passinga lambda around by value can potentially be minimized.

How do I use lambda expressions in a LINQ query?

Using delegates you can pass a lambda expression as a parameter to a function, and then use it in LINQ queries. This can be done with Func<…> delegates. If you want to pass a lambda expression to be used, for example, in Where clause, you need a Func<T, bool> delegate.


1 Answers

Use std::function like for example

#include <iostream>
#include <functional>

double receives( const std::function<double( double )> &f )
{
    return f( 5 );
}

int main() 
{
    int y = 5;

    auto fun = [y](double x) 
    {
      return x + y;
    };

    std::cout << receives( fun ) << '\n';

    return 0;
}
like image 155
Vlad from Moscow Avatar answered Oct 17 '22 18:10

Vlad from Moscow