[Range, Container] range of to-be-moved values ?
Given a boost::container::vector<C> vec; I can write vec.push_back(move(value)) for a move-only (non-copyable) type C, instead of vec.push_back(value) as usual. But what would I write instead of vec.insert(pos,b,e) where (b,e] is a range of values? I'm particularly fond of Range concept, rather than loose iterators, so my function would be something that takes something that works as a Range; e.g. template <typename R> void prepend (const R& src_range) { auto b= boost::begin(src_range); auto e= boost::end(src_range); vec.insert(vec.begin(), b, e); } An rvalue reference to a range is not the same thing as a range of values that I may move from. I could have a differently named function and pass it a range with the implicit assumption that the function will modify the values, but is there a proper way to indicate that as part of the type? src_range can be, say, a pair of (non-const) iterator s as opposed to a pair of const_iterator s, or otherwise range_iterator<R>::type is dereferencable to a const value or not. But that itself is hard to see in general let alone overload against! If my function were called prepend_and_destroy_original(src) it might clue in the caller, but in general it is surprising that a parameter of (const R&) will modify the values. src itself doesn't change, and now refers to the same range of values which are now swapped out with defaults. With built-in pointers and even shared_ptr I can distinguish between an in-out parameter and a parameter that dereferences to a modifiable or const value. But what about with Ranges? Even with lower-case ranges provided by two iterators, How can you call something like boost::container::vector v2 (InIt_start, InIt_end); to convert a std::vector or some other container of move-only items? Only if the types match exactly can the vector(vector&&) constructor be used. —John
On 05-02-2014 10:49, John M. Dlugosz wrote:
Given a boost::container::vector<C> vec; I can write vec.push_back(move(value)) for a move-only (non-copyable) type C, instead of vec.push_back(value) as usual. But what would I write instead of vec.insert(pos,b,e) where (b,e] is a range of values?
boost::push_front( vec, range ) ? http://www.boost.org/doc/libs/1_55_0/libs/range/doc/html/range/reference/alg... -Thorsten
On Wed, Feb 05, 2014 at 01:07:12PM +0100, Thorsten Ottosen wrote:
On 05-02-2014 10:49, John M. Dlugosz wrote:
But what would I write instead of vec.insert(pos,b,e) where (b,e] is a range of values?
Is (b,e] supposed to be a range half-open on the left or a typo for the usual [b,e) half-open on the right convention? -- Lars Viklund | zao@acc.umu.se
On 2/5/2014 6:16 AM, Lars Viklund wrote:
On Wed, Feb 05, 2014 at 01:07:12PM +0100, Thorsten Ottosen wrote:
On 05-02-2014 10:49, John M. Dlugosz wrote:
But what would I write instead of vec.insert(pos,b,e) where (b,e] is a range of values?
Is (b,e] supposed to be a range half-open on the left or a typo for the usual [b,e) half-open on the right convention?
Typo; meant the usual convention. Mismatched bracket styles is difficult for me — one reason I didn't become a quantum mechanic.
On 2/5/2014 6:07 AM, Thorsten Ottosen wrote:
On 05-02-2014 10:49, John M. Dlugosz wrote:
Given a boost::container::vector<C> vec; I can write vec.push_back(move(value)) for a move-only (non-copyable) type C, instead of vec.push_back(value) as usual. But what would I write instead of vec.insert(pos,b,e) where (b,e] is a range of values?
boost::push_front( vec, range )
?
Wouldn't that still try and _copy_ the values from range?
On 05-02-2014 17:38, John M. Dlugosz wrote:
On 2/5/2014 6:07 AM, Thorsten Ottosen wrote:
On 05-02-2014 10:49, John M. Dlugosz wrote:
Given a boost::container::vector<C> vec; I can write vec.push_back(move(value)) for a move-only (non-copyable) type C, instead of vec.push_back(value) as usual. But what would I write instead of vec.insert(pos,b,e) where (b,e] is a range of values?
boost::push_front( vec, range )
?
Wouldn't that still try and _copy_ the values from range?
Right. Hopefully get a make_move_range(rng) some day. Trivial to make, and you can apply such a beast to the second argument above to get what you want. regards Thorsten
On 05/02/2014 06:49 a.m., John M. Dlugosz wrote:
Given a boost::container::vector<C> vec; I can write vec.push_back(move(value)) for a move-only (non-copyable) type C, instead of vec.push_back(value) as usual. But what would I write instead of vec.insert(pos,b,e) where (b,e] is a range of values?
You would use `move_iterator`s: vec.insert(pos, boost::make_move_iterator(b), boost::make_move_iterator(e)); http://www.boost.org/doc/libs/1_55_0/doc/html/move/move_iterator.html Regards, -- Agustín K-ballo Bergé.- http://talesofcpp.fusionfenix.com
participants (4)
-
Agustín K-ballo Bergé
-
John M. Dlugosz
-
Lars Viklund
-
Thorsten Ottosen