Implement assignment operator for common::optional. (#800)

Implement assignment operator for common::optional.
master
Susanne Pielawa 2018-01-09 17:54:53 +01:00 committed by Wally B. Feed
parent 8c7c4e3d2a
commit 67d26747cc
2 changed files with 28 additions and 0 deletions

View File

@ -45,6 +45,20 @@ class optional {
return *value_; return *value_;
} }
optional<T>& operator=(const T& other_value) {
this->value_ = common::make_unique<T>(other_value);
return *this;
}
optional<T>& operator=(const optional<T>& other) {
if (!other.has_value()) {
this->value_ = nullptr;
} else {
this->value_ = common::make_unique<T>(other.value());
}
return *this;
}
private: private:
std::unique_ptr<T> value_; std::unique_ptr<T> value_;
}; };

View File

@ -44,6 +44,20 @@ TEST(OptionalTest, CreateFromOtherOptional) {
EXPECT_EQ(5, b.value()); EXPECT_EQ(5, b.value());
} }
TEST(OptionalTest, AssignmentOperator) {
optional<int> a(5);
optional<int> b(4);
optional<int> c;
a = b;
EXPECT_TRUE(a.has_value());
EXPECT_EQ(4, a.value());
a = c;
EXPECT_FALSE(a.has_value());
a = 3;
EXPECT_TRUE(a.has_value());
EXPECT_EQ(3, a.value());
}
} // namespace } // namespace
} // namespace common } // namespace common
} // namespace cartographer } // namespace cartographer