Skip to content

Commit 5648008

Browse files
committed
Fix self-swap and self-assignment
1 parent 91786b7 commit 5648008

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

include/oup/observable_unique_ptr.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ class observable_unique_ptr_base {
292292
/** \param other The other pointer to swap with
293293
*/
294294
void swap(observable_unique_ptr_base& other) noexcept {
295+
if (&other == this) {
296+
return;
297+
}
298+
295299
using std::swap;
296300
swap(block, other.block);
297301
swap(ptr_deleter, other.ptr_deleter);
@@ -973,6 +977,10 @@ class observer_ptr {
973977
/** \param value The existing weak pointer to copy
974978
*/
975979
observer_ptr& operator=(const observer_ptr& value) noexcept {
980+
if (&value == this) {
981+
return *this;
982+
}
983+
976984
if (data) {
977985
pop_ref_();
978986
}
@@ -993,6 +1001,10 @@ class observer_ptr {
9931001
*/
9941002
template<typename U, typename enable = std::enable_if_t<std::is_convertible_v<U*, T*>>>
9951003
observer_ptr& operator=(const observer_ptr<U>& value) noexcept {
1004+
if (&value == this) {
1005+
return *this;
1006+
}
1007+
9961008
if (data) {
9971009
pop_ref_();
9981010
}
@@ -1114,6 +1126,10 @@ class observer_ptr {
11141126
/** \param other The other pointer to swap with
11151127
*/
11161128
void swap(observer_ptr& other) noexcept {
1129+
if (&other == this) {
1130+
return;
1131+
}
1132+
11171133
using std::swap;
11181134
swap(block, other.block);
11191135
swap(data, other.data);

0 commit comments

Comments
 (0)