diff --git a/include/rcp.h b/include/rcp.h index 4a8b86b..a670d45 100644 --- a/include/rcp.h +++ b/include/rcp.h @@ -81,7 +81,7 @@ public: rcp & operator=(rcp && other) noexcept { - if (ptr != other.ptr) + if (this != &other) { if (ptr) { diff --git a/tests.cpp b/tests.cpp index 5c999bd..ec4c4de 100644 --- a/tests.cpp +++ b/tests.cpp @@ -191,6 +191,18 @@ void test_move_assignment() 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() { int constructed_before = mybase_construct; @@ -395,6 +407,7 @@ int main(int argc, char * argv[]) test_reset(); test_move_constructor(); test_move_assignment(); + test_move_assignment_same_object(); test_move_assignment_releases_existing(); test_upcast(); test_move_upcast();