Boost logo

Boost Users :

Subject: [Boost-users] Can I return std::unique_ptr in a boost::future?
From: Aaron_Wright_at_[hidden]
Date: 2013-11-14 20:05:35


I'm trying to return a std::unique_ptr in a boost::future. I can't get it
to compile. Is it possible? The code wraps a packaged_task in a functor to
make it copyable enough to pass to a boost::asio::io_service, where the
work will be done in another thread. The code works for other return types
that are copyable.

This is what I have for a small test case:

std::unique_ptr< int > get_int_impl(int i)
{
   return std::unique_ptr< int >(new int(i));
}

std::unique_ptr< int > get_int(int i)
{
   typedef boost::packaged_task< std::unique_ptr< int > > Task;

   boost::shared_ptr< Task > task = boost::make_shared< Task
>(boost::bind(&get_int_impl, 42));

   boost::unique_future< std::unique_ptr< int > > future =
task->get_future(); // <--- Error from this line.

   m_service.post(boost::bind(&Task::operator(), task));

   return future.get();
}

BOOST_AUTO_TEST_CASE(test_get_int)
{
   BOOST_CHECK_EQUAL(*get_int(42), 42);
}

The error:

boost/thread/future.hpp(239): error C2248:
'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared
in class 'std::unique_ptr<_Ty>'

---
Aaron Wright

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net