Parent
Part of #5. Depends on #71.
Summary
Implement correct copy and move constructors and assignment operators for SmallVector. These are non-trivial because move semantics differ depending on whether the source is in inline or heap mode.
The challenge with move
For heap-mode: steal the pointer (O(1)), same as std::vector.
For inline-mode: cannot steal the pointer — the inline buffer is part of the object itself, so moving the object doesn't move the buffer. Each element must be individually move-constructed into the destination's inline buffer.
// Heap mode — O(1) move
SmallVector(SmallVector&& other) {
if (!other.is_small()) {
heap_ptr = other.heap_ptr; // steal
other.heap_ptr = nullptr;
} else {
// Inline mode — O(N) move, no way around it
for (size_t i = 0; i < other.size_; ++i)
new(data() + i) T(std::move(other.data()[i]));
}
size_ = other.size_;
capacity_ = other.capacity_;
other.size_ = 0;
}
Scope
- Copy constructor — always element-wise copy
- Move constructor — pointer steal if heap, element-wise move if inline
- Copy assignment operator — handle self-assignment, destroy existing elements
- Move assignment operator — same inline/heap distinction as move ctor
- Constructor from
std::initializer_list<T>
- Constructor from iterator range
(InputIt first, InputIt last)
Iterator invalidation
After a move, all iterators into the source are invalidated. After a copy, iterators into the destination are new.
Testing requirements
- Move from inline → destination is inline, source is empty
- Move from heap → destination has stolen pointer, source pointer is null
- Self-assignment must be a no-op
- Assign smaller vector into larger — excess elements must be destroyed
- Assign larger vector into smaller — must trigger heap transition if needed
Parent
Part of #5. Depends on #71.
Summary
Implement correct copy and move constructors and assignment operators for
SmallVector. These are non-trivial because move semantics differ depending on whether the source is in inline or heap mode.The challenge with move
For heap-mode: steal the pointer (O(1)), same as
std::vector.For inline-mode: cannot steal the pointer — the inline buffer is part of the object itself, so moving the object doesn't move the buffer. Each element must be individually move-constructed into the destination's inline buffer.
Scope
std::initializer_list<T>(InputIt first, InputIt last)Iterator invalidation
After a move, all iterators into the source are invalidated. After a copy, iterators into the destination are new.
Testing requirements