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
}
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();
+ }
}
}
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() { }