Fix move assignment from the same object

This commit is contained in:
Josh Holtrop 2026-02-25 00:49:33 -05:00
parent 17a647b897
commit ec937b2395
2 changed files with 14 additions and 1 deletions

View File

@ -81,7 +81,7 @@ public:
rcp & operator=(rcp && other) noexcept rcp & operator=(rcp && other) noexcept
{ {
if (ptr != other.ptr) if (this != &other)
{ {
if (ptr) if (ptr)
{ {

View File

@ -191,6 +191,18 @@ void test_move_assignment()
assert(mybase_destruct == destructed_before + 1); assert(mybase_destruct == destructed_before + 1);
} }
void test_move_assignment_same_object()
{
MyB a = MyB::create(1, 2);
MyB b = a;
assert(a.use_count() == 2);
b = std::move(a);
assert(!a);
assert(b);
assert(b->x == 1);
assert(b.use_count() == 1);
}
void test_move_assignment_releases_existing() void test_move_assignment_releases_existing()
{ {
int constructed_before = mybase_construct; int constructed_before = mybase_construct;
@ -395,6 +407,7 @@ int main(int argc, char * argv[])
test_reset(); test_reset();
test_move_constructor(); test_move_constructor();
test_move_assignment(); test_move_assignment();
test_move_assignment_same_object();
test_move_assignment_releases_existing(); test_move_assignment_releases_existing();
test_upcast(); test_upcast();
test_move_upcast(); test_move_upcast();