Skip to content

Conversation

@saki7
Copy link
Collaborator

@saki7 saki7 commented Aug 17, 2025

Self-emplace and self-generic-assignment on non-valueless instance ALWAYS leads to UB, because the standard specifies emplace to destruct the contained object before emplacing the new value:

[variant.mod]
Effects: Destroys the currently contained value if valueless_by_exception() is false. Then direct-non-list-initializes...

This does apply also to generic assignment operator as the standard says it delegates to emplace on certain branches:

[variant.assign]
Otherwise, if is_nothrow_constructible_v<Tj, T> || !is_nothrow_move_constructible_v<Tj> is true, equivalent to emplace<j>(std​::​forward<T>(t)).


Our version (rvariant version) of the specification ([rvariant.mod]) allows creating temporary variable on recursive_wrapper, making it bulletproof to self-emplace. However, there's no point of conditionally omitting the assertion on that branch, because doing so would only make the codebase error-prone and has no benefit for end users.


Also fixes the wrong semantics where our implementation of generic assignment operator was not conforming to the standard, creating temporary object in non-permitted branch.

@saki7 saki7 self-assigned this Aug 17, 2025
@saki7 saki7 added bug Something isn't working enhancement New feature or request language-lawyer Implies unclear specification on the C++ standard, or potential misinterpretation/bug on compilers labels Aug 17, 2025
@cppwarningnotifier
Copy link

Environment C++23 C++26
ubuntu24.0414g++-14Debug✅success✅success
Release✅success✅success
21clang++-21Debug✅success✅success
Release✅success✅success
22clang++-22Debug✅success✅success
Release✅success✅success
windows2022v143clDebug✅success✅success
Release✅success✅success

@saki7 saki7 merged commit 6513ef0 into main Aug 17, 2025
20 checks passed
@saki7 saki7 deleted the assert-self-emplace branch August 17, 2025 06:59
@saki7 saki7 added the truly-recursive Issues related to truly recursive types label Aug 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request language-lawyer Implies unclear specification on the C++ standard, or potential misinterpretation/bug on compilers truly-recursive Issues related to truly recursive types

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants