Lambda function variables in C ++ 11

There are two ways to use a lambda function variable:

std::function<int(int, int)> x1 = [=](int a, int b) -> int{return a + b;};

//usage
void set(std::function<int(int, int)> x);
std::function<int(int, int)> get();

and

std::function<int(int, int)>* x2 = new std::function<int(int, int)>([=](int a, int b) -> int{return a + b;});

//usage
void set(std::function<int(int, int)>* x);
std::function<int(int, int)>* get();

I would like to know what the differences are because I do not know how the data of the lambda function is stored.

I would like to know the best way in terms of performance, memory usage and the best way to pass a lambda function as an argument or return a lambda function. I would prefer to use pointers if the size of the object of the lambda function is greater than 4 or to avoid errors (if some copy constructor is executed when I do the attribution or if some kind of destructor is done when I don't want to).

How to declare lambda function variables?

EDIT

I want to avoid copying and moving, I want to continue to use the same function again.

How do I change this example?

int call1(std::function<int(int, int)> f){
    return f(1, 2);
}
int call2(std::function<int(int, int)> f){
    return f(4, 3);
}
std::function<int(int, int)>& recv(int s){
    return [=](int a, int b) -> int{return a*b + s;};
}

int main(){
    std::function<int(int, int)> f1, f2;

    f1 = [=](int a, int b) -> int{return a + b;};
    f2 = recv(10);

    call1(f1);
    call2(f1);
    call1(f2);
    call2(f2);
}

recv:

 warning: returning reference to temporary

?

int call1(std::function<int(int, int)>* f){
    return (*f)(1, 2);
}
int call2(std::function<int(int, int)>* f){
    return (*f)(4, 3);
}

std::function<int(int, int)>* recv(int s){
    return new std::function<int(int, int)>([=](int a, int b) -> int{return a*b + s;});
}

int main(){
    std::function<int(int, int)> f1 = [=](int a, int b) -> int{return a + b;};
    std::function<int(int, int)> *f2 = recv(10);

    call1(&f1);
    call2(&f1);
    call1(f2);
    call2(f2);

    delete f2;
}

EDIT ()

- , . , .

+5
3

, . ... .

, , . std::shared_ptr . , , , .

, . , , , , . , shared_ptr, .


++ , , , . , , , "" " ", .

, .

std::function, , - ; - . , ; , . , , vtable + -. , , .

++, std::function . , , . , . , . , , .

, ++, std::function . .

, , :

std::function<int(int, int)> x1 = [=](int a, int b) -> int{return a + b;};

//usage
void set(std::function<int(int, int)> x);
const std::function<int(int, int)> &get();

set(std::move(x1)); //x1 is now *empty*; you can't use it anymore.

set . , get const&; , .

move ? , std::function, . , -, .

+5

std::function. . , .

, ( ) - , .

, , new .

, std::function , . auto :

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

std::cout << add(100,100) << std::endl;
std::cout << add(120,140) << std::endl;
+15

, " ++", - ( std:: function std:: bind) . :

1) , std::. - "X (..)" , , "X" , .

// I want a real name for this (plan to use multiple times, etc.)
// Rarely are you going to do this without moving the lambda around, so
// declaring it as 'auto' is pointless because C++ APIs expect a real type.

std::function<double(double)> computeAbs = [](double d) -> double { return fabs(d); };

// Here, we only ever use the lambda for the call to "X".
// So, just declare it inline. That will enforce our intended usage and probably
// produce more efficient code too.

X([](double r) -> double { return fabs(r); });

2) , - std::. , std::, , .

, , , ptr .. std::. API, "GetFunction", std::, API, .

3) API, std:: function lambdas, .

, r- ( "& &" ) , . , . , std:: function , 'int', , f ( int a) ".

, "f", -, :

template<typename Functor>
double f(Functor absFunc);

, std:: function:

double f(std::function<double(double)> absFunc);

Now, when you call "f ([] (double r) → double {return fabs (r);}), the compiler knows how to handle it. Users get something that" just works ", without having to code your API that you need.

0
source

All Articles