013: constructor with initializer list

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

    /**
     * @brief      Constructs the container with the contents of the initializer list.
     * @param[in]  init: Initializer list to initialize the elements of the container with.
     * @throw      std::length_error: If distance between first and last is greater than max_size().
     * @throw      std::bad_alloc: If malloc() fails to allocate storage.
     */
    myvector(std::initializer_list<value_type> init) :
        heap_(init.size() ? mymalloc(length_check(init.size())) : nullptr),
        size_(init.size()),
        capacity_(init.size())
    {
        pointer p = heap_;
        for (const auto& i: init) {
            new(p++) value_type(i);
        }
    }

...
};
引数の init の size() の数だけ領域を確保し、placement new でコピーしていきます。


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

0 件のコメント:

コメントを投稿