Fix type storage copy and move assignment function helper

Previously, in the case both the lhs and rhs optionals were present, the copy/move assignments were erroneously destroying the lhs object.
master
Francisco 3 years ago committed by Francisco Paisana
parent 866fe55c42
commit a2174a5714

@ -70,11 +70,9 @@ void copy_if_present_helper(type_storage<T, MinSize, AlignSize>& lhs,
{ {
if (lhs_present and rhs_present) { if (lhs_present and rhs_present) {
lhs.get() = rhs.get(); lhs.get() = rhs.get();
} } else if (lhs_present) {
if (lhs_present) {
lhs.destroy(); lhs.destroy();
} } else if (rhs_present) {
if (rhs_present) {
lhs.copy_ctor(rhs); lhs.copy_ctor(rhs);
} }
} }
@ -87,11 +85,9 @@ void move_if_present_helper(type_storage<T, MinSize, AlignSize>& lhs,
{ {
if (lhs_present and rhs_present) { if (lhs_present and rhs_present) {
lhs.move_assign(std::move(rhs)); lhs.move_assign(std::move(rhs));
} } else if (lhs_present) {
if (lhs_present) {
lhs.destroy(); lhs.destroy();
} } else if (rhs_present) {
if (rhs_present) {
lhs.move_ctor(std::move(rhs)); lhs.move_ctor(std::move(rhs));
} }
} }

@ -29,7 +29,26 @@ void test_optional_int()
TESTASSERT(opt == opt2); TESTASSERT(opt == opt2);
} }
struct C {
std::unique_ptr<int> val;
C(int val = 0) : val(std::make_unique<int>(val)) {}
};
void test_optional_move_only()
{
optional<C> a, b;
a.emplace(C{});
TESTASSERT(a.has_value());
TESTASSERT_EQ(0, *a.value().val);
TESTASSERT(not b.has_value());
b.emplace(C{5});
a = std::move(b);
TESTASSERT_EQ(5, *a.value().val);
}
int main() int main()
{ {
test_optional_int(); test_optional_int();
test_optional_move_only();
} }
Loading…
Cancel
Save