Skip to content

Commit 333ac2b

Browse files
aengusjiangAengus.Jiang
andauthored
optional class add self check for move assign (#242)
Co-authored-by: Aengus.Jiang <jiangyinglin@surfilter.com>
1 parent a354f41 commit 333ac2b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/utils/optional.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ namespace detail {
7373
optional& operator=(optional&& other)
7474
noexcept(std::is_nothrow_move_assignable<value_type>::value && std::is_nothrow_move_constructible<value_type>::value)
7575
{
76-
reset();
77-
if(other.holds_value) {
78-
new (static_cast<void*>(std::addressof(uvalue))) value_type(std::move(other.uvalue));
79-
holds_value = true;
76+
if (this != &other) {
77+
reset();
78+
if (other.holds_value) {
79+
new (static_cast<void*>(std::addressof(uvalue))) value_type(std::move(other.uvalue));
80+
holds_value = true;
81+
}
8082
}
8183
return *this;
8284
}

0 commit comments

Comments
 (0)