Skip to content

Make the containers at least as noexcept as MSVC STL, and fix their allocator handling - #94

Open
jmgilbert2 wants to merge 4 commits into
masterfrom
feat/noexcept-containers
Open

jmgilbert2 wants to merge 4 commits into
masterfrom
feat/noexcept-containers

Conversation

@jmgilbert2

Copy link
Copy Markdown
Owner

Part A2 of the noexcept pass, for the containers. Some commits change behavior; each is listed below. Two more commits will follow: splice/merge with unequal allocators (an assertion, since std makes it undefined), and noexcept beyond MSVC where EASTL can't throw.

1. d345c8c: containers at least as noexcept as MSVC STL (exception specifications only)

  • Covers vector, basic_string, deque, list, slist, the red-black tree (map/multimap/set/multiset), the hash table (hash_* and unordered_*), queue, stack, priority_queue, and their iterators. About 230 unconditional and 57 conditional specs.
  • vector, deque, the tree and the hash table exchange their allocator objects in swap (move assignment is clear + swap). MSVC's POCMA || is_always_equal conditions are therefore unconditional here.
  • Hash-table erase(iterator) is unconditional, beyond MSVC: EASTL iterators carry their bucket, so erasing never hashes.

2. a8753df: free swap for map, multimap, set, multiset and the hash containers

  • std declares these, and EASTL lacked them, so eastl::swap(map&, map&) used the generic three-move swap. That swap was slower than the member swap and not noexcept.
  • The hash table's own free swap took const& parameters, so it could never be called; it now takes non-const references.

3. d9e25cf: list, slist and basic_string propagate allocators (behavior change)

  • swap now exchanges the contents and the allocators in O(1), with no allocator comparison and no copy fallback. Move assignment is built on it, and both are noexcept in either exception mode.
  • This matches what vector, deque, the tree and the hash table already did. Default eastl::allocator objects always compare equal, so code using them sees no change. The fixed containers keep fixed_swap.

4. c721583: allocator-extended move constructors and deque move assignment (bug fixes)

  • (this_type&&, const allocator_type&) now uses the given allocator, as std requires. With equal allocators it takes x's storage; with unequal allocators it moves the elements one by one and leaves x empty.
  • Fixed for unequal allocators:
    • vector returned an empty vector and destroyed x's elements;
    • deque took x's allocator;
    • list and slist copied instead of moving;
    • basic_string left x's characters.
  • deque::operator=(deque&&) now clears and swaps, so it no longer allocates and its noexcept holds with exceptions on.

Decisions to review

  • vector's allocator-extended move constructor is not noexcept, and neither are basic_string's and slist's. MSVC uses noexcept(is_always_equal), but EASTL can't promise that: allocators can be replaced at runtime (set_allocator), and eastl::allocator itself can be user-defined (EASTL_USER_DEFINED_ALLOCATOR). stack and priority_queue over vector follow vector here.

Testing

  • New static_assert blocks in TestVector, TestString, TestDeque, TestList, TestSList, TestMap, TestSet, TestHash and TestExtra.
  • Runtime tests with two unequal allocator instances: swap, move assignment and the allocator-extended move constructors. They check contents, allocator identity, moves versus copies, and that each block is freed by its own allocator. They fail on master.
  • TestAllocatorPropagate and TestContainerBehaviour were updated to the new behavior. TestUtility's is_nothrow_swappable<vector<int>> is now true.
  • Probes compare noexcept(expr) against MSVC STL with cl and clang-cl, in C++17 and latest, and in latest with exceptions on.
  • All 8 configurations pass with no warning change against the master baseline. They are cl and clang-cl in latest Debug, latest Release and C++17 Release with exceptions off, plus latest Release with exceptions on.

🤖 Generated with Claude Code

jmgilbert2 and others added 4 commits November 10, 2026 10:57
Exception specifications of vector, basic_string, deque, list, slist, the
red-black tree (map, multimap, set, multiset), the hash table (hash_map,
hash_multimap, hash_set, hash_multiset and the unordered_* aliases), queue,
stack, priority_queue and their iterators now match or exceed MSVC STL 14.51 in
C++17 and latest. Where MSVC's spec is conditional, the same condition is used
with EASTL traits.

Allocator conditions: EASTL has no allocator_traits. vector, deque, the tree and
the hash table exchange the allocator objects together with the storage in swap
(move assignment is clear + swap), so the POCMA || is_always_equal conditions are
unconditional here. list, slist and basic_string still compare allocators and
copy when they differ, so their swap and move assignment are left unchanged in
this commit.

Only exception specifications change; no code generation changes. Tests are
static_asserts that fail on the previous headers. TestUtility's
is_nothrow_swappable<vector<int>> expectation is now true.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
std declares a free swap for each container. EASTL only had the base-class
overloads (rbtree, hashtable), so for map, multimap, set, multiset and the
hash_* (and unordered_*) containers the generic eastl::swap won overload
resolution: three moves instead of the member swap, and not noexcept.

Each container now has swap(C&, C&) noexcept(noexcept(a.swap(b))) calling the
member swap. The free swap of hashtable took const references, so it could never
be called; it now takes non-const references like rbtree's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tring

Behavior change for unequal allocators. vector, deque, the red-black tree and the
hash table already exchange their allocator objects together with their storage
(the effective behavior of propagate_on_container_swap = true). list, slist and
basic_string instead compared allocators and, when they differed, copied the
contents in swap and move assignment.

Now all of them behave the same: swap exchanges the contents and the allocators
in O(1) without touching any element, and move assignment is built on that swap.
Every node or heap block is still freed by the allocator that allocated it, swap
no longer requires a copyable value_type, and swap, the free swap, move
assignment, string::assign(this_type&&) and slist(this_type&&) are noexcept in
both exception configurations. Default eastl::allocator objects always compare
equal, so code using them sees no change. fixed_list, fixed_slist and
fixed_string keep their element-wise fixed_swap.

The allocator-extended move constructors keep their previous behavior here.

Tests: TestAllocatorPropagate now checks move assignment and swap for list,
slist and basic_string, and TestList/TestSList/TestString swap and move-assign
containers with two unequal allocator instances (contents, allocator identity,
no element copies, each block freed by its own allocator; string in small and
heap cases).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The (this_type&&, const allocator_type&) constructors of vector, deque, list,
slist and basic_string now construct with the given allocator, as std requires:
with equal allocators they take x's storage and keep the given allocator; with
unequal allocators they move x's elements one by one into storage from the given
allocator and leave x empty.

Bugs fixed (unequal allocators):
- vector returned an empty vector and destroyed x's elements (a temporary took
  them and was destroyed).
- deque took x's allocator instead of the given one (it was built on swap).
- list and slist copied the elements instead of moving them, and basic_string
  left x with its characters.

deque::operator=(this_type&&) no longer builds a temporary deque: it clears and
swaps, so it allocates nothing and its noexcept holds with exceptions enabled too.

vector's allocator-extended move constructor is no longer noexcept, since its
unequal branch allocates. MSVC makes it noexcept(is_always_equal), but EASTL has
no allocator_traits, an allocator can be replaced at runtime (set_allocator), and
eastl::allocator itself can be user-defined (EASTL_USER_DEFINED_ALLOCATOR), so no
always-equal guarantee can be given; basic_string's and slist's stay non-noexcept
for the same reason.

Tests: runtime tests with two unequal allocator instances for vector, deque,
list, slist and basic_string (given allocator used, elements moved not copied,
x left empty) that fail on the previous headers; TestContainerBehaviour now
expects moves and enables the vector, deque and basic_string unequal cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant