Boost logo

Boost :

Subject: Re: [boost] C++03 unique_ptr emulation
From: Daniel James (daniel_james_at_[hidden])
Date: 2009-01-08 13:55:21


2009/1/8 Howard Hinnant <hinnant_at_[hidden]>:
>
> Is it practical to do this instead?
>
> class vector
> {
> void push_back(value_type x); // internally move construct from x
> }

I was doing this (which is inspired by either Adobe or David Abrahams):

    template <class U>
    void push_back(U const& x, typename boost::copy_sink<U, T>::type = 0) {
        if(data_.end_ == data_.storage_end_) resize(data_.begin_ ?
size() * 2 : 4);
        std::uninitialized_fill_n(data_.end_, 1, x);
        ++data_.end_;
    }

#if !defined(BOOST_NO_SFINAE)

    template <class U>
    void push_back(U x, typename boost::move_sink<U, T>::type = 0) {
        if(data_.end_ == data_.storage_end_) resize(data_.begin_ ?
size() * 2 : 4);
        data_.end_ = boost::uninitialized_move(&x, &x + 1, data_.end_);
    }

#endif

This is taken from:

http://svn.boost.org/svn/boost/sandbox/move/libs/move/test/vector.hpp

The move header is at:

http://svn.boost.org/svn/boost/sandbox/move/boost/move/move.hpp

It is worth looking at (I can say that objectively as it's mostly
based on other people's ideas).

For emplace, you'd probably only want to do this for a small number of
arguments.

Daniel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk