QtConcurrent::mapped
をQVector<QString>
に使用しようとしています。私はすでに多くの方法を試しましたが、オーバーロードには常に問題があるようです。QtConcurrent :: mapsをlambdaで作成する
QVector<QString> words = {"one", "two", "three", "four"};
using StrDouble = std::pair<QString, double>;
QFuture<StrDouble> result = QtConcurrent::mapped<StrDouble>(words, [](const QString& word) -> StrDouble {
return std::make_pair(word + word, 10);
});
このスニペットは、次のエラーを返します。
/home/lhahn/dev/cpp/TestLambdaConcurrent/mainwindow.cpp:23: error: no matching function for call to ‘mapped(QVector<QString>&, MainWindow::MainWindow(QWidget*)::<lambda(const QString&)>)’
});
^
私はQtは、ラムダの戻り値を見つけることができないと言われ、このpostを見たので、あなたはそれでstd::bind
を使用する必要があります。私はこのしようとした場合:
using StrDouble = std::pair<QString, double>;
using std::placeholders::_1;
auto map_fn = [](const QString& word) -> StrDouble {
return std::make_pair(word + word, 10.0);
};
auto wrapper_map_fn = std::bind(map_fn, _1);
QFuture<StrDouble> result = QtConcurrent::mapped<StrDouble>(words, wrapper_map_fn);
しかし、それでもエラーが似ています。
/home/lhahn/dev/cpp/TestLambdaConcurrent/mainwindow.cpp:28: error: no matching function for call to ‘mapped(QVector<QString>&, std::_Bind<MainWindow::MainWindow(QWidget*)::<lambda(const QString&)>(std::_Placeholder<1>)>&)’
QFuture<StrDouble> result = QtConcurrent::mapped<StrDouble>(words, wrapper_map_fn);
^
私もstd::function
内部ラムダが、残念ながら、同様の結果をラップしてみました。
- この例は再現のためのものであり、コード内に変数を取り込むため、ラムダが必要です。
どうやら、QtConcurrentとしてラムダまだ...をサポートしていません。私がソースから読むことができる限り、関数ポインタやメンバ関数ポインタが必要です:https://github.com/qt/qtbase/blob/dev/src/concurrent/qtconcurrentfunctionwrappers.h – Felix