shared_ptr: Fix assignment of polymorphic types

Fix the assignment operator to work with polymorphic types.

Suggested by Nadav.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
This commit is contained in:
Pekka Enberg
2015-02-04 12:17:14 +02:00
parent a6356bd55e
commit 34018b2b65

View File

@@ -326,7 +326,7 @@ public:
}
template <typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
shared_ptr& operator=(const shared_ptr<U>& x) noexcept {
if (this != &x) {
if (*this != x) {
this->~shared_ptr();
new (this) shared_ptr(x);
}
@@ -334,7 +334,7 @@ public:
}
template <typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
shared_ptr& operator=(shared_ptr<U>&& x) noexcept {
if (this != &x) {
if (*this != x) {
this->~shared_ptr();
new (this) shared_ptr(std::move(x));
}