10 #if __cplusplus >= 201703L
17 #ifndef CTRL_UTILS_OPTIONAL_H_
18 #define CTRL_UTILS_OPTIONAL_H_
25 virtual const char* what()
const noexcept
override {
return "bad optional access"; }
37 optional(
optional<T>&& other) : value_(std::move(other.value_)), has_value_(other.has_value_) {}
39 optional(
const T& value) : value_(value), has_value_(
true) {}
40 optional(T&& value) : value_(std::forward<T>(value)), has_value_(
true) {}
42 optional& operator=(
const optional<T>& other) { value_ = other.value_; has_value_ = other.has_value_;
return *
this; }
43 optional& operator=(
optional<T>&& other) { value_ = std::move(other.value_); has_value_ = other.has_value_;
return *
this; }
45 optional& operator=(
const T& value) { value_ = value; has_value_ =
true;
return *
this; }
46 optional& operator=(T&& value) { value_ = std::forward<T>(value); has_value_ =
true;
return *
this; }
48 const T* operator->()
const { assert(has_value_);
return &value_; }
49 T* operator->() { assert(has_value_);
return &value_; }
50 const T& operator*()
const { assert(has_value_);
return value_; }
51 T& operator*() { assert(has_value_);
return value_; }
53 explicit operator bool()
const {
return has_value_; }
54 bool has_value()
const {
return has_value_; }
59 T value_or(T&& default_value)
const {
return has_value_ ? value_ : default_value; }
61 void swap(
optional& other) { std::swap(value_, other.value_); std::swap(has_value_, other.has_value_); }
63 void reset() { has_value_ =
false; }
65 template<
typename... Args>
66 T& emplace(Args&&... args) { value_ = T(std::forward<Args>(args)...); has_value_ =
true;
return value_; }
71 bool has_value_ =
false;
77 return lhs.has_value() == rhs.has_value() && (!lhs.has_value() || lhs.value() == rhs.value());
Definition: optional.h:22
Definition: optional.h:30