我正在尝试使用QtConcurrent::mapped到QVector<QString>. 我已经尝试了很多方法,但似乎总是存在重载的问题。
QVector<QString> words = {"one", "two", "three", "four"};\n\nusing StrDouble = std::pair<QString, double>;\n\nQFuture<StrDouble> result = QtConcurrent::mapped<StrDouble>(words, [](const QString& word) -> StrDouble {\n return std::make_pair(word + word, 10);\n});\nRun Code Online (Sandbox Code Playgroud)\n\n该片段返回以下错误:
\n\n/home/lhahn/dev/cpp/TestLambdaConcurrent/mainwindow.cpp:23: error: no matching function for call to \xe2\x80\x98mapped(QVector<QString>&, MainWindow::MainWindow(QWidget*)::<lambda(const QString&)>)\xe2\x80\x99\n });\n ^\nRun Code Online (Sandbox Code Playgroud)\n\n我看到这篇文章,它说 Qt 找不到 lambda 的返回值,所以你必须使用std::bind它。如果我尝试这个:
using StrDouble = std::pair<QString, double>;\nusing std::placeholders::_1;\n\nauto map_fn = [](const QString& word) -> StrDouble {\n return std::make_pair(word + word, 10.0);\n};\n\nauto wrapper_map_fn = std::bind(map_fn, _1);\n\nQFuture<StrDouble> result = QtConcurrent::mapped<StrDouble>(words, wrapper_map_fn);\nRun Code Online (Sandbox Code Playgroud)\n\n但错误仍然相似:
\n\n/home/lhahn/dev/cpp/TestLambdaConcurrent/mainwindow.cpp:28: error: no matching function for call to \xe2\x80\x98mapped(QVector<QString>&, std::_Bind<MainWindow::MainWindow(QWidget*)::<lambda(const QString&)>(std::_Placeholder<1>)>&)\xe2\x80\x99\n QFuture<StrDouble> result = QtConcurrent::mapped<StrDouble>(words, wrapper_map_fn);\n ^\nRun Code Online (Sandbox Code Playgroud)\n\n我也尝试将 lambda 包裹在里面std::function,但不幸的是类似的结果。
小智 5
以下内容为我编译:
QVector<QString> words = {"one", "two", "three", "four"};
std::function<StrDouble(const QString& word)> func = [](const QString &word) {
return std::make_pair(word + word, 10.0);
};
QFuture<StrDouble> result = QtConcurrent::mapped(words, func);
Run Code Online (Sandbox Code Playgroud)
输出qDebug() << result.results():
(std::pair("oneone",10)、std::pair("twotwo",10)、std::pair("三三",10)、std::pair("fourfour",10))
| 归档时间: |
|
| 查看次数: |
2670 次 |
| 最近记录: |