c++ - std::async 可以与模板函数一起使用吗

标签 c++ templates asynchronous c++11 std

std::async 假设使用模板函数?我试过启动 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.begin(), str.end());
    auto result_bar     = std::async(bar, str.begin(), 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, std::basic_string<char>::iterator)’
main.cpp:20:62: 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:20:62: erreur: unable to deduce ‘auto’ from ‘<expression error>’

但是,当我手动指定模板实例时,它通过了,例如std::async(std::reverse<std::string::iterator>, str.begin(), str.end()) .

这是编译器错误 (GCC 4.6.3) 还是定义明确的行为?

最佳答案

可以,但是调用有点不同:

auto result_reverse = std::async([&str]() {
        std::reverse(str.begin(), str.end());
    });

这是因为 std::reverse() 不是一个函数,而是一个函数模板,当它作为一个函数被调用时会变成一个函数。

上面反转了字符串的拷贝并丢弃了结果。要通过引用传递字符串,应将 lambda 表达式更改为以 [&str]() 开头。

关于c++ - std::async 可以与模板函数一起使用吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10620300/

相关文章:

c++ - sleep 语句在 C++ 中无法正常工作

python - 如何显示 Caffe 故障堆栈跟踪?

C++ 模板递归 - 如何解决?

c++ - 成员模板出现奇怪的 PC-Lint 错误

C++ 模板 type_trait enable_if 类是映射

java - 为什么 Spring Boot 应用程序使用 @Async 注解的方法会崩溃

c++ - 没有 if-else 语句的一维卷积(非 FFT)?

c++ - 更改帮助窗口的大小以适合文本

javascript - Node.js - 如何使用 MySQL 查询从 for 循环返回带数组的回调?

javascript - IF 语句和异步函数的控制流程