在std :: async调用中解析<unresolved overloaded =“”function =“”type =“”>

时间:2017-07-05 19:28:28

标签: c++11 asynchronous

无法解决从std :: async调用std :: lower_bound的这个实例。例如:

#include <vector>
#include <algorithm>
#include <future>
#include <iostream>

int main() {
  std::vector<int> v = {1,3,4,6,7};
  auto res = std::async(std::launch::async, std::lower_bound, v.begin(), v.end(), 4);
  std::cout << *res.get() << std::endl;
  return 0;
}

编译器输出看起来它没有实现std :: launch :: async是一个启动策略,它应该使用启动策略的异步调用,但我可能是错的。

test.cpp:8:84: error: no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::vector<int>::iterator, std::vector<int>::iterator, int)’
res = std::async(std::launch::async, std::lower_bound, v.begin(), v.end(), 3);
                                                                            ^
In file included from test.cpp:3:0:
/usr/include/c++/6/future:1709:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_BoundArgs>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
 async(launch __policy, _Fn&& __fn, _Args&&... __args)
 ^~~~~
/usr/include/c++/6/future:1709:5: note:   template argument deduction/substitution failed:
test.cpp:8:84: note:   couldn't deduce template parameter ‘_Fn’
res = std::async(std::launch::async, std::lower_bound, v.begin(), v.end(), 3);
                                                                          ^
In file included from test.cpp:3:0:
/usr/include/c++/6/future:1739:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_BoundArgs>::type ...)>::type> std::async(_Fn&&, _Args&& ...)
 async(_Fn&& __fn, _Args&&... __args)
 ^~~~~
/usr/include/c++/6/future:1739:5: note:   template argument deduction/substitution failed:
/usr/include/c++/6/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_BoundArgs>::type ...)>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {}]’:
test.cpp:8:84:   required from here
/usr/include/c++/6/future:1739:5: error: no type named ‘type’ in ‘class std::result_of<std::launch()>’

1 个答案:

答案 0 :(得分:2)

std::lower_bound是一个函数模板,你必须明确指定它的参数才能传递它:

int main() {
  using V = std::vector<int>;
  V v = {1,3,4,6,7};
  auto res = std::async(std::launch::async, std::lower_bound<V::iterator, int>, v.begin(), v.end(), 4);
  std::cout << *res.get() << std::endl;
  return 0;
}