009: constructor with size and initial value

前回定義したサイズ指定のコンストラクタによく似た、サイズと初期値を指定するコンストラクタを定義します。7つあるコンストラクタの3つ目の定義になります。
template <typename T>
class myvector
{
...

    /**
     * @brief      Constructor with size and initial value. CConstructs the container
     *             with count copies of elements with value.
     * @param[in]  count: The size of the container.
     * @param[in]  value: The value to initialize elements of the container with.
     * @throw      std::length_error: If count is greater than max_size().
     * @throw      std::bad_alloc: If malloc() fails to allocate storage.
     */
    myvector(size_type count, const value_type& value) :
        heap_(count ? mymalloc(length_check(count)) : nullptr),
        size_(count),
        capacity_(count)
    {
        for (size_type i = 0; i < size_; i++) {
            new(&heap_[i]) value_type(value);
        }
    }
...
};
今回定義したサイズと初期値を指定するコンストラクタと、前回定義したサイズ指定のコンストラクタとの違いは、placement new をする際にコピーコンストラクタを呼ぶのか、それともデフォルトコンストラクタを呼ぶのか、という点のみです。


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

0 件のコメント:

コメントを投稿