Can I return std::unique_ptr in a boost::future?
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
The error is certainly because unique_future (or future) tries to copy the unique_ptr. Can you tell us your Boost version and compiler?
Le 15/11/13 02:05, Aaron_Wright@selinc.com a écrit :
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>' ---
Hi, please try defining BOOST_VERSION 4 and using boost::future instead and let me know. Best, Vicente
Is this code expected to work then? I just need to upgrade my boost? I'm
currently using 1.49. I just want to make sure before I go through the
work of upgrading boost.
---
Aaron Wright
From: "Vicente J. Botet Escriba"
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>' ---
Hi, please try defining BOOST_VERSION 4 and using boost::future instead and let me know. Best, Vicente _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Upgrading to 1.55 and BOOST_THREAD_VERSION=4 fixed the issue. Thanks.
---
Aaron Wright
From: Aaron_Wright@selinc.com
To: boost-users@lists.boost.org
Date: 11/15/2013 08:14 AM
Subject: Re: [Boost-users] Can I return std::unique_ptr in a
boost::future?
Sent by: "Boost-users"
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>' ---
Hi, please try defining BOOST_VERSION 4 and using boost::future instead and let me know. Best, Vicente _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Le 15/11/13 17:14, Aaron_Wright@selinc.com a écrit :
Is this code expected to work then? I just need to upgrade my boost? I'm currently using 1.49. I just want to make sure before I go through the work of upgrading boost.
No, Boost.Thread with BOOST_THREAD_VERSION = 2 which is the default doesn't provides C++11 move semantics. You need to add #define BOOST_THREAD_VERSION 4 or #define BOOST_THREAD_VERSION 3 before including ant Boost file and change unique_future by future. I suspect that you need at least boost.1.50. Version 3.0.0 - boost 1.50 Breaking changes when BOOST_THREAD_VERSION==3: #6229 Breaking change: Rename the unique_future to future following the c++11. #6266 Breaking change: thread destructor should call terminate if joinable. #6269 Breaking change: thread move assignment should call terminate if joinable. New Features: #6194 Adapt to Boost.Move. I expect that helps, Vicente
---
Aaron Wright
From: "Vicente J. Botet Escriba"
To: boost-users@lists.boost.org Date: 11/14/2013 11:28 PM Subject: Re: [Boost-users] Can I return std::unique_ptr in a boost::future? Sent by: "Boost-users" Le 15/11/13 02:05, Aaron_Wright@selinc.com a écrit :
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>' ---
Hi, please try defining BOOST_VERSION 4 and using boost::future instead and let me know.
Best, Vicente _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Aaron_Wright@selinc.com
-
Gavin Lambert
-
Klaim - Joël Lamotte
-
Vicente J. Botet Escriba