[ADT] Make moving Optional not reset the Optional it moves from.

This brings it in line with std::optional. My recent changes to
make Optional of trivial types trivially copyable introduced
diverging behavior depending on the type, which is bad. Now all
types have the same moving behavior.

llvm-svn: 323445
This commit is contained in:
Benjamin Kramer 2018-01-25 17:24:22 +00:00
parent d328365b2e
commit fcf463e1c1
2 changed files with 6 additions and 8 deletions

View File

@ -46,7 +46,6 @@ template <typename T, bool IsPodLike> struct OptionalStorage {
OptionalStorage(OptionalStorage &&O) : hasVal(O.hasVal) {
if (O.hasVal) {
new (storage.buffer) T(std::move(*O.getPointer()));
O.reset();
}
}
@ -64,7 +63,6 @@ template <typename T, bool IsPodLike> struct OptionalStorage {
reset();
else {
*this = std::move(*O.getPointer());
O.reset();
}
return *this;
}

View File

@ -268,12 +268,12 @@ TEST_F(OptionalTest, MoveOnlyMoveConstruction) {
Optional<MoveOnly> A(MoveOnly(3));
MoveOnly::ResetCounts();
Optional<MoveOnly> B(std::move(A));
EXPECT_FALSE((bool)A);
EXPECT_TRUE((bool)A);
EXPECT_TRUE((bool)B);
EXPECT_EQ(3, B->val);
EXPECT_EQ(1u, MoveOnly::MoveConstructions);
EXPECT_EQ(0u, MoveOnly::MoveAssignments);
EXPECT_EQ(1u, MoveOnly::Destructions);
EXPECT_EQ(0u, MoveOnly::Destructions);
}
TEST_F(OptionalTest, MoveOnlyAssignment) {
@ -292,12 +292,12 @@ TEST_F(OptionalTest, MoveOnlyInitializingAssignment) {
Optional<MoveOnly> B;
MoveOnly::ResetCounts();
B = std::move(A);
EXPECT_FALSE((bool)A);
EXPECT_TRUE((bool)A);
EXPECT_TRUE((bool)B);
EXPECT_EQ(3, B->val);
EXPECT_EQ(1u, MoveOnly::MoveConstructions);
EXPECT_EQ(0u, MoveOnly::MoveAssignments);
EXPECT_EQ(1u, MoveOnly::Destructions);
EXPECT_EQ(0u, MoveOnly::Destructions);
}
TEST_F(OptionalTest, MoveOnlyNullingAssignment) {
@ -317,12 +317,12 @@ TEST_F(OptionalTest, MoveOnlyAssigningAssignment) {
Optional<MoveOnly> B(MoveOnly(4));
MoveOnly::ResetCounts();
B = std::move(A);
EXPECT_FALSE((bool)A);
EXPECT_TRUE((bool)A);
EXPECT_TRUE((bool)B);
EXPECT_EQ(3, B->val);
EXPECT_EQ(0u, MoveOnly::MoveConstructions);
EXPECT_EQ(1u, MoveOnly::MoveAssignments);
EXPECT_EQ(1u, MoveOnly::Destructions);
EXPECT_EQ(0u, MoveOnly::Destructions);
}
struct Immovable {