A deep dive into implementing a custom std::vector<T> from scratch in C++. Covers the full class template including constructors (default, initializer list, copy, move), copy/move assignment operators, destructor, and the rule-of-five. Explains memory management using aligned operator new, custom deleters with unique_ptr, and uninitialized memory algorithms (std::uninitialized_copy, std::uninitialized_move). Walks through implementing reserve(), resize(), push_back() with fast/slow paths, emplace_back(), pop_back(), and a range insert() with careful handling of edge cases like self-referential insertion and iterator invalidation. Unit tests using Google Test are provided throughout, and the complete source is available on Compiler Explorer.

21m read timeFrom accu.org
Post cover image
Table of contents
Unit tests for vector<T>vector member datavector constructorsBasic services of a vector-like classHow to think about adding elements to our container?ConclusionReferences

Sort: