jeudi 29 août 2019

How to assign a lambda method to an argument by reference

I would like to assign a lambda method by reference in a method.

Here is what I tried:

QList<double> MyClass::calculate( QList<double> input, 
                                  std::function <QString( const double &value )> &myMethod )
{
  double stdDev = standardDeviation(input);
  double mean = mean(input);

  valueToLabel = [ stdDev ]( const double &value ) -> QString
  {
    double normalized = ( value - mean ) / stdDev;
    return QString::number( normalized, 'f', 2 ) + " Std Dev";
  };
}

This compiles. But I fail to get how to call this:

std::function <QString( const double &value )> valueToLabel;
const QList<Break> breaks = calculate( input, &valueToLabel );

Here is the error:

qgsclassificationmethod.cpp:153:91: error: non-const lvalue reference to type 'std::function' cannot bind to a temporary of type 'std::function *' qgsclassificationmethod.h:277:57: note: passing argument to parameter 'valueToLabel' here.

How should I do this. Later, I would like to save this lambda in a variable to use it later.

Aucun commentaire:

Enregistrer un commentaire