mardi 27 janvier 2015

C++ Lambda Wrapper: Should one prefer to have arguments captured or passed?

I'm writing a wrapper function around some code to maximize its reuse. It looks something like the following:



template <class Func>
void do_work(const int *R, Func extra_work)
{
//do some work
int i = ...
int j = ...
extra_work(i,j); //Using extra_lamb_1
//extra_work(i,j,R); //Using extra_lamb_2
}


And the wrapper looks like the following:



void work_wrapper(const int *R, int *Q)
{
auto extra_lamb_1 = [R,Q] (int i, int j) //Captures R
{
//Do extra stuff
};

/*auto extra_lamb_2 = [Q] (int i, int j, int *R) //Passes R
{
//Do extra stuff
};*/

do_work(R,extra_lamb_1);
//do_work(R,extra_lamb_2);
}


My question is, given this situation, is extra_lamb_1 preferred to extra_lamb_2 or vice versa? Why?


Aucun commentaire:

Enregistrer un commentaire