Search code examples
c++qtstd-function

Why does my std::function have no viable conversion?


My Compressor class has field QMap<QString, std::function<DataList(Compressor&, DataList &)> &> *techniques; and static method static DataList & sndStep(DataList &l);. Their type definitions:

typedef QPair<QPointF, QString> Data;
typedef QList<Data> DataList;
typedef QList<DataList> DataTable;

I'm trying to create a QMap which will contain the QString name of technique as Key and technique as Value.

At first, I tried to insert pair ("R0", sndStep){

Compressor::Compressor(const QString& s)
{
  std::function<DataList(Compressor&, Datalist&)> f =  
      [] (DataList & l) {
          return &Compressor::sndStep(DataList & l);
       };

  techniques->insert("R0", f);
}

But I got many errors. One of them is "no viable conversion":

compressor.cpp:8:53: error: no viable conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::function<DataList (Compressor &, DataList &)>' (aka 'function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)>')
std_function.h:402:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
std_function.h:413:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'const std::function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)> &' for 1st argument
std_function.h:422:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)> &&' for 1st argument
std_function.h:446:2: note: candidate template ignored: substitution failure [with _Functor = (lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9), $1 = void]: no type named 'type' in 'std::result_of<(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9) &(Compressor &, QList<QPair<QPointF, QString>> &)>'
compressor.cpp:9:9: note: candidate function

How can I fix this error? Or maybe are there other ways to create a map of functions (not necessarily std::function)?


Solution

  • Here you are about to define f as a function taking two arguments (Compressor&, Datalist&) and returning DataList:

    std::function<DataList(Compressor&, Datalist&)> f
    

    but that's not the signature of the lambda:

        [] (DataList & l) {
            return &Compressor::sndStep(DataList & l);
        };
    

    which only takes one argument (DataList&) and returns something not known from the code snippet you've shown.