c++ - 使用 g++8 和 c++20 的 std::async 编译问题
问题描述
我正在尝试使用std::async
. 我写了这段代码
template<transport_type tt>
void stop_update_distances(const std::vector<stop>& stops, stop& from, const general_s& s, const osrm_machine& machine){
//...not important code...
}
void tt_map::update_distances(pqxx::connection& conn, const general_s& s,
const osrm_machine& walk_machine, const osrm_machine& car_machine){
std::vector<std::future<void>> futures;
futures.reserve(stops.size());
for(stop& from : stops){
auto res = std::async(std::launch::async, stop_update_distances<CAR>, stops, from, s, car_machine);
futures.push_back(res);
}
}
但是 g++8 给我返回了这个错误
src/lib/tt_map.cpp: In member function ‘void kp::mp::tt_map::update_distances(pqxx::connection&, const kp::general_s&, const kp::osrm_machine&, const kp::osrm_machine&)’:
src/lib/tt_map.cpp:383:108: error: no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::vector<kp::mp::stop>&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&)’
auto res = std::async(std::launch::async, stop_update_distances<CAR>, stops, from, s, car_machine);
^
In file included from src/lib/tt_map.cpp:11:
/usr/include/c++/8/future:1712:5: note: candidate: ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)’
async(launch __policy, _Fn&& __fn, _Args&&... __args)
^~~~~
/usr/include/c++/8/future:1712:5: note: template argument deduction/substitution failed:
/usr/include/c++/8/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = void (&)(const std::vector<kp::mp::stop>&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&); _Args = {std::vector<kp::mp::stop, std::allocator<kp::mp::stop> >&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&}]’:
src/lib/tt_map.cpp:383:108: required from here
/usr/include/c++/8/future:1712:5: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<kp::mp::stop>, kp::mp::stop, kp::general_s, kp::osrm_machine))(const std::vector<kp::mp::stop>&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&)>’
/usr/include/c++/8/future:1745:5: note: candidate: ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(_Fn&&, _Args&& ...)’
async(_Fn&& __fn, _Args&&... __args)
^~~~~
/usr/include/c++/8/future:1745:5: note: template argument deduction/substitution failed:
/usr/include/c++/8/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {void (&)(const std::vector<kp::mp::stop, std::allocator<kp::mp::stop> >&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&), std::vector<kp::mp::stop, std::allocator<kp::mp::stop> >&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&}]’:
src/lib/tt_map.cpp:383:108: required from here
/usr/include/c++/8/future:1745:5: error: no type named ‘type’ in ‘class std::result_of<std::launch(void (*)(const std::vector<kp::mp::stop>&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&), std::vector<kp::mp::stop>, kp::mp::stop, kp::general_s, kp::osrm_machine)>’
我真的不明白。
我已按照cppreference.com中的示例进行操作。即使它不起作用。
感谢您的任何提示,有什么问题。
解决方案
std::thread
默认情况下,类似的类会复制它们的参数,并将它们按值传递给线程函数。这可能不是您想要的。编译器错误的具体原因是通过非常量引用stop_update_distances
获取stop& from
参数,并且无法在那里传递临时副本。
如果不知道如何使用它的参数,很难说stop_update_distances
,但我猜,你可能想通过引用传递它们。您可以通过将它们包裹起来std::ref
或std::cref
根据需要来做到这一点。像这样的东西:
auto res = std::async(
std::launch::async,
stop_update_distances<CAR>,
std::cref(stops),
std::ref(from),
std::cref(s),
std::cref(car_machine));
小心发出生命周期和访问同步 - 您现在在线程之间共享对象。
推荐阅读
- matlab - for loop-Matlab 中的错误
- apache-spark - Spark SQL 限制与 Hive SQL 限制之间的区别
- google-cloud-run - 在 Cloud Code 中调试时访问 computeMetadata
- .net - 为什么在 .NET4.6 中很少有分析器规则将其有效严重性设置为抑制
- c++ - 如何正确创建合并排序函数?
- git - Git ssh url 到特定文件夹
- google-cloud-platform - 是否可以在本地模拟 Google Cloud Task?
- windows - Powershell:如何在“;”之后获得数字的平均值 象征?(来自一个txt文件)
- javascript - 如何在节点中创建 Twilio 视频连接?
- admob - 仅仅请求应用透明度权限是否足以确保谷歌移动广告符合 iOS14 标准?