std::async
是否喜欢使用模板功能?我试图将laugh
std::reverse
作为一个异步任务bu得到编译时错误.
我试图使用更简单的函数(foo和bar),并发现只有非模板功能正在运行.
#include <algorithm> #include <future> #include <string> void foo(std::string::iterator first,std::string::iterator last) { } template<class BidirectionalIterator> void bar(BidirectionalIterator first,BidirectionalIterator last) { } int main() { std::string str = "Lorem ipsum,dolor sit amet"; auto result_reverse = std::async(std::reverse,str.begin(),str.end()); // Compile-time error auto result_foo = std::async(foo,str.end()); auto result_bar = std::async(bar,str.end()); // Compile-time error result_reverse.get(); result_foo.get(); result_bar.get(); }
编译错误如下:
main.cpp: In function ‘int main()’: main.cpp:18:71: erreur: no matching function for call to ‘async(<unresolved overloaded function type>,std::basic_string<char>::iterator,std::basic_string<char>::iterator)’ main.cpp:18:71: note: candidates are: /usr/include/c++/4.6/future:1355:5: note: template<class _Fn,class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch,_Fn&&,_Args&& ...) /usr/include/c++/4.6/future:1378:5: note: template<class _Fn,class ... _Args> typename std::__async_sfinae_helper<typename std::decay<_Functor>::type,_Fn,_Args ...>::type std::async(_Fn&&,_Args&& ...) main.cpp:18:71: erreur: unable to deduce ‘auto’ from ‘<expression error>’ main.cpp:20:62: erreur: no matching function for call to ‘async(<unresolved overloaded function type>,std::basic_string<char>::iterator)’ main.cpp:20:62: note: candidates are: /usr/include/c++/4.6/future:1355:5: note: template<class _Fn,_Args&& ...) main.cpp:20:62: erreur: unable to deduce ‘auto’ from ‘<expression error>’
但是,当我手动指定模板instanciation,如std :: async(std :: reverse< std :: string :: iterator> str.begin(),str.end())时,它会通过.
解决方法
原文地址:https://www.jb51.cc/c/111426.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。