This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
std::packaged_task trunk problem
- From: larsbj at gullik dot org (Lars Gullik Bjønnes)
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 01 Nov 2011 11:55:16 +0100
- Subject: std::packaged_task trunk problem
I am trying out trunk and the std::packaged_task in C++11. I found an
example on usage at
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2709.html
but are unable to make it work.
I wonder if it is the example or if it is the libstdc++ playing up.
#include <iostream>
#include <future>
#include <thread>
template<typename F>
std::future<typename std::result_of<F()>::type> spawn_task(F f)
{
typedef typename std::result_of<F()>::type result_type;
std::packaged_task<result_type()> task(std::move(f));
std::future<result_type> res(task.get_future());
std::thread(std::move(task));
return res;
}
int get_res()
{
return 42;
}
int main()
{
auto f = spawn_task(get_res);
std::cout << "Res = " << f.get() << std::endl;
}
$ /opt/gcc/gcc-trunk/bin/g++ -Wall -Wextra -O2 -std=gnu++0x packaged_task_example.cpp
In file included from /opt/gcc/gcc-trunk/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/functional:56:0,
from /opt/gcc/gcc-trunk/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/future:38,
from packaged_task_example.cpp:2:
/opt/gcc/gcc-trunk/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/tuple: In instantiation of ‘struct std::_Tuple_impl<0ul, std::packaged_task<int()> >’:
/opt/gcc/gcc-trunk/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/tuple:398:11: required from ‘class std::tuple<std::packaged_task<int()> >’
/opt/gcc/gcc-trunk/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/functional:1546:39: required from ‘struct std::_Bind_simple<std::packaged_task<int()>()>’
/opt/gcc/gcc-trunk/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/thread:133:9: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = std::packaged_task<int()>; _Args = {}]’
packaged_task_example.cpp:12:5: required from ‘std::future<typename std::result_of<F()>::type> spawn_task(F) [with F = int (*)(); typename std::result_of<F()>::type = int]’
packaged_task_example.cpp:25:29: required from here
/opt/gcc/gcc-trunk/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/tuple:274:17: error: ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 0ul; _Head = std::packaged_task<int()>; _Tail = {}; std::_Tuple_impl<_Idx, _Head, _Tail ...> = std::_Tuple_impl<0ul, std::packaged_task<int()> >]’ declared to take const reference, but implicit declaration would take non-const
Thanks,
--
Lgb