mercredi 27 avril 2016

How can I convert my conversion function to a lambda expression?

I have a C++ code with basic conversion functions, such as the following:

void convert(double *x_out, double *y_out, double *x_in, double *y_in)
{
        *x_out = *x_in;
        *y_out = *y_in;
}

I then have another function later in my code which takes a function pointer to a conversion function.

void process(void (*converter)(double *x_out, double *y_out, double *x_in, double *y_in), double x1, double y1, double x2, double y2)
{
    (*converter)(x1, y1, x2, y2);
    // do something
    return something;
}

process is called elsewhere in the code.

void my_func(...args...)
{
    process(&convert, _x1_, _y1_, _x2_, _y2_);
}

I want to use a lambda function instead of the function pointer approach.

I can't quite get my head around lambdas.

My best guess so far from reading this http://ift.tt/OEhZWO this http://ift.tt/125N24c and this http://ift.tt/1UhArHZ is:

void my_func(double _x1_, double _y1_, double _x2_, double _y2_)
{

    [&_x1_, &_x2_, &_y1_, &_y2_] -> void
    {
        double x_in = _x1_;
        double y_in = _y1_;
        double x_out = x_in;
        double y_out = y_in;

        // return
        _x2_ = x_out;
        _y2_ = y_out;    
    }

    process(what goes here?, _x1_, _y1_, _x2_, _y2_);

}

I'm fairly sure the declaration of the lambda goes inside the function my_func itself, so that it can capture the local arguments/variables.

But I'm not sure how to call it from process()?

Aucun commentaire:

Enregistrer un commentaire