xref: /freebsd/contrib/llvm-project/libcxx/include/optional (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_OPTIONAL
110b57cec5SDimitry Andric#define _LIBCPP_OPTIONAL
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    optional synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric// C++1z
170b57cec5SDimitry Andric
180b57cec5SDimitry Andricnamespace std {
1906c3fb27SDimitry Andric  // [optional.optional], class template optional
2006c3fb27SDimitry Andric  template <class T>
2106c3fb27SDimitry Andric    class optional;
220b57cec5SDimitry Andric
2306c3fb27SDimitry Andric  template<class T>
2406c3fb27SDimitry Andric    concept is-derived-from-optional = requires(const T& t) {       // exposition only
2506c3fb27SDimitry Andric      []<class U>(const optional<U>&){ }(t);
2606c3fb27SDimitry Andric    };
2706c3fb27SDimitry Andric
2806c3fb27SDimitry Andric  // [optional.nullopt], no-value state indicator
290b57cec5SDimitry Andric  struct nullopt_t{see below };
300b57cec5SDimitry Andric  inline constexpr nullopt_t nullopt(unspecified );
310b57cec5SDimitry Andric
3206c3fb27SDimitry Andric  // [optional.bad.access], class bad_optional_access
330b57cec5SDimitry Andric  class bad_optional_access;
340b57cec5SDimitry Andric
3506c3fb27SDimitry Andric  // [optional.relops], relational operators
360b57cec5SDimitry Andric  template <class T, class U>
370b57cec5SDimitry Andric    constexpr bool operator==(const optional<T>&, const optional<U>&);
380b57cec5SDimitry Andric  template <class T, class U>
390b57cec5SDimitry Andric    constexpr bool operator!=(const optional<T>&, const optional<U>&);
400b57cec5SDimitry Andric  template <class T, class U>
410b57cec5SDimitry Andric    constexpr bool operator<(const optional<T>&, const optional<U>&);
420b57cec5SDimitry Andric  template <class T, class U>
430b57cec5SDimitry Andric    constexpr bool operator>(const optional<T>&, const optional<U>&);
440b57cec5SDimitry Andric  template <class T, class U>
450b57cec5SDimitry Andric    constexpr bool operator<=(const optional<T>&, const optional<U>&);
460b57cec5SDimitry Andric  template <class T, class U>
470b57cec5SDimitry Andric    constexpr bool operator>=(const optional<T>&, const optional<U>&);
4806c3fb27SDimitry Andric  template<class T, three_way_comparable_with<T> U>
4906c3fb27SDimitry Andric    constexpr compare_three_way_result_t<T, U>
5006c3fb27SDimitry Andric      operator<=>(const optional<T>&, const optional<U>&); // since C++20
510b57cec5SDimitry Andric
5206c3fb27SDimitry Andric  // [optional.nullops], comparison with nullopt
530b57cec5SDimitry Andric  template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;
5406c3fb27SDimitry Andric  template<class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept; // until C++17
5506c3fb27SDimitry Andric  template<class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; // until C++17
5606c3fb27SDimitry Andric  template<class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; // until C++17
5706c3fb27SDimitry Andric  template<class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept;  // until C++17
5806c3fb27SDimitry Andric  template<class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept;  // until C++17
5906c3fb27SDimitry Andric  template<class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; // until C++17
6006c3fb27SDimitry Andric  template<class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; // until C++17
6106c3fb27SDimitry Andric  template<class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept;  // until C++17
6206c3fb27SDimitry Andric  template<class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept;  // until C++17
6306c3fb27SDimitry Andric  template<class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; // until C++17
6406c3fb27SDimitry Andric  template<class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; // until C++17
6506c3fb27SDimitry Andric  template<class T>
6606c3fb27SDimitry Andric    constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept;     // since C++20
670b57cec5SDimitry Andric
6806c3fb27SDimitry Andric  // [optional.comp.with.t], comparison with T
690b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator==(const optional<T>&, const U&);
700b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator==(const T&, const optional<U>&);
710b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&);
720b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&);
730b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator<(const optional<T>&, const U&);
740b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator<(const T&, const optional<U>&);
750b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&);
760b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&);
770b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator>(const optional<T>&, const U&);
780b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator>(const T&, const optional<U>&);
790b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&);
800b57cec5SDimitry Andric  template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&);
8106c3fb27SDimitry Andric  template<class T, class U>
8206c3fb27SDimitry Andric      requires (!is-derived-from-optional<U>) && three_way_comparable_with<T, U>
8306c3fb27SDimitry Andric    constexpr compare_three_way_result_t<T, U>
8406c3fb27SDimitry Andric      operator<=>(const optional<T>&, const U&);                                       // since C++20
850b57cec5SDimitry Andric
8606c3fb27SDimitry Andric  // [optional.specalg], specialized algorithms
8706c3fb27SDimitry Andric  template<class T>
8806c3fb27SDimitry Andric    void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20
8906c3fb27SDimitry Andric
9006c3fb27SDimitry Andric  template<class T>
9106c3fb27SDimitry Andric    constexpr optional<see below > make_optional(T&&);
920b57cec5SDimitry Andric  template<class T, class... Args>
930b57cec5SDimitry Andric    constexpr optional<T> make_optional(Args&&... args);
940b57cec5SDimitry Andric  template<class T, class U, class... Args>
950b57cec5SDimitry Andric    constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
960b57cec5SDimitry Andric
9706c3fb27SDimitry Andric  // [optional.hash], hash support
980b57cec5SDimitry Andric  template<class T> struct hash;
990b57cec5SDimitry Andric  template<class T> struct hash<optional<T>>;
1000b57cec5SDimitry Andric
10106c3fb27SDimitry Andric  template<class T>
10206c3fb27SDimitry Andric  class optional {
1030b57cec5SDimitry Andric  public:
1040b57cec5SDimitry Andric    using value_type = T;
1050b57cec5SDimitry Andric
10606c3fb27SDimitry Andric    // [optional.ctor], constructors
1070b57cec5SDimitry Andric    constexpr optional() noexcept;
1080b57cec5SDimitry Andric    constexpr optional(nullopt_t) noexcept;
109bdd1243dSDimitry Andric    constexpr optional(const optional &);
110bdd1243dSDimitry Andric    constexpr optional(optional &&) noexcept(see below);
11106c3fb27SDimitry Andric    template<class... Args>
11206c3fb27SDimitry Andric      constexpr explicit optional(in_place_t, Args &&...);
1130b57cec5SDimitry Andric    template<class U, class... Args>
1140b57cec5SDimitry Andric      constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...);
1150b57cec5SDimitry Andric    template<class U = T>
11681ad6265SDimitry Andric      constexpr explicit(see-below) optional(U &&);
1170b57cec5SDimitry Andric    template<class U>
11881ad6265SDimitry Andric      explicit(see-below) optional(const optional<U> &);                          // constexpr in C++20
1190b57cec5SDimitry Andric    template<class U>
12081ad6265SDimitry Andric      explicit(see-below) optional(optional<U> &&);                               // constexpr in C++20
1210b57cec5SDimitry Andric
12206c3fb27SDimitry Andric    // [optional.dtor], destructor
123fe6060f1SDimitry Andric    ~optional(); // constexpr in C++20
1240b57cec5SDimitry Andric
12506c3fb27SDimitry Andric    // [optional.assign], assignment
126fe6060f1SDimitry Andric    optional &operator=(nullopt_t) noexcept;                                      // constexpr in C++20
127bdd1243dSDimitry Andric    constexpr optional &operator=(const optional &);
128bdd1243dSDimitry Andric    constexpr optional &operator=(optional &&) noexcept(see below);
129fe6060f1SDimitry Andric    template<class U = T> optional &operator=(U &&);                              // constexpr in C++20
130fe6060f1SDimitry Andric    template<class U> optional &operator=(const optional<U> &);                   // constexpr in C++20
131fe6060f1SDimitry Andric    template<class U> optional &operator=(optional<U> &&);                        // constexpr in C++20
132fe6060f1SDimitry Andric    template<class... Args> T& emplace(Args &&...);                               // constexpr in C++20
13306c3fb27SDimitry Andric    template<class U, class... Args> T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20
1340b57cec5SDimitry Andric
13506c3fb27SDimitry Andric    // [optional.swap], swap
136fe6060f1SDimitry Andric    void swap(optional &) noexcept(see below ); // constexpr in C++20
1370b57cec5SDimitry Andric
13806c3fb27SDimitry Andric    // [optional.observe], observers
139*0fca6ea1SDimitry Andric    constexpr T const *operator->() const noexcept;
140*0fca6ea1SDimitry Andric    constexpr T *operator->() noexcept;
141*0fca6ea1SDimitry Andric    constexpr T const &operator*() const & noexcept;
142*0fca6ea1SDimitry Andric    constexpr T &operator*() & noexcept;
143*0fca6ea1SDimitry Andric    constexpr T &&operator*() && noexcept;
144*0fca6ea1SDimitry Andric    constexpr const T &&operator*() const && noexcept;
1450b57cec5SDimitry Andric    constexpr explicit operator bool() const noexcept;
1460b57cec5SDimitry Andric    constexpr bool has_value() const noexcept;
1470b57cec5SDimitry Andric    constexpr T const &value() const &;
1480b57cec5SDimitry Andric    constexpr T &value() &;
1490b57cec5SDimitry Andric    constexpr T &&value() &&;
1500b57cec5SDimitry Andric    constexpr const T &&value() const &&;
1510b57cec5SDimitry Andric    template<class U> constexpr T value_or(U &&) const &;
1520b57cec5SDimitry Andric    template<class U> constexpr T value_or(U &&) &&;
1530b57cec5SDimitry Andric
1540eae32dcSDimitry Andric    // [optional.monadic], monadic operations
1550eae32dcSDimitry Andric    template<class F> constexpr auto and_then(F&& f) &;         // since C++23
1560eae32dcSDimitry Andric    template<class F> constexpr auto and_then(F&& f) &&;        // since C++23
1570eae32dcSDimitry Andric    template<class F> constexpr auto and_then(F&& f) const&;    // since C++23
1580eae32dcSDimitry Andric    template<class F> constexpr auto and_then(F&& f) const&&;   // since C++23
1590eae32dcSDimitry Andric    template<class F> constexpr auto transform(F&& f) &;        // since C++23
1600eae32dcSDimitry Andric    template<class F> constexpr auto transform(F&& f) &&;       // since C++23
1610eae32dcSDimitry Andric    template<class F> constexpr auto transform(F&& f) const&;   // since C++23
1620eae32dcSDimitry Andric    template<class F> constexpr auto transform(F&& f) const&&;  // since C++23
1630eae32dcSDimitry Andric    template<class F> constexpr optional or_else(F&& f) &&;     // since C++23
1640eae32dcSDimitry Andric    template<class F> constexpr optional or_else(F&& f) const&; // since C++23
1650eae32dcSDimitry Andric
16606c3fb27SDimitry Andric    // [optional.mod], modifiers
167fe6060f1SDimitry Andric    void reset() noexcept;                                      // constexpr in C++20
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric  private:
1700b57cec5SDimitry Andric    T *val;         // exposition only
1710b57cec5SDimitry Andric  };
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric  template<class T>
1740b57cec5SDimitry Andric    optional(T) -> optional<T>;
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric} // namespace std
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric*/
1790b57cec5SDimitry Andric
180*0fca6ea1SDimitry Andric#include <__assert>
18106c3fb27SDimitry Andric#include <__compare/compare_three_way_result.h>
18206c3fb27SDimitry Andric#include <__compare/three_way_comparable.h>
1830eae32dcSDimitry Andric#include <__concepts/invocable.h>
184fe6060f1SDimitry Andric#include <__config>
1855f757f3fSDimitry Andric#include <__exception/exception.h>
18681ad6265SDimitry Andric#include <__functional/hash.h>
18781ad6265SDimitry Andric#include <__functional/invoke.h>
18881ad6265SDimitry Andric#include <__functional/unary_function.h>
189*0fca6ea1SDimitry Andric#include <__fwd/functional.h>
19006c3fb27SDimitry Andric#include <__memory/addressof.h>
19181ad6265SDimitry Andric#include <__memory/construct_at.h>
19206c3fb27SDimitry Andric#include <__tuple/sfinae_helpers.h>
19306c3fb27SDimitry Andric#include <__type_traits/add_pointer.h>
19406c3fb27SDimitry Andric#include <__type_traits/conditional.h>
19506c3fb27SDimitry Andric#include <__type_traits/conjunction.h>
19606c3fb27SDimitry Andric#include <__type_traits/decay.h>
19706c3fb27SDimitry Andric#include <__type_traits/disjunction.h>
19806c3fb27SDimitry Andric#include <__type_traits/is_array.h>
19906c3fb27SDimitry Andric#include <__type_traits/is_assignable.h>
20006c3fb27SDimitry Andric#include <__type_traits/is_constructible.h>
20106c3fb27SDimitry Andric#include <__type_traits/is_convertible.h>
20206c3fb27SDimitry Andric#include <__type_traits/is_destructible.h>
203*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_assignable.h>
204*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_constructible.h>
20506c3fb27SDimitry Andric#include <__type_traits/is_object.h>
20606c3fb27SDimitry Andric#include <__type_traits/is_reference.h>
20706c3fb27SDimitry Andric#include <__type_traits/is_scalar.h>
20806c3fb27SDimitry Andric#include <__type_traits/is_swappable.h>
209*0fca6ea1SDimitry Andric#include <__type_traits/is_trivially_assignable.h>
210*0fca6ea1SDimitry Andric#include <__type_traits/is_trivially_constructible.h>
21106c3fb27SDimitry Andric#include <__type_traits/is_trivially_destructible.h>
212*0fca6ea1SDimitry Andric#include <__type_traits/is_trivially_relocatable.h>
21306c3fb27SDimitry Andric#include <__type_traits/negation.h>
21406c3fb27SDimitry Andric#include <__type_traits/remove_const.h>
21506c3fb27SDimitry Andric#include <__type_traits/remove_cvref.h>
21606c3fb27SDimitry Andric#include <__type_traits/remove_reference.h>
21706c3fb27SDimitry Andric#include <__utility/declval.h>
21881ad6265SDimitry Andric#include <__utility/forward.h>
21981ad6265SDimitry Andric#include <__utility/in_place.h>
22081ad6265SDimitry Andric#include <__utility/move.h>
22181ad6265SDimitry Andric#include <__utility/swap.h>
22206c3fb27SDimitry Andric#include <__verbose_abort>
2230b57cec5SDimitry Andric#include <initializer_list>
2240b57cec5SDimitry Andric#include <new>
2250b57cec5SDimitry Andric#include <version>
2260b57cec5SDimitry Andric
22781ad6265SDimitry Andric// standard-mandated includes
228bdd1243dSDimitry Andric
229bdd1243dSDimitry Andric// [optional.syn]
23081ad6265SDimitry Andric#include <compare>
23181ad6265SDimitry Andric
2320b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2330b57cec5SDimitry Andric#  pragma GCC system_header
2340b57cec5SDimitry Andric#endif
2350b57cec5SDimitry Andric
23606c3fb27SDimitry Andric_LIBCPP_PUSH_MACROS
23706c3fb27SDimitry Andric#include <__undef_macros>
23806c3fb27SDimitry Andric
2390b57cec5SDimitry Andricnamespace std // purposefully not using versioning namespace
2400b57cec5SDimitry Andric{
2410b57cec5SDimitry Andric
242cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access : public exception {
2430b57cec5SDimitry Andricpublic:
2445f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bad_optional_access() _NOEXCEPT                                      = default;
2455f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bad_optional_access(const bad_optional_access&) _NOEXCEPT            = default;
2465f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default;
2470b57cec5SDimitry Andric  // Get the key function ~bad_optional_access() into the dylib
248bdd1243dSDimitry Andric  ~bad_optional_access() _NOEXCEPT override;
249bdd1243dSDimitry Andric  const char* what() const _NOEXCEPT override;
2500b57cec5SDimitry Andric};
2510b57cec5SDimitry Andric
2520eae32dcSDimitry Andric} // namespace std
2530b57cec5SDimitry Andric
25406c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2570b57cec5SDimitry Andric
258cb14a3feSDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS void
259cb14a3feSDimitry Andric__throw_bad_optional_access() {
26006c3fb27SDimitry Andric#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2610b57cec5SDimitry Andric  throw bad_optional_access();
2620b57cec5SDimitry Andric#  else
26306c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode");
2640b57cec5SDimitry Andric#  endif
2650b57cec5SDimitry Andric}
2660b57cec5SDimitry Andric
267cb14a3feSDimitry Andricstruct nullopt_t {
268cb14a3feSDimitry Andric  struct __secret_tag {
269cb14a3feSDimitry Andric    explicit __secret_tag() = default;
270cb14a3feSDimitry Andric  };
2715f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
2720b57cec5SDimitry Andric};
2730b57cec5SDimitry Andric
274349cc55cSDimitry Andricinline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
2750b57cec5SDimitry Andric
2760eae32dcSDimitry Andricstruct __optional_construct_from_invoke_tag {};
2770eae32dcSDimitry Andric
2780b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_destructible<_Tp>::value>
2790b57cec5SDimitry Andricstruct __optional_destruct_base;
2800b57cec5SDimitry Andric
2810b57cec5SDimitry Andrictemplate <class _Tp>
282cb14a3feSDimitry Andricstruct __optional_destruct_base<_Tp, false> {
2830b57cec5SDimitry Andric  typedef _Tp value_type;
284cb14a3feSDimitry Andric  static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
285cb14a3feSDimitry Andric  union {
2860b57cec5SDimitry Andric    char __null_state_;
2870b57cec5SDimitry Andric    value_type __val_;
2880b57cec5SDimitry Andric  };
2890b57cec5SDimitry Andric  bool __engaged_;
2900b57cec5SDimitry Andric
291cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__optional_destruct_base() {
2920b57cec5SDimitry Andric    if (__engaged_)
2930b57cec5SDimitry Andric      __val_.~value_type();
2940b57cec5SDimitry Andric  }
2950b57cec5SDimitry Andric
296cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andric  template <class... _Args>
299cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
300cb14a3feSDimitry Andric      : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
3010b57cec5SDimitry Andric
30206c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 23
3030eae32dcSDimitry Andric  template <class _Fp, class... _Args>
304cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base(
305cb14a3feSDimitry Andric      __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
3065f757f3fSDimitry Andric      : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
3070eae32dcSDimitry Andric#  endif
3080eae32dcSDimitry Andric
309cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
310cb14a3feSDimitry Andric    if (__engaged_) {
3110b57cec5SDimitry Andric      __val_.~value_type();
3120b57cec5SDimitry Andric      __engaged_ = false;
3130b57cec5SDimitry Andric    }
3140b57cec5SDimitry Andric  }
3150b57cec5SDimitry Andric};
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andrictemplate <class _Tp>
318cb14a3feSDimitry Andricstruct __optional_destruct_base<_Tp, true> {
3190b57cec5SDimitry Andric  typedef _Tp value_type;
320cb14a3feSDimitry Andric  static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
321cb14a3feSDimitry Andric  union {
3220b57cec5SDimitry Andric    char __null_state_;
3230b57cec5SDimitry Andric    value_type __val_;
3240b57cec5SDimitry Andric  };
3250b57cec5SDimitry Andric  bool __engaged_;
3260b57cec5SDimitry Andric
327cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
3280b57cec5SDimitry Andric
3290b57cec5SDimitry Andric  template <class... _Args>
330cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
331cb14a3feSDimitry Andric      : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
3320b57cec5SDimitry Andric
33306c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 23
3340eae32dcSDimitry Andric  template <class _Fp, class... _Args>
335cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base(
336cb14a3feSDimitry Andric      __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
3375f757f3fSDimitry Andric      : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
3380eae32dcSDimitry Andric#  endif
3390eae32dcSDimitry Andric
340cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
341cb14a3feSDimitry Andric    if (__engaged_) {
3420b57cec5SDimitry Andric      __engaged_ = false;
3430b57cec5SDimitry Andric    }
3440b57cec5SDimitry Andric  }
3450b57cec5SDimitry Andric};
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andrictemplate <class _Tp, bool = is_reference<_Tp>::value>
348cb14a3feSDimitry Andricstruct __optional_storage_base : __optional_destruct_base<_Tp> {
3490b57cec5SDimitry Andric  using __base     = __optional_destruct_base<_Tp>;
3500b57cec5SDimitry Andric  using value_type = _Tp;
3510b57cec5SDimitry Andric  using __base::__base;
3520b57cec5SDimitry Andric
353cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; }
3540b57cec5SDimitry Andric
355cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; }
356cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; }
357cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() && noexcept { return std::move(this->__val_); }
358cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& __get() const&& noexcept { return std::move(this->__val_); }
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andric  template <class... _Args>
361cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_Args&&... __args) {
36206c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
3635f757f3fSDimitry Andric    std::__construct_at(std::addressof(this->__val_), std::forward<_Args>(__args)...);
3640b57cec5SDimitry Andric    this->__engaged_ = true;
3650b57cec5SDimitry Andric  }
3660b57cec5SDimitry Andric
3670b57cec5SDimitry Andric  template <class _That>
368cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
3690b57cec5SDimitry Andric    if (__opt.has_value())
3705f757f3fSDimitry Andric      __construct(std::forward<_That>(__opt).__get());
3710b57cec5SDimitry Andric  }
3720b57cec5SDimitry Andric
3730b57cec5SDimitry Andric  template <class _That>
374cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
375cb14a3feSDimitry Andric    if (this->__engaged_ == __opt.has_value()) {
3760b57cec5SDimitry Andric      if (this->__engaged_)
3775f757f3fSDimitry Andric        this->__val_ = std::forward<_That>(__opt).__get();
378cb14a3feSDimitry Andric    } else {
3790b57cec5SDimitry Andric      if (this->__engaged_)
3800b57cec5SDimitry Andric        this->reset();
3810b57cec5SDimitry Andric      else
3825f757f3fSDimitry Andric        __construct(std::forward<_That>(__opt).__get());
3830b57cec5SDimitry Andric    }
3840b57cec5SDimitry Andric  }
3850b57cec5SDimitry Andric};
3860b57cec5SDimitry Andric
38781ad6265SDimitry Andric// optional<T&> is currently required to be ill-formed. However, it may
38881ad6265SDimitry Andric// be allowed in the future. For this reason, it has already been implemented
38981ad6265SDimitry Andric// to ensure we can make the change in an ABI-compatible manner.
3900b57cec5SDimitry Andrictemplate <class _Tp>
391cb14a3feSDimitry Andricstruct __optional_storage_base<_Tp, true> {
3920b57cec5SDimitry Andric  using value_type = _Tp;
3930b57cec5SDimitry Andric  using __raw_type = remove_reference_t<_Tp>;
3940b57cec5SDimitry Andric  __raw_type* __value_;
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric  template <class _Up>
39706c3fb27SDimitry Andric  static _LIBCPP_HIDE_FROM_ABI constexpr bool __can_bind_reference() {
398bdd1243dSDimitry Andric    using _RawUp = __libcpp_remove_reference_t<_Up>;
3990b57cec5SDimitry Andric    using _UpPtr = _RawUp*;
400bdd1243dSDimitry Andric    using _RawTp = __libcpp_remove_reference_t<_Tp>;
4010b57cec5SDimitry Andric    using _TpPtr = _RawTp*;
402cb14a3feSDimitry Andric    using _CheckLValueArg =
403cb14a3feSDimitry Andric        integral_constant<bool,
404cb14a3feSDimitry Andric                          (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value) ||
405cb14a3feSDimitry Andric                              is_same<_RawUp, reference_wrapper<_RawTp>>::value ||
406cb14a3feSDimitry Andric                              is_same<_RawUp, reference_wrapper<__remove_const_t<_RawTp>>>::value >;
407cb14a3feSDimitry Andric    return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value) ||
408cb14a3feSDimitry Andric           (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value &&
4090b57cec5SDimitry Andric            is_convertible<_UpPtr, _TpPtr>::value);
4100b57cec5SDimitry Andric  }
4110b57cec5SDimitry Andric
412cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr __optional_storage_base() noexcept : __value_(nullptr) {}
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andric  template <class _UArg>
415cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg)
416cb14a3feSDimitry Andric      : __value_(std::addressof(__uarg)) {
4170b57cec5SDimitry Andric    static_assert(__can_bind_reference<_UArg>(),
4180b57cec5SDimitry Andric                  "Attempted to construct a reference element in tuple from a "
4190b57cec5SDimitry Andric                  "possible temporary");
4200b57cec5SDimitry Andric  }
4210b57cec5SDimitry Andric
422cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; }
4230b57cec5SDimitry Andric
424cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; }
4250b57cec5SDimitry Andric
426cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const& noexcept { return *__value_; }
4270b57cec5SDimitry Andric
428cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() const&& noexcept { return std::forward<value_type>(*__value_); }
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andric  template <class _UArg>
431cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_UArg&& __val) {
43206c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
4330b57cec5SDimitry Andric    static_assert(__can_bind_reference<_UArg>(),
4340b57cec5SDimitry Andric                  "Attempted to construct a reference element in tuple from a "
4350b57cec5SDimitry Andric                  "possible temporary");
4365f757f3fSDimitry Andric    __value_ = std::addressof(__val);
4370b57cec5SDimitry Andric  }
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andric  template <class _That>
440cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
4410b57cec5SDimitry Andric    if (__opt.has_value())
4425f757f3fSDimitry Andric      __construct(std::forward<_That>(__opt).__get());
4430b57cec5SDimitry Andric  }
4440b57cec5SDimitry Andric
4450b57cec5SDimitry Andric  template <class _That>
446cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
447cb14a3feSDimitry Andric    if (has_value() == __opt.has_value()) {
4480b57cec5SDimitry Andric      if (has_value())
4495f757f3fSDimitry Andric        *__value_ = std::forward<_That>(__opt).__get();
450cb14a3feSDimitry Andric    } else {
4510b57cec5SDimitry Andric      if (has_value())
4520b57cec5SDimitry Andric        reset();
4530b57cec5SDimitry Andric      else
4545f757f3fSDimitry Andric        __construct(std::forward<_That>(__opt).__get());
4550b57cec5SDimitry Andric    }
4560b57cec5SDimitry Andric  }
4570b57cec5SDimitry Andric};
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value>
460cb14a3feSDimitry Andricstruct __optional_copy_base : __optional_storage_base<_Tp> {
4610b57cec5SDimitry Andric  using __optional_storage_base<_Tp>::__optional_storage_base;
4620b57cec5SDimitry Andric};
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andrictemplate <class _Tp>
465cb14a3feSDimitry Andricstruct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> {
4660b57cec5SDimitry Andric  using __optional_storage_base<_Tp>::__optional_storage_base;
4670b57cec5SDimitry Andric
468cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default;
4690b57cec5SDimitry Andric
470cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) {
4710b57cec5SDimitry Andric    this->__construct_from(__opt);
4720b57cec5SDimitry Andric  }
4730b57cec5SDimitry Andric
474cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&)                 = default;
475cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default;
476cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&)      = default;
4770b57cec5SDimitry Andric};
4780b57cec5SDimitry Andric
4790b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_move_constructible<_Tp>::value>
480cb14a3feSDimitry Andricstruct __optional_move_base : __optional_copy_base<_Tp> {
4810b57cec5SDimitry Andric  using __optional_copy_base<_Tp>::__optional_copy_base;
4820b57cec5SDimitry Andric};
4830b57cec5SDimitry Andric
4840b57cec5SDimitry Andrictemplate <class _Tp>
485cb14a3feSDimitry Andricstruct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> {
4860b57cec5SDimitry Andric  using value_type = _Tp;
4870b57cec5SDimitry Andric  using __optional_copy_base<_Tp>::__optional_copy_base;
4880b57cec5SDimitry Andric
489cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_move_base()                            = default;
490cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default;
4910b57cec5SDimitry Andric
492cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
493cb14a3feSDimitry Andric  __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) {
4945f757f3fSDimitry Andric    this->__construct_from(std::move(__opt));
4950b57cec5SDimitry Andric  }
4960b57cec5SDimitry Andric
497cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default;
498cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&)      = default;
4990b57cec5SDimitry Andric};
5000b57cec5SDimitry Andric
501cb14a3feSDimitry Andrictemplate <class _Tp,
502cb14a3feSDimitry Andric          bool = is_trivially_destructible<_Tp>::value && is_trivially_copy_constructible<_Tp>::value &&
5030b57cec5SDimitry Andric                 is_trivially_copy_assignable<_Tp>::value>
504cb14a3feSDimitry Andricstruct __optional_copy_assign_base : __optional_move_base<_Tp> {
5050b57cec5SDimitry Andric  using __optional_move_base<_Tp>::__optional_move_base;
5060b57cec5SDimitry Andric};
5070b57cec5SDimitry Andric
5080b57cec5SDimitry Andrictemplate <class _Tp>
509cb14a3feSDimitry Andricstruct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> {
5100b57cec5SDimitry Andric  using __optional_move_base<_Tp>::__optional_move_base;
5110b57cec5SDimitry Andric
512cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base()                                   = default;
513cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
514cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&)      = default;
5150b57cec5SDimitry Andric
516cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base&
517cb14a3feSDimitry Andric  operator=(const __optional_copy_assign_base& __opt) {
5180b57cec5SDimitry Andric    this->__assign_from(__opt);
5190b57cec5SDimitry Andric    return *this;
5200b57cec5SDimitry Andric  }
5210b57cec5SDimitry Andric
522cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
5230b57cec5SDimitry Andric};
5240b57cec5SDimitry Andric
525cb14a3feSDimitry Andrictemplate <class _Tp,
526cb14a3feSDimitry Andric          bool = is_trivially_destructible<_Tp>::value && is_trivially_move_constructible<_Tp>::value &&
5270b57cec5SDimitry Andric                 is_trivially_move_assignable<_Tp>::value>
528cb14a3feSDimitry Andricstruct __optional_move_assign_base : __optional_copy_assign_base<_Tp> {
5290b57cec5SDimitry Andric  using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
5300b57cec5SDimitry Andric};
5310b57cec5SDimitry Andric
5320b57cec5SDimitry Andrictemplate <class _Tp>
533cb14a3feSDimitry Andricstruct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> {
5340b57cec5SDimitry Andric  using value_type = _Tp;
5350b57cec5SDimitry Andric  using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
5360b57cec5SDimitry Andric
537cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base()                                              = default;
538cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt)      = default;
539cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&)                 = default;
540cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
5410b57cec5SDimitry Andric
542cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base&
543cb14a3feSDimitry Andric  operator=(__optional_move_assign_base&& __opt) noexcept(
544cb14a3feSDimitry Andric      is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) {
5455f757f3fSDimitry Andric    this->__assign_from(std::move(__opt));
5460b57cec5SDimitry Andric    return *this;
5470b57cec5SDimitry Andric  }
5480b57cec5SDimitry Andric};
5490b57cec5SDimitry Andric
5500b57cec5SDimitry Andrictemplate <class _Tp>
551cb14a3feSDimitry Andricusing __optional_sfinae_ctor_base_t =
552cb14a3feSDimitry Andric    __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >;
5530b57cec5SDimitry Andric
5540b57cec5SDimitry Andrictemplate <class _Tp>
555cb14a3feSDimitry Andricusing __optional_sfinae_assign_base_t =
556cb14a3feSDimitry Andric    __sfinae_assign_base< (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),
557cb14a3feSDimitry Andric                          (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value) >;
5580b57cec5SDimitry Andric
5590b57cec5SDimitry Andrictemplate <class _Tp>
5600eae32dcSDimitry Andricclass optional;
56106c3fb27SDimitry Andric
56206c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
56306c3fb27SDimitry Andric
56406c3fb27SDimitry Andrictemplate <class _Tp>
56506c3fb27SDimitry Andricconcept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(const optional<_Up>&) {}(__t); };
56606c3fb27SDimitry Andric
56706c3fb27SDimitry Andric#  endif // _LIBCPP_STD_VER >= 20
56806c3fb27SDimitry Andric
5690eae32dcSDimitry Andrictemplate <class _Tp>
5700eae32dcSDimitry Andricstruct __is_std_optional : false_type {};
571cb14a3feSDimitry Andrictemplate <class _Tp>
572cb14a3feSDimitry Andricstruct __is_std_optional<optional<_Tp>> : true_type {};
5730eae32dcSDimitry Andric
5740eae32dcSDimitry Andrictemplate <class _Tp>
57506c3fb27SDimitry Andricclass _LIBCPP_DECLSPEC_EMPTY_BASES optional
576cb14a3feSDimitry Andric    : private __optional_move_assign_base<_Tp>,
577cb14a3feSDimitry Andric      private __optional_sfinae_ctor_base_t<_Tp>,
578cb14a3feSDimitry Andric      private __optional_sfinae_assign_base_t<_Tp> {
5790b57cec5SDimitry Andric  using __base = __optional_move_assign_base<_Tp>;
580cb14a3feSDimitry Andric
5810b57cec5SDimitry Andricpublic:
5820b57cec5SDimitry Andric  using value_type = _Tp;
5830b57cec5SDimitry Andric
584*0fca6ea1SDimitry Andric  using __trivially_relocatable = conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, optional, void>;
585*0fca6ea1SDimitry Andric
5860b57cec5SDimitry Andricprivate:
5870b57cec5SDimitry Andric  // Disable the reference extension using this static assert.
588bdd1243dSDimitry Andric  static_assert(!is_same_v<__remove_cvref_t<value_type>, in_place_t>,
5890b57cec5SDimitry Andric                "instantiation of optional with in_place_t is ill-formed");
590bdd1243dSDimitry Andric  static_assert(!is_same_v<__remove_cvref_t<value_type>, nullopt_t>,
5910b57cec5SDimitry Andric                "instantiation of optional with nullopt_t is ill-formed");
592cb14a3feSDimitry Andric  static_assert(!is_reference_v<value_type>, "instantiation of optional with a reference type is ill-formed");
593cb14a3feSDimitry Andric  static_assert(is_destructible_v<value_type>, "instantiation of optional with a non-destructible type is ill-formed");
594cb14a3feSDimitry Andric  static_assert(!is_array_v<value_type>, "instantiation of optional with an array type is ill-formed");
5950b57cec5SDimitry Andric
5960b57cec5SDimitry Andric  // LWG2756: conditionally explicit conversion from _Up
5970b57cec5SDimitry Andric  struct _CheckOptionalArgsConstructor {
5980b57cec5SDimitry Andric    template <class _Up>
59906c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
600cb14a3feSDimitry Andric      return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>;
6010b57cec5SDimitry Andric    }
6020b57cec5SDimitry Andric
6030b57cec5SDimitry Andric    template <class _Up>
60406c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
605cb14a3feSDimitry Andric      return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>;
6060b57cec5SDimitry Andric    }
6070b57cec5SDimitry Andric  };
6080b57cec5SDimitry Andric  template <class _Up>
609cb14a3feSDimitry Andric  using _CheckOptionalArgsCtor =
610cb14a3feSDimitry Andric      _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value &&
6115f757f3fSDimitry Andric               (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value),
6120b57cec5SDimitry Andric           _CheckOptionalArgsConstructor,
613cb14a3feSDimitry Andric           __check_tuple_constructor_fail >;
6140b57cec5SDimitry Andric  template <class _QualUp>
6150b57cec5SDimitry Andric  struct _CheckOptionalLikeConstructor {
6160b57cec5SDimitry Andric    template <class _Up, class _Opt = optional<_Up>>
617cb14a3feSDimitry Andric    using __check_constructible_from_opt =
618cb14a3feSDimitry Andric        _Or< is_constructible<_Tp, _Opt&>,
6190b57cec5SDimitry Andric             is_constructible<_Tp, _Opt const&>,
6200b57cec5SDimitry Andric             is_constructible<_Tp, _Opt&&>,
6210b57cec5SDimitry Andric             is_constructible<_Tp, _Opt const&&>,
6220b57cec5SDimitry Andric             is_convertible<_Opt&, _Tp>,
6230b57cec5SDimitry Andric             is_convertible<_Opt const&, _Tp>,
6240b57cec5SDimitry Andric             is_convertible<_Opt&&, _Tp>,
625cb14a3feSDimitry Andric             is_convertible<_Opt const&&, _Tp> >;
6260b57cec5SDimitry Andric    template <class _Up, class _Opt = optional<_Up>>
627cb14a3feSDimitry Andric    using __check_assignable_from_opt =
628cb14a3feSDimitry Andric        _Or< is_assignable<_Tp&, _Opt&>,
6290b57cec5SDimitry Andric             is_assignable<_Tp&, _Opt const&>,
6300b57cec5SDimitry Andric             is_assignable<_Tp&, _Opt&&>,
631cb14a3feSDimitry Andric             is_assignable<_Tp&, _Opt const&&> >;
6320b57cec5SDimitry Andric    template <class _Up, class _QUp = _QualUp>
63306c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
6340b57cec5SDimitry Andric      return is_convertible<_QUp, _Tp>::value &&
6355f757f3fSDimitry Andric             (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
6360b57cec5SDimitry Andric    }
6370b57cec5SDimitry Andric    template <class _Up, class _QUp = _QualUp>
63806c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
6390b57cec5SDimitry Andric      return !is_convertible<_QUp, _Tp>::value &&
6405f757f3fSDimitry Andric             (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
6410b57cec5SDimitry Andric    }
6420b57cec5SDimitry Andric    template <class _Up, class _QUp = _QualUp>
64306c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() {
644e8d8bef9SDimitry Andric      // Construction and assignability of _QUp to _Tp has already been
6450b57cec5SDimitry Andric      // checked.
646cb14a3feSDimitry Andric      return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value;
6470b57cec5SDimitry Andric    }
6480b57cec5SDimitry Andric  };
6490b57cec5SDimitry Andric
6500b57cec5SDimitry Andric  template <class _Up, class _QualUp>
651cb14a3feSDimitry Andric  using _CheckOptionalLikeCtor =
652cb14a3feSDimitry Andric      _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value,
6530b57cec5SDimitry Andric           _CheckOptionalLikeConstructor<_QualUp>,
654cb14a3feSDimitry Andric           __check_tuple_constructor_fail >;
6550b57cec5SDimitry Andric  template <class _Up, class _QualUp>
656cb14a3feSDimitry Andric  using _CheckOptionalLikeAssign =
657cb14a3feSDimitry Andric      _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value,
6580b57cec5SDimitry Andric           _CheckOptionalLikeConstructor<_QualUp>,
659cb14a3feSDimitry Andric           __check_tuple_constructor_fail >;
6600eae32dcSDimitry Andric
6610b57cec5SDimitry Andricpublic:
6625f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {}
6635f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default;
6645f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&)      = default;
6655f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {}
6660b57cec5SDimitry Andric
667cb14a3feSDimitry Andric  template <
668cb14a3feSDimitry Andric      class _InPlaceT,
669cb14a3feSDimitry Andric      class... _Args,
670cb14a3feSDimitry Andric      class = enable_if_t< _And< _IsSame<_InPlaceT, in_place_t>, is_constructible<value_type, _Args...> >::value > >
671cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args)
6725f757f3fSDimitry Andric      : __base(in_place, std::forward<_Args>(__args)...) {}
6730b57cec5SDimitry Andric
674cb14a3feSDimitry Andric  template <class _Up,
675cb14a3feSDimitry Andric            class... _Args,
676cb14a3feSDimitry Andric            class = enable_if_t< is_constructible_v<value_type, initializer_list<_Up>&, _Args...>> >
677cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
6785f757f3fSDimitry Andric      : __base(in_place, __il, std::forward<_Args>(__args)...) {}
6790b57cec5SDimitry Andric
680cb14a3feSDimitry Andric  template <class _Up                                                                         = value_type,
681cb14a3feSDimitry Andric            enable_if_t< _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
682cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}
6830b57cec5SDimitry Andric
684cb14a3feSDimitry Andric  template <class _Up, enable_if_t< _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>
685cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}
6860b57cec5SDimitry Andric
6870b57cec5SDimitry Andric  // LWG2756: conditionally explicit conversion from const optional<_Up>&
688cb14a3feSDimitry Andric  template <class _Up,
689cb14a3feSDimitry Andric            enable_if_t< _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
690cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v) {
6910b57cec5SDimitry Andric    this->__construct_from(__v);
6920b57cec5SDimitry Andric  }
693cb14a3feSDimitry Andric  template <class _Up,
694cb14a3feSDimitry Andric            enable_if_t< _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
695cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v) {
6960b57cec5SDimitry Andric    this->__construct_from(__v);
6970b57cec5SDimitry Andric  }
6980b57cec5SDimitry Andric
6990b57cec5SDimitry Andric  // LWG2756: conditionally explicit conversion from optional<_Up>&&
700cb14a3feSDimitry Andric  template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>
701cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v) {
7025f757f3fSDimitry Andric    this->__construct_from(std::move(__v));
7030b57cec5SDimitry Andric  }
704cb14a3feSDimitry Andric  template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>
705cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v) {
7065f757f3fSDimitry Andric    this->__construct_from(std::move(__v));
7070b57cec5SDimitry Andric  }
7080b57cec5SDimitry Andric
70906c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 23
7100eae32dcSDimitry Andric  template <class _Fp, class... _Args>
711cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
712cb14a3feSDimitry Andric      : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}
7130eae32dcSDimitry Andric#  endif
7140eae32dcSDimitry Andric
715cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept {
7160b57cec5SDimitry Andric    reset();
7170b57cec5SDimitry Andric    return *this;
7180b57cec5SDimitry Andric  }
7190b57cec5SDimitry Andric
72006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default;
72106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&)      = default;
7220b57cec5SDimitry Andric
7230b57cec5SDimitry Andric  // LWG2756
724cb14a3feSDimitry Andric  template <
725cb14a3feSDimitry Andric      class _Up = value_type,
726cb14a3feSDimitry Andric      class     = enable_if_t< _And< _IsNotSame<__remove_cvref_t<_Up>, optional>,
727cb14a3feSDimitry Andric                                     _Or< _IsNotSame<__remove_cvref_t<_Up>, value_type>, _Not<is_scalar<value_type>> >,
7280b57cec5SDimitry Andric                                     is_constructible<value_type, _Up>,
729cb14a3feSDimitry Andric                                     is_assignable<value_type&, _Up> >::value> >
730cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) {
7310b57cec5SDimitry Andric    if (this->has_value())
7325f757f3fSDimitry Andric      this->__get() = std::forward<_Up>(__v);
7330b57cec5SDimitry Andric    else
7345f757f3fSDimitry Andric      this->__construct(std::forward<_Up>(__v));
7350b57cec5SDimitry Andric    return *this;
7360b57cec5SDimitry Andric  }
7370b57cec5SDimitry Andric
7380b57cec5SDimitry Andric  // LWG2756
739cb14a3feSDimitry Andric  template <class _Up,
740cb14a3feSDimitry Andric            enable_if_t< _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0>
741cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) {
7420b57cec5SDimitry Andric    this->__assign_from(__v);
7430b57cec5SDimitry Andric    return *this;
7440b57cec5SDimitry Andric  }
7450b57cec5SDimitry Andric
7460b57cec5SDimitry Andric  // LWG2756
747cb14a3feSDimitry Andric  template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0>
748cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) {
7495f757f3fSDimitry Andric    this->__assign_from(std::move(__v));
7500b57cec5SDimitry Andric    return *this;
7510b57cec5SDimitry Andric  }
7520b57cec5SDimitry Andric
753cb14a3feSDimitry Andric  template <class... _Args, class = enable_if_t< is_constructible_v<value_type, _Args...> > >
754cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
7550b57cec5SDimitry Andric    reset();
7565f757f3fSDimitry Andric    this->__construct(std::forward<_Args>(__args)...);
7570b57cec5SDimitry Andric    return this->__get();
7580b57cec5SDimitry Andric  }
7590b57cec5SDimitry Andric
760cb14a3feSDimitry Andric  template <class _Up,
761cb14a3feSDimitry Andric            class... _Args,
762cb14a3feSDimitry Andric            class = enable_if_t< is_constructible_v<value_type, initializer_list<_Up>&, _Args...> > >
763cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
7640b57cec5SDimitry Andric    reset();
7655f757f3fSDimitry Andric    this->__construct(__il, std::forward<_Args>(__args)...);
7660b57cec5SDimitry Andric    return this->__get();
7670b57cec5SDimitry Andric  }
7680b57cec5SDimitry Andric
769cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
770cb14a3feSDimitry Andric  swap(optional& __opt) noexcept(is_nothrow_move_constructible_v<value_type> && is_nothrow_swappable_v<value_type>) {
771cb14a3feSDimitry Andric    if (this->has_value() == __opt.has_value()) {
7725f757f3fSDimitry Andric      using std::swap;
7730b57cec5SDimitry Andric      if (this->has_value())
7740b57cec5SDimitry Andric        swap(this->__get(), __opt.__get());
775cb14a3feSDimitry Andric    } else {
776cb14a3feSDimitry Andric      if (this->has_value()) {
7775f757f3fSDimitry Andric        __opt.__construct(std::move(this->__get()));
7780b57cec5SDimitry Andric        reset();
779cb14a3feSDimitry Andric      } else {
7805f757f3fSDimitry Andric        this->__construct(std::move(__opt.__get()));
7810b57cec5SDimitry Andric        __opt.reset();
7820b57cec5SDimitry Andric      }
7830b57cec5SDimitry Andric    }
7840b57cec5SDimitry Andric  }
7850b57cec5SDimitry Andric
786*0fca6ea1SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type const> operator->() const noexcept {
78706c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
7885f757f3fSDimitry Andric    return std::addressof(this->__get());
7890b57cec5SDimitry Andric  }
7900b57cec5SDimitry Andric
791*0fca6ea1SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type> operator->() noexcept {
79206c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
7935f757f3fSDimitry Andric    return std::addressof(this->__get());
7940b57cec5SDimitry Andric  }
7950b57cec5SDimitry Andric
796cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr const value_type& operator*() const& noexcept {
79706c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
7980b57cec5SDimitry Andric    return this->__get();
7990b57cec5SDimitry Andric  }
8000b57cec5SDimitry Andric
801cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr value_type& operator*() & noexcept {
80206c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
8030b57cec5SDimitry Andric    return this->__get();
8040b57cec5SDimitry Andric  }
8050b57cec5SDimitry Andric
806cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr value_type&& operator*() && noexcept {
80706c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
8085f757f3fSDimitry Andric    return std::move(this->__get());
8090b57cec5SDimitry Andric  }
8100b57cec5SDimitry Andric
811cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& operator*() const&& noexcept {
81206c3fb27SDimitry Andric    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
8135f757f3fSDimitry Andric    return std::move(this->__get());
8140b57cec5SDimitry Andric  }
8150b57cec5SDimitry Andric
816cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); }
8170b57cec5SDimitry Andric
8180b57cec5SDimitry Andric  using __base::__get;
819cb14a3feSDimitry Andric  using __base::has_value;
8200b57cec5SDimitry Andric
821cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type const& value() const& {
8220b57cec5SDimitry Andric    if (!this->has_value())
8230b57cec5SDimitry Andric      __throw_bad_optional_access();
8240b57cec5SDimitry Andric    return this->__get();
8250b57cec5SDimitry Andric  }
8260b57cec5SDimitry Andric
827cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type& value() & {
8280b57cec5SDimitry Andric    if (!this->has_value())
8290b57cec5SDimitry Andric      __throw_bad_optional_access();
8300b57cec5SDimitry Andric    return this->__get();
8310b57cec5SDimitry Andric  }
8320b57cec5SDimitry Andric
833cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type&& value() && {
8340b57cec5SDimitry Andric    if (!this->has_value())
8350b57cec5SDimitry Andric      __throw_bad_optional_access();
8365f757f3fSDimitry Andric    return std::move(this->__get());
8370b57cec5SDimitry Andric  }
8380b57cec5SDimitry Andric
839cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type const&& value() const&& {
8400b57cec5SDimitry Andric    if (!this->has_value())
8410b57cec5SDimitry Andric      __throw_bad_optional_access();
8425f757f3fSDimitry Andric    return std::move(this->__get());
8430b57cec5SDimitry Andric  }
8440b57cec5SDimitry Andric
8450b57cec5SDimitry Andric  template <class _Up>
846cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) const& {
847cb14a3feSDimitry Andric    static_assert(is_copy_constructible_v<value_type>, "optional<T>::value_or: T must be copy constructible");
848cb14a3feSDimitry Andric    static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
849cb14a3feSDimitry Andric    return this->has_value() ? this->__get() : static_cast<value_type>(std::forward<_Up>(__v));
8500b57cec5SDimitry Andric  }
8510b57cec5SDimitry Andric
8520b57cec5SDimitry Andric  template <class _Up>
853cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) && {
854cb14a3feSDimitry Andric    static_assert(is_move_constructible_v<value_type>, "optional<T>::value_or: T must be move constructible");
855cb14a3feSDimitry Andric    static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
856cb14a3feSDimitry Andric    return this->has_value() ? std::move(this->__get()) : static_cast<value_type>(std::forward<_Up>(__v));
8570b57cec5SDimitry Andric  }
8580b57cec5SDimitry Andric
85906c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 23
8600eae32dcSDimitry Andric  template <class _Func>
861cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) & {
8620eae32dcSDimitry Andric    using _Up = invoke_result_t<_Func, value_type&>;
8630eae32dcSDimitry Andric    static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
8640eae32dcSDimitry Andric                  "Result of f(value()) must be a specialization of std::optional");
8650eae32dcSDimitry Andric    if (*this)
8665f757f3fSDimitry Andric      return std::invoke(std::forward<_Func>(__f), value());
8670eae32dcSDimitry Andric    return remove_cvref_t<_Up>();
8680eae32dcSDimitry Andric  }
8690eae32dcSDimitry Andric
8700eae32dcSDimitry Andric  template <class _Func>
871cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) const& {
8720eae32dcSDimitry Andric    using _Up = invoke_result_t<_Func, const value_type&>;
8730eae32dcSDimitry Andric    static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
8740eae32dcSDimitry Andric                  "Result of f(value()) must be a specialization of std::optional");
8750eae32dcSDimitry Andric    if (*this)
8765f757f3fSDimitry Andric      return std::invoke(std::forward<_Func>(__f), value());
8770eae32dcSDimitry Andric    return remove_cvref_t<_Up>();
8780eae32dcSDimitry Andric  }
8790eae32dcSDimitry Andric
8800eae32dcSDimitry Andric  template <class _Func>
881cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) && {
8820eae32dcSDimitry Andric    using _Up = invoke_result_t<_Func, value_type&&>;
8830eae32dcSDimitry Andric    static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
8840eae32dcSDimitry Andric                  "Result of f(std::move(value())) must be a specialization of std::optional");
8850eae32dcSDimitry Andric    if (*this)
8865f757f3fSDimitry Andric      return std::invoke(std::forward<_Func>(__f), std::move(value()));
8870eae32dcSDimitry Andric    return remove_cvref_t<_Up>();
8880eae32dcSDimitry Andric  }
8890eae32dcSDimitry Andric
8900eae32dcSDimitry Andric  template <class _Func>
891cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
8920eae32dcSDimitry Andric    using _Up = invoke_result_t<_Func, const value_type&&>;
8930eae32dcSDimitry Andric    static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
8940eae32dcSDimitry Andric                  "Result of f(std::move(value())) must be a specialization of std::optional");
8950eae32dcSDimitry Andric    if (*this)
8965f757f3fSDimitry Andric      return std::invoke(std::forward<_Func>(__f), std::move(value()));
8970eae32dcSDimitry Andric    return remove_cvref_t<_Up>();
8980eae32dcSDimitry Andric  }
8990eae32dcSDimitry Andric
9000eae32dcSDimitry Andric  template <class _Func>
901cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) & {
9020eae32dcSDimitry Andric    using _Up = remove_cv_t<invoke_result_t<_Func, value_type&>>;
9030eae32dcSDimitry Andric    static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
904cb14a3feSDimitry Andric    static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
905cb14a3feSDimitry Andric    static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
9060eae32dcSDimitry Andric    static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
9070eae32dcSDimitry Andric    if (*this)
9085f757f3fSDimitry Andric      return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
9090eae32dcSDimitry Andric    return optional<_Up>();
9100eae32dcSDimitry Andric  }
9110eae32dcSDimitry Andric
9120eae32dcSDimitry Andric  template <class _Func>
913cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) const& {
9140eae32dcSDimitry Andric    using _Up = remove_cv_t<invoke_result_t<_Func, const value_type&>>;
9150eae32dcSDimitry Andric    static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
916cb14a3feSDimitry Andric    static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
917cb14a3feSDimitry Andric    static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
9180eae32dcSDimitry Andric    static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
9190eae32dcSDimitry Andric    if (*this)
9205f757f3fSDimitry Andric      return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
9210eae32dcSDimitry Andric    return optional<_Up>();
9220eae32dcSDimitry Andric  }
9230eae32dcSDimitry Andric
9240eae32dcSDimitry Andric  template <class _Func>
925cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) && {
9260eae32dcSDimitry Andric    using _Up = remove_cv_t<invoke_result_t<_Func, value_type&&>>;
9270eae32dcSDimitry Andric    static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
928cb14a3feSDimitry Andric    static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
929cb14a3feSDimitry Andric    static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
9300eae32dcSDimitry Andric    static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
9310eae32dcSDimitry Andric    if (*this)
9325f757f3fSDimitry Andric      return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
9330eae32dcSDimitry Andric    return optional<_Up>();
9340eae32dcSDimitry Andric  }
9350eae32dcSDimitry Andric
9360eae32dcSDimitry Andric  template <class _Func>
937cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) const&& {
9380eae32dcSDimitry Andric    using _Up = remove_cvref_t<invoke_result_t<_Func, const value_type&&>>;
9390eae32dcSDimitry Andric    static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
940cb14a3feSDimitry Andric    static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
941cb14a3feSDimitry Andric    static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
9420eae32dcSDimitry Andric    static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
9430eae32dcSDimitry Andric    if (*this)
9445f757f3fSDimitry Andric      return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
9450eae32dcSDimitry Andric    return optional<_Up>();
9460eae32dcSDimitry Andric  }
9470eae32dcSDimitry Andric
9480eae32dcSDimitry Andric  template <invocable _Func>
949cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
950cb14a3feSDimitry Andric    requires is_copy_constructible_v<value_type>
951cb14a3feSDimitry Andric  {
9520eae32dcSDimitry Andric    static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
9530eae32dcSDimitry Andric                  "Result of f() should be the same type as this optional");
9540eae32dcSDimitry Andric    if (*this)
9550eae32dcSDimitry Andric      return *this;
9565f757f3fSDimitry Andric    return std::forward<_Func>(__f)();
9570eae32dcSDimitry Andric  }
9580eae32dcSDimitry Andric
9590eae32dcSDimitry Andric  template <invocable _Func>
960cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
961cb14a3feSDimitry Andric    requires is_move_constructible_v<value_type>
962cb14a3feSDimitry Andric  {
9630eae32dcSDimitry Andric    static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
9640eae32dcSDimitry Andric                  "Result of f() should be the same type as this optional");
9650eae32dcSDimitry Andric    if (*this)
9665f757f3fSDimitry Andric      return std::move(*this);
9675f757f3fSDimitry Andric    return std::forward<_Func>(__f)();
9680eae32dcSDimitry Andric  }
96906c3fb27SDimitry Andric#  endif // _LIBCPP_STD_VER >= 23
9700eae32dcSDimitry Andric
9710b57cec5SDimitry Andric  using __base::reset;
9720b57cec5SDimitry Andric};
9730b57cec5SDimitry Andric
974349cc55cSDimitry Andric#  if _LIBCPP_STD_VER >= 17
97504eeddc0SDimitry Andrictemplate <class _Tp>
97604eeddc0SDimitry Andricoptional(_Tp) -> optional<_Tp>;
9770b57cec5SDimitry Andric#  endif
9780b57cec5SDimitry Andric
9790b57cec5SDimitry Andric// Comparisons between optionals
9800b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
981cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
982cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
983cb14a3feSDimitry Andric    bool >
984cb14a3feSDimitry Andricoperator==(const optional<_Tp>& __x, const optional<_Up>& __y) {
9850b57cec5SDimitry Andric  if (static_cast<bool>(__x) != static_cast<bool>(__y))
9860b57cec5SDimitry Andric    return false;
9870b57cec5SDimitry Andric  if (!static_cast<bool>(__x))
9880b57cec5SDimitry Andric    return true;
9890b57cec5SDimitry Andric  return *__x == *__y;
9900b57cec5SDimitry Andric}
9910b57cec5SDimitry Andric
9920b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
993cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
994cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
995cb14a3feSDimitry Andric    bool >
996cb14a3feSDimitry Andricoperator!=(const optional<_Tp>& __x, const optional<_Up>& __y) {
9970b57cec5SDimitry Andric  if (static_cast<bool>(__x) != static_cast<bool>(__y))
9980b57cec5SDimitry Andric    return true;
9990b57cec5SDimitry Andric  if (!static_cast<bool>(__x))
10000b57cec5SDimitry Andric    return false;
10010b57cec5SDimitry Andric  return *__x != *__y;
10020b57cec5SDimitry Andric}
10030b57cec5SDimitry Andric
10040b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1005cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1006cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1007cb14a3feSDimitry Andric    bool >
1008cb14a3feSDimitry Andricoperator<(const optional<_Tp>& __x, const optional<_Up>& __y) {
10090b57cec5SDimitry Andric  if (!static_cast<bool>(__y))
10100b57cec5SDimitry Andric    return false;
10110b57cec5SDimitry Andric  if (!static_cast<bool>(__x))
10120b57cec5SDimitry Andric    return true;
10130b57cec5SDimitry Andric  return *__x < *__y;
10140b57cec5SDimitry Andric}
10150b57cec5SDimitry Andric
10160b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1017cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1018cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1019cb14a3feSDimitry Andric    bool >
1020cb14a3feSDimitry Andricoperator>(const optional<_Tp>& __x, const optional<_Up>& __y) {
10210b57cec5SDimitry Andric  if (!static_cast<bool>(__x))
10220b57cec5SDimitry Andric    return false;
10230b57cec5SDimitry Andric  if (!static_cast<bool>(__y))
10240b57cec5SDimitry Andric    return true;
10250b57cec5SDimitry Andric  return *__x > *__y;
10260b57cec5SDimitry Andric}
10270b57cec5SDimitry Andric
10280b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1029cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1030cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1031cb14a3feSDimitry Andric    bool >
1032cb14a3feSDimitry Andricoperator<=(const optional<_Tp>& __x, const optional<_Up>& __y) {
10330b57cec5SDimitry Andric  if (!static_cast<bool>(__x))
10340b57cec5SDimitry Andric    return true;
10350b57cec5SDimitry Andric  if (!static_cast<bool>(__y))
10360b57cec5SDimitry Andric    return false;
10370b57cec5SDimitry Andric  return *__x <= *__y;
10380b57cec5SDimitry Andric}
10390b57cec5SDimitry Andric
10400b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1041cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1042cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1043cb14a3feSDimitry Andric    bool >
1044cb14a3feSDimitry Andricoperator>=(const optional<_Tp>& __x, const optional<_Up>& __y) {
10450b57cec5SDimitry Andric  if (!static_cast<bool>(__y))
10460b57cec5SDimitry Andric    return true;
10470b57cec5SDimitry Andric  if (!static_cast<bool>(__x))
10480b57cec5SDimitry Andric    return false;
10490b57cec5SDimitry Andric  return *__x >= *__y;
10500b57cec5SDimitry Andric}
10510b57cec5SDimitry Andric
105206c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
105306c3fb27SDimitry Andric
105406c3fb27SDimitry Andrictemplate <class _Tp, three_way_comparable_with<_Tp> _Up>
105506c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
105606c3fb27SDimitry Andricoperator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) {
105706c3fb27SDimitry Andric  if (__x && __y)
105806c3fb27SDimitry Andric    return *__x <=> *__y;
105906c3fb27SDimitry Andric  return __x.has_value() <=> __y.has_value();
106006c3fb27SDimitry Andric}
106106c3fb27SDimitry Andric
106206c3fb27SDimitry Andric#  endif // _LIBCPP_STD_VER >= 20
106306c3fb27SDimitry Andric
10640b57cec5SDimitry Andric// Comparisons with nullopt
10650b57cec5SDimitry Andrictemplate <class _Tp>
1066cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept {
10670b57cec5SDimitry Andric  return !static_cast<bool>(__x);
10680b57cec5SDimitry Andric}
10690b57cec5SDimitry Andric
107006c3fb27SDimitry Andric#  if _LIBCPP_STD_VER <= 17
107106c3fb27SDimitry Andric
10720b57cec5SDimitry Andrictemplate <class _Tp>
1073cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(nullopt_t, const optional<_Tp>& __x) noexcept {
10740b57cec5SDimitry Andric  return !static_cast<bool>(__x);
10750b57cec5SDimitry Andric}
10760b57cec5SDimitry Andric
10770b57cec5SDimitry Andrictemplate <class _Tp>
1078cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, nullopt_t) noexcept {
10790b57cec5SDimitry Andric  return static_cast<bool>(__x);
10800b57cec5SDimitry Andric}
10810b57cec5SDimitry Andric
10820b57cec5SDimitry Andrictemplate <class _Tp>
1083cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(nullopt_t, const optional<_Tp>& __x) noexcept {
10840b57cec5SDimitry Andric  return static_cast<bool>(__x);
10850b57cec5SDimitry Andric}
10860b57cec5SDimitry Andric
10870b57cec5SDimitry Andrictemplate <class _Tp>
1088cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>&, nullopt_t) noexcept {
10890b57cec5SDimitry Andric  return false;
10900b57cec5SDimitry Andric}
10910b57cec5SDimitry Andric
10920b57cec5SDimitry Andrictemplate <class _Tp>
1093cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(nullopt_t, const optional<_Tp>& __x) noexcept {
10940b57cec5SDimitry Andric  return static_cast<bool>(__x);
10950b57cec5SDimitry Andric}
10960b57cec5SDimitry Andric
10970b57cec5SDimitry Andrictemplate <class _Tp>
1098cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, nullopt_t) noexcept {
10990b57cec5SDimitry Andric  return !static_cast<bool>(__x);
11000b57cec5SDimitry Andric}
11010b57cec5SDimitry Andric
11020b57cec5SDimitry Andrictemplate <class _Tp>
1103cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(nullopt_t, const optional<_Tp>&) noexcept {
11040b57cec5SDimitry Andric  return true;
11050b57cec5SDimitry Andric}
11060b57cec5SDimitry Andric
11070b57cec5SDimitry Andrictemplate <class _Tp>
1108cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, nullopt_t) noexcept {
11090b57cec5SDimitry Andric  return static_cast<bool>(__x);
11100b57cec5SDimitry Andric}
11110b57cec5SDimitry Andric
11120b57cec5SDimitry Andrictemplate <class _Tp>
1113cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(nullopt_t, const optional<_Tp>&) noexcept {
11140b57cec5SDimitry Andric  return false;
11150b57cec5SDimitry Andric}
11160b57cec5SDimitry Andric
11170b57cec5SDimitry Andrictemplate <class _Tp>
1118cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>&, nullopt_t) noexcept {
11190b57cec5SDimitry Andric  return true;
11200b57cec5SDimitry Andric}
11210b57cec5SDimitry Andric
11220b57cec5SDimitry Andrictemplate <class _Tp>
1123cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(nullopt_t, const optional<_Tp>& __x) noexcept {
11240b57cec5SDimitry Andric  return !static_cast<bool>(__x);
11250b57cec5SDimitry Andric}
11260b57cec5SDimitry Andric
112706c3fb27SDimitry Andric#  else // _LIBCPP_STD_VER <= 17
112806c3fb27SDimitry Andric
112906c3fb27SDimitry Andrictemplate <class _Tp>
113006c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept {
113106c3fb27SDimitry Andric  return __x.has_value() <=> false;
113206c3fb27SDimitry Andric}
113306c3fb27SDimitry Andric
113406c3fb27SDimitry Andric#  endif // _LIBCPP_STD_VER <= 17
113506c3fb27SDimitry Andric
11360b57cec5SDimitry Andric// Comparisons with T
11370b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1138cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1139cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1140cb14a3feSDimitry Andric    bool >
1141cb14a3feSDimitry Andricoperator==(const optional<_Tp>& __x, const _Up& __v) {
11420b57cec5SDimitry Andric  return static_cast<bool>(__x) ? *__x == __v : false;
11430b57cec5SDimitry Andric}
11440b57cec5SDimitry Andric
11450b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1146cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1147cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
1148cb14a3feSDimitry Andric    bool >
1149cb14a3feSDimitry Andricoperator==(const _Tp& __v, const optional<_Up>& __x) {
11500b57cec5SDimitry Andric  return static_cast<bool>(__x) ? __v == *__x : false;
11510b57cec5SDimitry Andric}
11520b57cec5SDimitry Andric
11530b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1154cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1155cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1156cb14a3feSDimitry Andric    bool >
1157cb14a3feSDimitry Andricoperator!=(const optional<_Tp>& __x, const _Up& __v) {
11580b57cec5SDimitry Andric  return static_cast<bool>(__x) ? *__x != __v : true;
11590b57cec5SDimitry Andric}
11600b57cec5SDimitry Andric
11610b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1162cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1163cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
1164cb14a3feSDimitry Andric    bool >
1165cb14a3feSDimitry Andricoperator!=(const _Tp& __v, const optional<_Up>& __x) {
11660b57cec5SDimitry Andric  return static_cast<bool>(__x) ? __v != *__x : true;
11670b57cec5SDimitry Andric}
11680b57cec5SDimitry Andric
11690b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1170cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1171cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1172cb14a3feSDimitry Andric    bool >
1173cb14a3feSDimitry Andricoperator<(const optional<_Tp>& __x, const _Up& __v) {
11740b57cec5SDimitry Andric  return static_cast<bool>(__x) ? *__x < __v : true;
11750b57cec5SDimitry Andric}
11760b57cec5SDimitry Andric
11770b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1178cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1179cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
1180cb14a3feSDimitry Andric    bool >
1181cb14a3feSDimitry Andricoperator<(const _Tp& __v, const optional<_Up>& __x) {
11820b57cec5SDimitry Andric  return static_cast<bool>(__x) ? __v < *__x : false;
11830b57cec5SDimitry Andric}
11840b57cec5SDimitry Andric
11850b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1186cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1187cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1188cb14a3feSDimitry Andric    bool >
1189cb14a3feSDimitry Andricoperator<=(const optional<_Tp>& __x, const _Up& __v) {
11900b57cec5SDimitry Andric  return static_cast<bool>(__x) ? *__x <= __v : true;
11910b57cec5SDimitry Andric}
11920b57cec5SDimitry Andric
11930b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1194cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1195cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
1196cb14a3feSDimitry Andric    bool >
1197cb14a3feSDimitry Andricoperator<=(const _Tp& __v, const optional<_Up>& __x) {
11980b57cec5SDimitry Andric  return static_cast<bool>(__x) ? __v <= *__x : false;
11990b57cec5SDimitry Andric}
12000b57cec5SDimitry Andric
12010b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1202cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1203cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1204cb14a3feSDimitry Andric    bool >
1205cb14a3feSDimitry Andricoperator>(const optional<_Tp>& __x, const _Up& __v) {
12060b57cec5SDimitry Andric  return static_cast<bool>(__x) ? *__x > __v : false;
12070b57cec5SDimitry Andric}
12080b57cec5SDimitry Andric
12090b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1210cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1211cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
1212cb14a3feSDimitry Andric    bool >
1213cb14a3feSDimitry Andricoperator>(const _Tp& __v, const optional<_Up>& __x) {
12140b57cec5SDimitry Andric  return static_cast<bool>(__x) ? __v > *__x : true;
12150b57cec5SDimitry Andric}
12160b57cec5SDimitry Andric
12170b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1218cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1219cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1220cb14a3feSDimitry Andric    bool >
1221cb14a3feSDimitry Andricoperator>=(const optional<_Tp>& __x, const _Up& __v) {
12220b57cec5SDimitry Andric  return static_cast<bool>(__x) ? *__x >= __v : false;
12230b57cec5SDimitry Andric}
12240b57cec5SDimitry Andric
12250b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
1226cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
1227cb14a3feSDimitry Andric    is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
1228cb14a3feSDimitry Andric    bool >
1229cb14a3feSDimitry Andricoperator>=(const _Tp& __v, const optional<_Up>& __x) {
12300b57cec5SDimitry Andric  return static_cast<bool>(__x) ? __v >= *__x : true;
12310b57cec5SDimitry Andric}
12320b57cec5SDimitry Andric
123306c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
123406c3fb27SDimitry Andric
123506c3fb27SDimitry Andrictemplate <class _Tp, class _Up>
123606c3fb27SDimitry Andric  requires(!__is_derived_from_optional<_Up>) && three_way_comparable_with<_Tp, _Up>
123706c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
123806c3fb27SDimitry Andricoperator<=>(const optional<_Tp>& __x, const _Up& __v) {
123906c3fb27SDimitry Andric  return __x.has_value() ? *__x <=> __v : strong_ordering::less;
124006c3fb27SDimitry Andric}
124106c3fb27SDimitry Andric
124206c3fb27SDimitry Andric#  endif // _LIBCPP_STD_VER >= 20
124306c3fb27SDimitry Andric
12440b57cec5SDimitry Andrictemplate <class _Tp>
1245*0fca6ea1SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
1246*0fca6ea1SDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, void >
1247cb14a3feSDimitry Andricswap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
12480b57cec5SDimitry Andric  __x.swap(__y);
12490b57cec5SDimitry Andric}
12500b57cec5SDimitry Andric
12510b57cec5SDimitry Andrictemplate <class _Tp>
1252cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
12535f757f3fSDimitry Andric  return optional<decay_t<_Tp>>(std::forward<_Tp>(__v));
12540b57cec5SDimitry Andric}
12550b57cec5SDimitry Andric
12560b57cec5SDimitry Andrictemplate <class _Tp, class... _Args>
1257cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
12585f757f3fSDimitry Andric  return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
12590b57cec5SDimitry Andric}
12600b57cec5SDimitry Andric
12610b57cec5SDimitry Andrictemplate <class _Tp, class _Up, class... _Args>
1262cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) {
12635f757f3fSDimitry Andric  return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
12640b57cec5SDimitry Andric}
12650b57cec5SDimitry Andric
12660b57cec5SDimitry Andrictemplate <class _Tp>
1267cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > {
1268fe6060f1SDimitry Andric#  if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1269fe6060f1SDimitry Andric  _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type;
1270fe6060f1SDimitry Andric  _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
1271fe6060f1SDimitry Andric#  endif
12720b57cec5SDimitry Andric
1273cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
12740b57cec5SDimitry Andric    return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
12750b57cec5SDimitry Andric  }
12760b57cec5SDimitry Andric};
12770b57cec5SDimitry Andric
12780b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
12790b57cec5SDimitry Andric
128006c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 17
128106c3fb27SDimitry Andric
128206c3fb27SDimitry Andric_LIBCPP_POP_MACROS
12830b57cec5SDimitry Andric
1284bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1285bdd1243dSDimitry Andric#  include <atomic>
1286bdd1243dSDimitry Andric#  include <climits>
1287bdd1243dSDimitry Andric#  include <concepts>
1288bdd1243dSDimitry Andric#  include <ctime>
1289bdd1243dSDimitry Andric#  include <iterator>
1290*0fca6ea1SDimitry Andric#  include <limits>
1291bdd1243dSDimitry Andric#  include <memory>
1292bdd1243dSDimitry Andric#  include <ratio>
12935f757f3fSDimitry Andric#  include <stdexcept>
1294bdd1243dSDimitry Andric#  include <tuple>
129506c3fb27SDimitry Andric#  include <type_traits>
1296bdd1243dSDimitry Andric#  include <typeinfo>
1297bdd1243dSDimitry Andric#  include <utility>
1298bdd1243dSDimitry Andric#  include <variant>
1299bdd1243dSDimitry Andric#endif
1300bdd1243dSDimitry Andric
13010b57cec5SDimitry Andric#endif // _LIBCPP_OPTIONAL
1302