011: move constructor

ムーブコンストラクタを定義します。7つあるコンストラクタの5つ目の定義になります。
template <typename T>
class myvector
{
...

private:
    using self_type = myvector;
...

public:
    /**
     * @brief      Move constructor. Constructs the container with the contents of
     *             other using move semantics. After the move, other is guaranteed
     *             to be empty().
     * @param[in,out] other: Another container to be used as source to initialize the
     *                       elements of the container with.
     */
    myvector(self_type&& other) noexcept
    {
        std::swap(heap_, other.heap_);
        std::swap(size_, other.size_);
        std::swap(capacity_, other.capacity_);
    } 
...
};
ムーブコンストラクタの実装は、swap() 関数と同等になります。
まだ定義していませんが、myvector の swap() は O(1) で No-throw guarantee という非常に使い勝手の良いものになります。ムーブコンストラクタは、swap() の中身をそのまま拝借して、実装となります。


全ソースコード: https://github.com/suomesta/myvector/tree/master/011

0 件のコメント:

コメントを投稿