gpt4 book ai didi

c++ - 带有 packaged_task 的模板无法编译

转载 作者:太空狗 更新时间:2023-10-29 21:43:31 25 4
gpt4 key购买 nike

我正在玩 Antony Williams - C++ Concurrency in Action 一书中的示例 4.14,其中使用 std::packaged_task 和 std::thread 模拟 std::async。

为什么当我取消注释行时此代码无法编译以及如何重写模板以使其工作?

#include <iostream>
#include <future>
#include <thread>
#include <string>

void func_string(const std::string &x) {}

void func_int(int x) {}

template <typename F, typename A>
std::future<typename std::result_of<F(A&&)>::type> spawn_task(F &&f, A &&a) {
typedef typename std::result_of<F(A&&)>::type result_type;
std::packaged_task<result_type(A&&)> task(std::move(f));
std::future<result_type> res(task.get_future());
std::thread t(std::move(task), std::move(a));
t.detach();
return res;
}


int main () {
std::string str = "abc";

// auto res1 = spawn_task(func_string, str);
// res1.get();
auto res2 = spawn_task(func_int, 10);
res2.get();

return 0;
}

编译错误:

nnovzver@archer /tmp $ clang++ -std=c++11 -lpthread temp.cpp && ./a.out
In file included from temp.cpp:2:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2/future:38:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2/functional:1697:56: error: no type
named 'type' in 'std::result_of<std::packaged_task<void (std::basic_string<char> &)> (std::basic_string<char>)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2/thread:135:41: note: in instantiation
of template class 'std::_Bind_simple<std::packaged_task<void (std::basic_string<char> &)>
(std::basic_string<char>)>' requested here
_M_start_thread(_M_make_routine(std::__bind_simple(
^
temp.cpp:15:15: note: in instantiation of function template specialization 'std::thread::thread<std::packaged_task<void
(std::basic_string<char> &)>, std::basic_string<char> >' requested here
std::thread t(std::move(task), std::move(a));
^
temp.cpp:24:15: note: in instantiation of function template specialization 'spawn_task<void (&)(const
std::basic_string<char> &), std::basic_string<char> &>' requested here
auto res1 = spawn_task(func_string, str);
^
In file included from temp.cpp:2:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2/future:38:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2/functional:1726:50: error: no type
named 'type' in 'std::result_of<std::packaged_task<void (std::basic_string<char> &)> (std::basic_string<char>)>'
typename result_of<_Callable(_Args...)>::type
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
2 errors generated.

最佳答案

如果您查看 documentation异步,你想要模拟的特性,它会衰减所有 future 的模板参数。它是有效的,因为它像这样工作得更好,如图所示 here :

template <typename T_>
using decay_t = typename std::decay<T_>::type;
template< class T >
using result_of_t = typename std::result_of<T>::type;

template <typename F, typename A>
std::future<result_of_t<decay_t<F>(decay_t<A>)>> spawn_task(F &&f, A &&a) {
using result_t = result_of_t<decay_t<F>(decay_t<A>)>;
std::packaged_task< result_t(decay_t<A>)> task(std::forward<F>(f));
auto res = task.get_future();
std::thread t(std::move(task), std::forward<A>(a));
t.detach();
return res;
}

关于c++ - 带有 packaged_task 的模板无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22709480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com