}
}
- const Value& value() const {
+ const Value& value() const& {
require_value();
return value_;
}
- Value& value() {
+ Value& value() & {
require_value();
return value_;
}
- Value* get_pointer() { return hasValue_ ? &value_ : nullptr; }
- const Value* get_pointer() const { return hasValue_ ? &value_ : nullptr; }
+ Value value() && {
+ require_value();
+ return std::move(value_);
+ }
+
+ const Value* get_pointer() const& { return hasValue_ ? &value_ : nullptr; }
+ Value* get_pointer() & { return hasValue_ ? &value_ : nullptr; }
+ Value* get_pointer() && = delete;
bool hasValue() const { return hasValue_; }
return hasValue();
}
- const Value& operator*() const { return value(); }
- Value& operator*() { return value(); }
+ const Value& operator*() const& { return value(); }
+ Value& operator*() & { return value(); }
+ Value operator*() && { return std::move(value()); }
const Value* operator->() const { return &value(); }
Value* operator->() { return &value(); }
EXPECT_EQ(42, *std::move(opt).value_or(std::move(dflt)));
}
+struct ExpectingDeleter {
+ explicit ExpectingDeleter(int expected) : expected(expected) { }
+ int expected;
+ void operator()(const int* ptr) {
+ EXPECT_EQ(*ptr, expected);
+ delete ptr;
+ }
+};
+
+TEST(Optional, value_life_extention) {
+ // Extends the life of the value.
+ const auto& ptr = Optional<std::unique_ptr<int, ExpectingDeleter>>(
+ {new int(42), ExpectingDeleter{1337}}).value();
+ *ptr = 1337;
+}
+
+TEST(Optional, value_move) {
+ auto ptr = Optional<std::unique_ptr<int, ExpectingDeleter>>(
+ {new int(42), ExpectingDeleter{1337}}).value();
+ *ptr = 1337;
+}
+
+TEST(Optional, dereference_life_extention) {
+ // Extends the life of the value.
+ const auto& ptr = *Optional<std::unique_ptr<int, ExpectingDeleter>>(
+ {new int(42), ExpectingDeleter{1337}});
+ *ptr = 1337;
+}
+
+TEST(Optional, dereference_move) {
+ auto ptr = *Optional<std::unique_ptr<int, ExpectingDeleter>>(
+ {new int(42), ExpectingDeleter{1337}});
+ *ptr = 1337;
+}
+
TEST(Optional, EmptyConstruct) {
Optional<int> opt;
EXPECT_FALSE(bool(opt));