Skip to content

Move compressed_pair's elements from rvalues instead of copying them - #86

Merged
jmgilbert2 merged 1 commit into
masterfrom
fix/compressed-pair-move
Nov 7, 2026
Merged

jmgilbert2 merged 1 commit into
masterfrom
fix/compressed-pair-move

Conversation

@jmgilbert2

@jmgilbert2 jmgilbert2 commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

compressed_pair only had constructors taking its elements by const reference, and cp_swap copied through a temporary. unique_ptr stores its deleter in a compressed_pair, so it copied the deleter on construction from an rvalue deleter, on move construction, and on swap: a move-only deleter didn't compile, although std supports one (MSVC STL's _Compressed_pair forwards its arguments). Found while adding the deleter constraints in #83; independent of it.

  • compressed_pair and each of its six layouts (neither, first, second or both elements as empty bases, and the two same-type layouts) get a two-element forwarding constructor, constrained on each element being constructible from its argument. Rvalues are moved; lvalues still copy. The existing (const T1&, const T2&) constructor stays, so braced and converting arguments work as before.
  • cp_swap moves instead of copying.

Effect on the containers: vector, list, slist, string and tuple_vector store their allocator in a compressed_pair. Only list and slist pass rvalues to the two-element constructor: their default constructors pass a temporary base_node_type() and a temporary default allocator, which are now moved instead of copied. Allocators passed as lvalues are copied as before. eastl::allocator declares a copy constructor and no move constructor, so moving it calls the same copy constructor, and base_node_type is two pointers: the default containers run the same code. A user allocator with its own move constructor is now moved from that temporary.

Tests:

  • TestExtra: every layout stores move-only elements, and swaps them where both elements have state;
  • TestSmartPtr: unique_ptr<int, MoveOnlyDeleter> constructed from an rvalue deleter, move constructed, move assigned and swapped, and unique_ptr<int[], MoveOnlyArrayDeleter> move constructed, each keeping the right deleter.

On master both fail to compile (copy constructor and copy assignment of the move-only types in compressed_pair and cp_swap).

Verification: VS 2026 cl 14.51 and clang-cl 22, C++17 and latest, Debug and Release: all 8 EASTL suites pass, no new warnings compared with master.

🤖 Generated with Claude Code

compressed_pair only had constructors taking its elements by const
reference, and cp_swap copied through a temporary. So unique_ptr, which
stores its deleter in a compressed_pair, copied the deleter on
construction from an rvalue deleter, on move construction and on swap:
a move-only deleter didn't compile, although std supports one.

A two-element forwarding constructor, constrained on the elements being
constructible from the arguments, is added to compressed_pair and each
of its layouts, and cp_swap moves. Lvalue arguments are still copied;
the containers that store their allocator in a compressed_pair now move
the default-constructed temporary allocator they pass in.

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