Implement assignment operator for common::optional. (#800)
Implement assignment operator for common::optional.master
parent
8c7c4e3d2a
commit
67d26747cc
|
@ -45,6 +45,20 @@ class optional {
|
|||
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:
|
||||
std::unique_ptr<T> value_;
|
||||
};
|
||||
|
|
|
@ -44,6 +44,20 @@ TEST(OptionalTest, CreateFromOtherOptional) {
|
|||
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 common
|
||||
} // namespace cartographer
|
||||
|
|
Loading…
Reference in New Issue