Commit cb2dc2c8 authored by Zejun Wu's avatar Zejun Wu Committed by Tudor Bosman

Check for self-assignment in move assignment

Summary:
Check for self-assignment in move assignment. Otherwise
Optional<Neko> cat = whatever;
cat = std::move(cat);
cat.hasValue(); // always returns false

Test Plan: fbmake runtests

Reviewed By: tjackson@fb.com

FB internal diff: D1440633
parent b6a8eb2e
......@@ -136,11 +136,13 @@ class Optional {
}
void assign(Optional&& src) {
if (src.hasValue()) {
assign(std::move(src.value()));
src.clear();
} else {
clear();
if (this != &src) {
if (src.hasValue()) {
assign(std::move(src.value()));
src.clear();
} else {
clear();
}
}
}
......
......@@ -379,6 +379,16 @@ TEST(Optional, MakeOptional) {
EXPECT_EQ(**optIntPtr, 3);
}
TEST(Optional, SelfAssignment) {
Optional<int> a = 42;
a = a;
ASSERT_TRUE(a.hasValue() && a.value() == 42);
Optional<int> b = 23333333;
b = std::move(b);
ASSERT_TRUE(b.hasValue() && b.value() == 23333333);
}
class ContainsOptional {
public:
ContainsOptional() { }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment