Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda functions as class members

Is it possible to accept two different types of lambda function as class members without knowing their template arguments ahead of time?

struct two_functors {
    std::function<???> a;
    std::function<???> b;
    ...
};

Such that something like this would be possible:

void main(){
    vector<two_functors> many_functors;

    int a = 2;
    int b = 3;
    double c = 4.7;
    double d = 8.4;

    two_functors add_and_subtract;
    add_and_subtract.a = [a, b](int x, int y){cout << x + y << endl;};
    add_and_subtract.b = [c, d](double x, double y){cout << x - y << endl;};

    two_functors multiply_and_divide;
    multiply_and_divide.a = [c, d](double x, double y){cout << x * y << endl;};
    multiply_and_divide.b = [a, b](int x, int y){cout << x / y << endl;};

    many_functors.push_back(add_and_subtract);
    many_functors.push_back(multiply_and_divide);

    for (auto functors : many_functors){
        functors.a();
        functors.b();
    }

}
like image 890
Eric B Avatar asked Jun 05 '13 16:06

Eric B


People also ask

How do you capture a member variable in lambda?

To capture the member variables inside lambda function, capture the “this” pointer by value i.e. std::for_each(vec. begin(), vec. end(), [this](int element){ //.... }

Can lambdas be Constexpr?

constexpr lambda expressions in C++ Visual Studio 2017 version 15.3 and later (available in /std:c++17 mode and later): A lambda expression may be declared as constexpr or used in a constant expression when the initialization of each data member that it captures or introduces is allowed within a constant expression.

How do you call a lambda function in C++?

Only variables that are mentioned in the lambda body are captured when a capture-default is used. To use lambda expressions in the body of a class member function, pass the this pointer to the capture clause to provide access to the member functions and data members of the enclosing class.

How do you declare a lambda function?

Syntax. Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).


2 Answers

If you just want to construct two_functors at various times, but execute them later in sequence all at once, you could just use the captured data.

struct two_functors
{
    function<void ()> a;
    function<void ()> b;
};

int main()
{
    vector<two_functors> many_functors;

    int a = 2;
    int b = 3;
    double c = 4.7;
    double d = 8.4;

    two_functors add_and_subtract {
        [a, b](){cout << a + b << endl;},
        [c, d](){cout << c - d << endl;}
    };

    two_functors multiply_and_divide {
        [c, d](){cout << c * d << endl;},
        [a, b](){cout << a / b << endl;}
    };

    many_functors.push_back(add_and_subtract);
    many_functors.push_back(multiply_and_divide);

    for (auto functors : many_functors){
        functors.a();
        functors.b();
    }
}
like image 96
baysmith Avatar answered Sep 28 '22 16:09

baysmith


That's essentially a tuple. You can see how the interface is implemented for that.

template< class F0, class F1 >
struct two_functors {
   F0 func0;
   F1 func1;
};

template< class F0, class F1 >
two_functors<F0, F1> make_two_functor( F0&& f0, F1&& f1 )
{ 
   // Added [std::forward][2]
   return two_functors<F0,F1>( std::forward<F0>(f0), std::forward<F1>(f1) ); 
}
like image 36
Steven Maitlall Avatar answered Sep 28 '22 16:09

Steven Maitlall