Repository navigation
Make the containers at least as noexcept as MSVC STL, and fix their allocator handling - #94
Open
jmgilbert2 wants to merge 4 commits into
Open
jmgilbert2 wants to merge 4 commits into
jmgilbert2 wants to merge 4 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
hash_*andunordered_*), queue, stack, priority_queue, and their iterators. About 230 unconditional and 57 conditional specs.POCMA || is_always_equalconditions are therefore unconditional here.erase(iterator)is unconditional, beyond MSVC: EASTL iterators carry their bucket, so erasing never hashes.2. a8753df: free
swapfor map, multimap, set, multiset and the hash containerseastl::swap(map&, map&)used the generic three-move swap. That swap was slower than the member swap and not noexcept.swaptookconst¶meters, so it could never be called; it now takes non-const references.3. d9e25cf: list, slist and basic_string propagate allocators (behavior change)
eastl::allocatorobjects always compare equal, so code using them sees no change. The fixed containers keepfixed_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.deque::operator=(deque&&)now clears and swaps, so it no longer allocates and its noexcept holds with exceptions on.Decisions to review
noexcept(is_always_equal), but EASTL can't promise that: allocators can be replaced at runtime (set_allocator), andeastl::allocatoritself can be user-defined (EASTL_USER_DEFINED_ALLOCATOR). stack and priority_queue over vector follow vector here.Testing
is_nothrow_swappable<vector<int>>is now true.noexcept(expr)against MSVC STL with cl and clang-cl, in C++17 and latest, and in latest with exceptions on.🤖 Generated with Claude Code