35 #ifndef ABSL_TYPES_OPTIONAL_H_ 36 #define ABSL_TYPES_OPTIONAL_H_ 41 #ifdef ABSL_HAVE_STD_OPTIONAL 46 using std::bad_optional_access;
53 #else // ABSL_HAVE_STD_OPTIONAL 57 #include <initializer_list> 58 #include <type_traits> 115 template <
typename T>
118 optional_internal::ctor_copy_traits<T>::traits>,
120 optional_internal::assign_copy_traits<T>::traits> {
145 template <
typename InPlaceT,
typename... Args,
147 std::is_same<InPlaceT, in_place_t>,
148 std::is_constructible<T, Args&&...> >
::value>* =
nullptr>
149 constexpr
explicit optional(InPlaceT, Args&&... args)
156 template <
typename U,
typename... Args,
157 typename =
typename std::enable_if<std::is_constructible<
158 T, std::initializer_list<U>&, Args&&...>
::value>::type>
167 typename std::enable_if<
172 std::is_convertible<U&&, T>,
173 std::is_constructible<T, U&&> >
::value,
180 typename std::enable_if<
182 in_place_t,
typename std::decay<U>::type>>,
184 optional<T>,
typename std::decay<U>::type>>,
186 std::is_constructible<T, U&&>>
::value,
192 template <
typename U,
193 typename std::enable_if<
196 std::is_constructible<T, const U&>,
200 std::is_convertible<const U&, T> >
::value,
209 template <
typename U,
210 typename std::enable_if<
212 absl::negation<std::is_same<T, U>>,
213 std::is_constructible<T, const U&>,
226 template <
typename U,
227 typename std::enable_if<
229 absl::negation<std::is_same<T, U> >,
230 std::is_constructible<T, U&&>,
234 std::is_convertible<U&&, T> >
::value,
245 typename std::enable_if<
247 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U&&>,
251 absl::negation<std::is_convertible<U&&, T>>>
::value,
286 std::is_same<optional<T>,
typename std::decay<U>::type>>,
289 std::is_same<T, typename std::decay<U>::type>>>,
290 std::is_constructible<T, U>, std::is_assignable<T&, U>>::value>::type>
292 this->assign(std::forward<U>(
v));
299 absl::negation<std::is_same<T, U>>,
300 std::is_constructible<T, const U&>, std::is_assignable<T&, const U&>,
304 T, U>>>::value>::type>
314 template <
typename U,
316 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U>,
317 std::is_assignable<T&, U>,
321 T, U>>>::value>::type>
351 template <
typename... Args,
352 typename =
typename std::enable_if<
353 std::is_constructible<T, Args&&...>::value>::type>
356 this->
construct(std::forward<Args>(args)...);
371 template <
typename U,
typename... Args,
372 typename =
typename std::enable_if<std::is_constructible<
373 T, std::initializer_list<U>&, Args&&...>::value>::type>
374 T&
emplace(std::initializer_list<U> il, Args&&... args) {
376 this->
construct(il, std::forward<Args>(args)...);
412 assert(this->engaged_);
413 return std::addressof(this->
data_);
416 assert(this->engaged_);
417 return std::addressof(this->
data_);
428 assert(this->engaged_);
435 assert(this->engaged_);
449 constexpr
explicit operator bool() const noexcept {
return this->engaged_; }
455 constexpr
bool has_value() const noexcept {
return this->engaged_; }
460 #pragma warning(push) 461 #pragma warning(disable : 4702) 469 constexpr
const T&
value() const & {
470 return static_cast<bool>(*this)
475 return static_cast<bool>(*this)
481 static_cast<bool>(*
this)
485 constexpr
const T&&
value() const && {
487 static_cast<bool>(*
this)
499 template <
typename U>
502 "optional<T>::value_or: T must by copy constructible");
504 "optional<T>::value_or: U must be convertible to T");
505 return static_cast<bool>(*this)
507 :
static_cast<T
>(absl::forward<U>(
v));
509 template <
typename U>
512 "optional<T>::value_or: T must by move constructible");
514 "optional<T>::value_or: U must be convertible to T");
515 return static_cast<bool>(*this) ?
std::move(**
this)
516 :
static_cast<T
>(std::forward<U>(
v));
527 !std::is_same<
nullopt_t,
typename std::remove_cv<T>::type>::
value,
528 "optional<nullopt_t> is not allowed.");
530 !std::is_same<in_place_t,
typename std::remove_cv<T>::type>::
value,
531 "optional<in_place_t> is not allowed.");
533 "optional<reference> is not allowed.");
542 template <
typename T,
typename std::enable_if<
565 template <
typename T>
570 template <
typename T,
typename... Args>
575 template <
typename T,
typename U,
typename... Args>
579 absl::forward<Args>(args)...);
596 template <
typename T,
typename U>
599 return static_cast<bool>(x) != static_cast<bool>(y)
601 :
static_cast<bool>(x) ==
false ?
true 602 : static_cast<bool>(*x == *y);
607 template <
typename T,
typename U>
610 return static_cast<bool>(x) != static_cast<bool>(y)
612 :
static_cast<bool>(x) ==
false ?
false 613 : static_cast<bool>(*x != *y);
616 template <
typename T,
typename U>
617 constexpr
auto operator<(const optional<T>& x,
const optional<U>& y)
619 return !y ?
false : !x ?
true :
static_cast<bool>(*x < *y);
622 template <
typename T,
typename U>
625 return !x ?
false : !y ?
true :
static_cast<bool>(*x > *y);
628 template <
typename T,
typename U>
629 constexpr
auto operator<=(const optional<T>& x,
const optional<U>& y)
631 return !x ?
true : !y ?
false :
static_cast<bool>(*x <= *y);
634 template <
typename T,
typename U>
637 return !y ?
true : !x ?
false :
static_cast<bool>(*x >= *y);
642 template <
typename T>
646 template <
typename T>
650 template <
typename T>
652 return static_cast<bool>(x);
654 template <
typename T>
656 return static_cast<bool>(x);
658 template <
typename T>
659 constexpr
bool operator<(const optional<T>&,
nullopt_t) noexcept {
662 template <
typename T>
663 constexpr
bool operator<(nullopt_t, const optional<T>& x) noexcept {
664 return static_cast<bool>(x);
666 template <
typename T>
667 constexpr
bool operator<=(const optional<T>& x,
nullopt_t) noexcept {
670 template <
typename T>
671 constexpr
bool operator<=(nullopt_t, const optional<T>&) noexcept {
674 template <
typename T>
676 return static_cast<bool>(x);
678 template <
typename T>
682 template <
typename T>
686 template <
typename T>
696 template <
typename T,
typename U>
699 return static_cast<bool>(x) ? static_cast<bool>(*x ==
v) :
false;
701 template <
typename T,
typename U>
704 return static_cast<bool>(x) ? static_cast<bool>(
v == *x) :
false;
706 template <
typename T,
typename U>
709 return static_cast<bool>(x) ? static_cast<bool>(*x !=
v) :
true;
711 template <
typename T,
typename U>
714 return static_cast<bool>(x) ? static_cast<bool>(
v != *x) :
true;
716 template <
typename T,
typename U>
717 constexpr
auto operator<(const optional<T>& x,
const U&
v)
719 return static_cast<bool>(x) ? static_cast<bool>(*x <
v) :
true;
721 template <
typename T,
typename U>
722 constexpr
auto operator<(const U& v, const optional<T>& x)
724 return static_cast<bool>(x) ? static_cast<bool>(
v < *x) :
false;
726 template <
typename T,
typename U>
727 constexpr
auto operator<=(const optional<T>& x,
const U&
v)
729 return static_cast<bool>(x) ? static_cast<bool>(*x <=
v) :
true;
731 template <
typename T,
typename U>
732 constexpr
auto operator<=(const U& v, const optional<T>& x)
734 return static_cast<bool>(x) ? static_cast<bool>(
v <= *x) :
false;
736 template <
typename T,
typename U>
739 return static_cast<bool>(x) ? static_cast<bool>(*x >
v) :
false;
741 template <
typename T,
typename U>
744 return static_cast<bool>(x) ? static_cast<bool>(
v > *x) :
true;
746 template <
typename T,
typename U>
749 return static_cast<bool>(x) ? static_cast<bool>(*x >=
v) :
false;
751 template <
typename T,
typename U>
754 return static_cast<bool>(x) ? static_cast<bool>(
v >= *x) :
true;
762 template <
typename T>
768 #undef ABSL_MSVC_CONSTEXPR_BUG_IN_UNION_LIKE_CLASS 770 #endif // ABSL_HAVE_STD_OPTIONAL 772 #endif // ABSL_TYPES_OPTIONAL_H_
constexpr optional(nullopt_t) noexcept
constexpr nullopt_t(optional_internal::init_t) noexcept
optional & operator=(const optional< U > &rhs)
constexpr const T & reference() const
T & emplace(std::initializer_list< U > il, Args &&...args)
constexpr optional(InPlaceT, Args &&...args)
constexpr T && forward(absl::remove_reference_t< T > &t) noexcept
optional & operator=(optional< U > &&rhs)
#define ABSL_ATTRIBUTE_REINITIALIZES
#define ABSL_ASSERT(expr)
constexpr const T & operator*() const &
optional(const optional< U > &rhs)
optional & operator=(nullopt_t) noexcept
bool operator>(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
typename std::enable_if< B, T >::type enable_if_t
optional & operator=(U &&v)
void swap(optional &rhs) noexcept(std::is_nothrow_move_constructible< T >::value &&type_traits_internal::IsNothrowSwappable< T >::value)
constexpr optional< T > make_optional(std::initializer_list< U > il, Args &&...args)
T & emplace(Args &&...args)
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
constexpr optional() noexcept
bool convertible_to_bool(bool)
bool operator>=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
void throw_bad_optional_access()
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
hash_default_hash< typename T::first_type > hash
constexpr const T && operator*() const &&
constexpr bool has_value() const noexcept
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
const T * operator->() const
static std::function< void(void *, Slot *, Slot)> construct
constexpr const T & value() const &
constexpr optional(in_place_t, std::initializer_list< U > il, Args &&...args)
constexpr optional< typename std::decay< T >::type > make_optional(T &&v)
constexpr const T && value() const &&
ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos,-1)
ABSL_ATTRIBUTE_REINITIALIZES void reset() noexcept
optional(optional< U > &&rhs)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
constexpr T value_or(U &&v) const &
constexpr optional(U &&v)