template <typename T> class myvector { ... private: using self_type = myvector引数の other の size() の数だけ領域を確保し、placement new でコピーしていきます。 コピー元が自分自身のため、check_length() と使ったサイズチェックは不要となります。; ... public: /** * @brief Copy constructor. Constructs the container with the copy of the * contents of other. * @param[in,out] other: Another container to be used as source to initialize the * elements of the container with. */ myvector(const self_type& other) : heap_(other.size() ? mymalloc(other.size()) : nullptr), size_(other.size()), capacity_(other.size()) { pointer p = heap_; for (auto i = other.cbegin(); i != other.cend(); ++i) { new(p++) value_type(*i); } } ... };
全ソースコード: https://github.com/suomesta/myvector/tree/master/010
0 件のコメント:
コメントを投稿