20 typedef void (
optional::*bool_type)()
const;
21 void this_type_does_not_support_comparisons()
const {}
29 value_ =
new(&storage_) T(other.value());
34 value_ =
new(&storage_) T(value);
45 *
this = other.value();
61 value_ =
new(&storage_) T(value);
71 value_ =
new(&storage_) T(std::move(other.value()));
76 value_ =
new(&storage_) T(std::move(value));
82 *
this = std::move(other.value());
94 *value_ = std::move(value);
98 value_ =
new(&storage_) T(std::move(value));
104 operator bool_type()
const
106 this_type_does_not_support_comparisons();
107 return value_ != 0 ? &optional::this_type_does_not_support_comparisons : 0;
111 if (value_) {
return *value_; }
114 const T & value()
const
116 if (value_) {
return *value_; }
122 T value_or(U&& default_value)
const
124 if (value_) {
return *value_; }
125 return default_value;
129 T value_or(
const U& default_value)
const
131 if (value_) {
return *value_; }
132 return default_value;
135 const T* operator->()
const { assert(value_);
return value_; }
136 T* operator->() { assert(value_);
return value_; }
137 const T& operator*()
const { assert(value_);
return *value_; }
138 T& operator*() { assert(value_);
return *value_; }
144 value_->~T(); value_ = 0;
148 typename std::aligned_storage<
sizeof(T),
149 std::alignment_of<T>::value>::type storage_;
158 typedef void (
optional::*bool_type)()
const;
159 void this_type_does_not_support_comparisons()
const {}
165 optional(T& value) :value_(&value) { }
175 value_ = other.value_;
183 operator bool_type()
const
185 this_type_does_not_support_comparisons();
186 return value_ != 0 ? &optional::this_type_does_not_support_comparisons : 0;
190 if (value_) {
return *value_; }
193 const T & value()
const
195 if (value_) {
return *value_; }
200 T& value_or(T& default_value)
const
202 if (value_) {
return *value_; }
203 return default_value;
206 T& value_or(T& default_value)
const
208 if (value_) {
return *value_; }
209 return default_value;
213 const T* operator->()
const { assert(value_);
return value_; }
214 T* operator->() { assert(value_);
return value_; }
215 const T& operator*()
const { assert(value_);
return *value_; }
216 T& operator*() { assert(value_);
return *value_; }
227 if (
bool(lhs) !=
bool(rhs)) {
return false; }
228 if (
bool(lhs) ==
false) {
return true; }
229 return lhs.value() == rhs.value();
232 bool operator!=(
const optional<T>& lhs,
const optional<T>& rhs)
234 return !(lhs == rhs);
237 bool operator<(
const optional<T>& lhs,
const optional<T>& rhs)
239 if (!
bool(rhs)) {
return false; }
240 if (!
bool(lhs)) {
return true; }
241 return lhs.value() < rhs.value();
244 bool operator<=(
const optional<T>& lhs,
const optional<T>& rhs)
249 bool operator>(
const optional<T>& lhs,
const optional<T>& rhs)
254 bool operator>=(
const optional<T>& lhs,
const optional<T>& rhs)
self implement for std::optional(C++17 feature).
Definition: optional.hpp:19
Definition: optional.hpp:14
Definition: optional.hpp:15