15 #ifndef SHAKA_EMBEDDED_OPTIONAL_H_ 16 #define SHAKA_EMBEDDED_OPTIONAL_H_ 20 #include <type_traits> 61 template <
class U = T,
62 class =
typename std::enable_if<
63 std::is_constructible<T, U&&>::value &&
65 optional(U&& value) : value_(
std::forward<U>(value)), has_value_(true) {}
69 new (&value_) T(other.
value_);
73 new (&value_) T(std::move(other.value_));
80 new (&value_) T(other.
value_);
85 new (&value_) T(std::move(other.value_));
99 has_value_ = other.has_value_;
101 new (&value_) T(other.
value_);
106 has_value_ = other.has_value_;
108 new (&value_) T(std::move(other.value_));
133 return std::move(value_);
137 return std::move(value_);
140 explicit operator bool()
const {
157 return std::move(value_);
161 return std::move(value_);
166 return has_value_ ? value_ :
static_cast<T
>(std::forward<U>(default_value));
170 return has_value_ ? std::move(value_)
171 :
static_cast<T
>(std::forward<U>(default_value));
181 template <
class... Args>
185 new (&value_) T(std::forward<Args>(args)...);
201 "Optional bad alignment");
206 template <
class A,
class B>
213 template <
class A,
class B>
215 return !(lhs == rhs);
217 template <
class A,
class B>
219 if (!lhs.has_value() || !rhs.has_value())
222 return lhs.value() == rhs.value();
224 template <
class A,
class B>
226 return lhs < rhs || lhs == rhs;
228 template <
class A,
class B>
232 template <
class A,
class B>
234 return rhs < lhs || lhs == rhs;
258 bool operator<(nullopt_t, const optional<T>& opt) {
266 bool operator<=(nullopt_t, const optional<T>& opt) {
286 template <
class A,
class B>
288 return bool(opt) ? *opt == value :
false;
290 template <
class A,
class B>
292 return bool(opt) ? value == *opt :
false;
294 template <
class A,
class B>
296 return bool(opt) ? *opt != value :
true;
298 template <
class A,
class B>
300 return bool(opt) ? value == *opt :
true;
302 template <
class A,
class B>
303 bool operator<(const optional<A>& opt,
const B& value) {
304 return bool(opt) ? *opt < value :
true;
306 template <
class A,
class B>
307 bool operator<(const A& value, const optional<B>& opt) {
308 return bool(opt) ? value < *opt :
false;
310 template <
class A,
class B>
311 bool operator<=(const optional<A>& opt,
const B& value) {
312 return bool(opt) ? *opt <= value :
true;
314 template <
class A,
class B>
315 bool operator<=(const A& value, const optional<B>& opt) {
316 return bool(opt) ? value <= *opt :
false;
318 template <
class A,
class B>
320 return bool(opt) ? *opt > value :
false;
322 template <
class A,
class B>
324 return bool(opt) ? value > *opt :
true;
326 template <
class A,
class B>
328 return bool(opt) ? *opt >= value :
false;
330 template <
class A,
class B>
332 return bool(opt) ? value >= *opt :
true;
337 #endif // SHAKA_EMBEDDED_OPTIONAL_H_ bool operator>=(const optional< A > &lhs, const optional< B > &rhs)
optional(const optional &other)
bool operator>(const optional< A > &lhs, const optional< B > &rhs)
const T && operator*() const &&
T value_or(U &&default_value) &&
optional(optional< U > &&other)
const T & operator*() const &
bool operator==(const optional< A > &lhs, const optional< B > &rhs)
optional(optional &&other)
bool operator!=(const optional< A > &lhs, const optional< B > &rhs)
optional & operator=(optional &&other)
const T & value() const &
optional(const optional< U > &other)
optional & operator=(const optional &other)
T value_or(U &&default_value) const &
const T && value() const &&
const T * operator->() const
T & emplace(Args &&... args)