From bb9ad59f42160d12ea3db9c4d01d06c67cd00559 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Wed, 8 Oct 2025 11:39:41 +0800 Subject: [PATCH 1/4] Remove `_Assign` --- stl/inc/any | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/stl/inc/any b/stl/inc/any index 9aba1b3fb41..3254308a717 100644 --- a/stl/inc/any +++ b/stl/inc/any @@ -176,12 +176,16 @@ public: // Assignment [any.assign] any& operator=(const any& _That) { - _Assign(_That); + any _Tmp = _That; + reset(); + _Move_from(_Tmp); return *this; } any& operator=(any&& _That) noexcept { - _Assign(_STD move(_That)); + any _Tmp = _STD move(_That); + reset(); + _Move_from(_Tmp); return *this; } @@ -190,7 +194,9 @@ public: int> = 0> any& operator=(_ValueType&& _Value) { // replace contained value with an object of type decay_t<_ValueType> initialized from _Value - _Assign(_STD forward<_ValueType>(_Value)); + any _Tmp = _STD forward<_ValueType>(_Value); + reset(); + _Move_from(_Tmp); return *this; } @@ -306,11 +312,6 @@ private: } } - void _Assign(any _That) noexcept { // intentionally pass by value - reset(); - _Move_from(_That); - } - template _Decayed& _Emplace(_Types&&... _Args) { // emplace construct _Decayed if constexpr (_Any_is_trivial<_Decayed>) { From 2294cfeae4951fc2a46c533915350ee543f9eaf6 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Wed, 8 Oct 2025 11:40:41 +0800 Subject: [PATCH 2/4] Combine `reset` and `_Move_from` --- stl/inc/any | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/stl/inc/any b/stl/inc/any index 3254308a717..39a789a3238 100644 --- a/stl/inc/any +++ b/stl/inc/any @@ -177,15 +177,13 @@ public: // Assignment [any.assign] any& operator=(const any& _That) { any _Tmp = _That; - reset(); - _Move_from(_Tmp); + _Reset_and_move_from(_Tmp); return *this; } any& operator=(any&& _That) noexcept { any _Tmp = _STD move(_That); - reset(); - _Move_from(_Tmp); + _Reset_and_move_from(_Tmp); return *this; } @@ -195,8 +193,7 @@ public: any& operator=(_ValueType&& _Value) { // replace contained value with an object of type decay_t<_ValueType> initialized from _Value any _Tmp = _STD forward<_ValueType>(_Value); - reset(); - _Move_from(_Tmp); + _Reset_and_move_from(_Tmp); return *this; } @@ -237,10 +234,8 @@ public: void swap(any& _That) noexcept { any _Old = _STD move(*this); - reset(); - _Move_from(_That); - _That.reset(); - _That._Move_from(_Old); + _Reset_and_move_from(_That); + _That._Reset_and_move_from(_Old); } // Observers [any.observers] @@ -312,6 +307,11 @@ private: } } + void _Reset_and_move_from(any& _That) noexcept { + reset(); + _Move_from(_That); + } + template _Decayed& _Emplace(_Types&&... _Args) { // emplace construct _Decayed if constexpr (_Any_is_trivial<_Decayed>) { From fb0fb10be958e17a1c51412fc21cde9ad1ba8a3f Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Wed, 15 Oct 2025 18:35:35 +0800 Subject: [PATCH 3/4] Precondition --- stl/inc/any | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stl/inc/any b/stl/inc/any index 39a789a3238..1d2518ab598 100644 --- a/stl/inc/any +++ b/stl/inc/any @@ -289,6 +289,7 @@ private: } void _Move_from(any& _That) noexcept { + _STL_INTERNAL_CHECK(_Storage._TypeData == 0); // !has_value() _Storage._TypeData = _That._Storage._TypeData; switch (_Rep()) { case _Any_representation::_Small: @@ -314,6 +315,7 @@ private: template _Decayed& _Emplace(_Types&&... _Args) { // emplace construct _Decayed + _STL_INTERNAL_CHECK(_Storage._TypeData == 0); // !has_value() if constexpr (_Any_is_trivial<_Decayed>) { // using the _Trivial representation auto& _Obj = reinterpret_cast<_Decayed&>(_Storage._TrivialData); From 3e12cc1f6f3cee4e9825f913e5c87326052c6385 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Wed, 15 Oct 2025 18:47:03 +0800 Subject: [PATCH 4/4] Use `_Tmp` (which is more regularly used in this project e.g. std::swap and ranges::swap) --- stl/inc/any | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stl/inc/any b/stl/inc/any index 1d2518ab598..c9d30736a57 100644 --- a/stl/inc/any +++ b/stl/inc/any @@ -233,9 +233,9 @@ public: } void swap(any& _That) noexcept { - any _Old = _STD move(*this); + any _Tmp = _STD move(*this); _Reset_and_move_from(_That); - _That._Reset_and_move_from(_Old); + _That._Reset_and_move_from(_Tmp); } // Observers [any.observers]