Move boost :: ptr_vector
Given a type that is not copied but moves, I am trying to build boost::ptr_vector
that type and return it from a function. I am using Visual Studio 2013.
struct NonCopyableButMovable
{
using this_type = NonCopyableButMovable;
this_type() {}
this_type(this_type const &) = delete;
this_type & operator =(this_type const &) = delete;
this_type(this_type &&) {} // no default move ctr in VS2013
this_type & operator =(this_type &&) {} // no default move assignment in VS2013
};
boost::ptr_vector<NonCopyableButMovable> make_ptr_vector()
{
boost::ptr_vector<NonCopyableButMovable> res;
//return res; // (1)
//return std::move(res); // (2)
return boost::ptr_vector<NonCopyableButMovable>(); // (3)
}
I can only get (3) to compile: the temporary is ptr_vector
moved. With (1) and (2), the compiler calls boost::new_clone
, which tries to call the NonCopyableButMovable
copy constructor .
According to this FAQ , the "Moving out of functions" section, (1) should work, ie. res
should be moved, not copied.
According to this thread , (2) can be used as a workaround for compilers that are not fully standard-compliant, but for some reason I don't understand is boost::new_clone
also called with (2).
Note that if I replace boost::ptr_vector
with std::vector
, the three return methods are compiled.
Is there something broken in the Boost ptr_containers move semantics with VS2013? What happens in case (2)? What would be the workaround to implement such a ptr_container factory function?
source to share
No one has answered this question yet
See similar questions:
or similar: