xref: /freebsd/contrib/llvm-project/libcxx/include/optional (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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 {
190b57cec5SDimitry Andric  // 23.6.3, optional for object types
200b57cec5SDimitry Andric  template <class T> class optional;
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric  // 23.6.4, no-value state indicator
230b57cec5SDimitry Andric  struct nullopt_t{see below };
240b57cec5SDimitry Andric  inline constexpr nullopt_t nullopt(unspecified );
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric  // 23.6.5, class bad_optional_access
270b57cec5SDimitry Andric  class bad_optional_access;
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric  // 23.6.6, relational operators
300b57cec5SDimitry Andric  template <class T, class U>
310b57cec5SDimitry Andric  constexpr bool operator==(const optional<T>&, const optional<U>&);
320b57cec5SDimitry Andric  template <class T, class U>
330b57cec5SDimitry Andric  constexpr bool operator!=(const optional<T>&, const optional<U>&);
340b57cec5SDimitry Andric  template <class T, class U>
350b57cec5SDimitry Andric  constexpr bool operator<(const optional<T>&, const optional<U>&);
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
430b57cec5SDimitry Andric  // 23.6.7 comparison with nullopt
440b57cec5SDimitry Andric  template <class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;
450b57cec5SDimitry Andric  template <class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept;
460b57cec5SDimitry Andric  template <class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept;
470b57cec5SDimitry Andric  template <class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept;
480b57cec5SDimitry Andric  template <class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept;
490b57cec5SDimitry Andric  template <class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept;
500b57cec5SDimitry Andric  template <class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept;
510b57cec5SDimitry Andric  template <class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept;
520b57cec5SDimitry Andric  template <class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept;
530b57cec5SDimitry Andric  template <class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept;
540b57cec5SDimitry Andric  template <class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept;
550b57cec5SDimitry Andric  template <class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept;
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric  // 23.6.8, comparison with T
580b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator==(const optional<T>&, const U&);
590b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator==(const T&, const optional<U>&);
600b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator!=(const optional<T>&, const U&);
610b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator!=(const T&, const optional<U>&);
620b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator<(const optional<T>&, const U&);
630b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator<(const T&, const optional<U>&);
640b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator<=(const optional<T>&, const U&);
650b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator<=(const T&, const optional<U>&);
660b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator>(const optional<T>&, const U&);
670b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator>(const T&, const optional<U>&);
680b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator>=(const optional<T>&, const U&);
690b57cec5SDimitry Andric  template <class T, class U> constexpr bool operator>=(const T&, const optional<U>&);
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric  // 23.6.9, specialized algorithms
72fe6060f1SDimitry Andric  template <class T> void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20
730b57cec5SDimitry Andric  template <class T> constexpr optional<see below > make_optional(T&&);
740b57cec5SDimitry Andric  template <class T, class... Args>
750b57cec5SDimitry Andric    constexpr optional<T> make_optional(Args&&... args);
760b57cec5SDimitry Andric  template <class T, class U, class... Args>
770b57cec5SDimitry Andric    constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric  // 23.6.10, hash support
800b57cec5SDimitry Andric  template <class T> struct hash;
810b57cec5SDimitry Andric  template <class T> struct hash<optional<T>>;
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric  template <class T> class optional {
840b57cec5SDimitry Andric  public:
850b57cec5SDimitry Andric    using value_type = T;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric    // 23.6.3.1, constructors
880b57cec5SDimitry Andric    constexpr optional() noexcept;
890b57cec5SDimitry Andric    constexpr optional(nullopt_t) noexcept;
900b57cec5SDimitry Andric    optional(const optional &);
910b57cec5SDimitry Andric    optional(optional &&) noexcept(see below);
920b57cec5SDimitry Andric    template <class... Args> constexpr explicit optional(in_place_t, Args &&...);
930b57cec5SDimitry Andric    template <class U, class... Args>
940b57cec5SDimitry Andric      constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...);
950b57cec5SDimitry Andric    template <class U = T>
96*81ad6265SDimitry Andric      constexpr explicit(see-below) optional(U &&);
970b57cec5SDimitry Andric    template <class U>
98*81ad6265SDimitry Andric      explicit(see-below) optional(const optional<U> &);         // constexpr in C++20
990b57cec5SDimitry Andric    template <class U>
100*81ad6265SDimitry Andric      explicit(see-below) optional(optional<U> &&);              // constexpr in C++20
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric    // 23.6.3.2, destructor
103fe6060f1SDimitry Andric    ~optional(); // constexpr in C++20
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric    // 23.6.3.3, assignment
106fe6060f1SDimitry Andric    optional &operator=(nullopt_t) noexcept;                     // constexpr in C++20
1070b57cec5SDimitry Andric    optional &operator=(const optional &);                       // constexpr in C++20
1080b57cec5SDimitry Andric    optional &operator=(optional &&) noexcept(see below);        // constexpr in C++20
109fe6060f1SDimitry Andric    template <class U = T> optional &operator=(U &&);            // constexpr in C++20
110fe6060f1SDimitry Andric    template <class U> optional &operator=(const optional<U> &); // constexpr in C++20
111fe6060f1SDimitry Andric    template <class U> optional &operator=(optional<U> &&);      // constexpr in C++20
112fe6060f1SDimitry Andric    template <class... Args> T& emplace(Args &&...);             // constexpr in C++20
1130b57cec5SDimitry Andric    template <class U, class... Args>
114fe6060f1SDimitry Andric      T& emplace(initializer_list<U>, Args &&...);               // constexpr in C++20
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric    // 23.6.3.4, swap
117fe6060f1SDimitry Andric    void swap(optional &) noexcept(see below ); // constexpr in C++20
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric    // 23.6.3.5, observers
1200b57cec5SDimitry Andric    constexpr T const *operator->() const;
1210b57cec5SDimitry Andric    constexpr T *operator->();
1220b57cec5SDimitry Andric    constexpr T const &operator*() const &;
1230b57cec5SDimitry Andric    constexpr T &operator*() &;
1240b57cec5SDimitry Andric    constexpr T &&operator*() &&;
1250b57cec5SDimitry Andric    constexpr const T &&operator*() const &&;
1260b57cec5SDimitry Andric    constexpr explicit operator bool() const noexcept;
1270b57cec5SDimitry Andric    constexpr bool has_value() const noexcept;
1280b57cec5SDimitry Andric    constexpr T const &value() const &;
1290b57cec5SDimitry Andric    constexpr T &value() &;
1300b57cec5SDimitry Andric    constexpr T &&value() &&;
1310b57cec5SDimitry Andric    constexpr const T &&value() const &&;
1320b57cec5SDimitry Andric    template <class U> constexpr T value_or(U &&) const &;
1330b57cec5SDimitry Andric    template <class U> constexpr T value_or(U &&) &&;
1340b57cec5SDimitry Andric
1350eae32dcSDimitry Andric    // [optional.monadic], monadic operations
1360eae32dcSDimitry Andric    template<class F> constexpr auto and_then(F&& f) &;         // since C++23
1370eae32dcSDimitry Andric    template<class F> constexpr auto and_then(F&& f) &&;        // since C++23
1380eae32dcSDimitry Andric    template<class F> constexpr auto and_then(F&& f) const&;    // since C++23
1390eae32dcSDimitry Andric    template<class F> constexpr auto and_then(F&& f) const&&;   // since C++23
1400eae32dcSDimitry Andric    template<class F> constexpr auto transform(F&& f) &;        // since C++23
1410eae32dcSDimitry Andric    template<class F> constexpr auto transform(F&& f) &&;       // since C++23
1420eae32dcSDimitry Andric    template<class F> constexpr auto transform(F&& f) const&;   // since C++23
1430eae32dcSDimitry Andric    template<class F> constexpr auto transform(F&& f) const&&;  // since C++23
1440eae32dcSDimitry Andric    template<class F> constexpr optional or_else(F&& f) &&;     // since C++23
1450eae32dcSDimitry Andric    template<class F> constexpr optional or_else(F&& f) const&; // since C++23
1460eae32dcSDimitry Andric
1470b57cec5SDimitry Andric    // 23.6.3.6, modifiers
148fe6060f1SDimitry Andric    void reset() noexcept; // constexpr in C++20
1490b57cec5SDimitry Andric
1500b57cec5SDimitry Andric  private:
1510b57cec5SDimitry Andric    T *val; // exposition only
1520b57cec5SDimitry Andric  };
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andrictemplate<class T>
1550b57cec5SDimitry Andric  optional(T) -> optional<T>;
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric} // namespace std
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric*/
1600b57cec5SDimitry Andric
161*81ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
162e8d8bef9SDimitry Andric#include <__availability>
1630eae32dcSDimitry Andric#include <__concepts/invocable.h>
164fe6060f1SDimitry Andric#include <__config>
165*81ad6265SDimitry Andric#include <__functional/hash.h>
166*81ad6265SDimitry Andric#include <__functional/invoke.h>
167*81ad6265SDimitry Andric#include <__functional/unary_function.h>
168*81ad6265SDimitry Andric#include <__memory/construct_at.h>
169*81ad6265SDimitry Andric#include <__tuple>
170*81ad6265SDimitry Andric#include <__utility/forward.h>
171*81ad6265SDimitry Andric#include <__utility/in_place.h>
172*81ad6265SDimitry Andric#include <__utility/move.h>
173*81ad6265SDimitry Andric#include <__utility/swap.h>
1740b57cec5SDimitry Andric#include <initializer_list>
1750b57cec5SDimitry Andric#include <new>
1760b57cec5SDimitry Andric#include <stdexcept>
1770b57cec5SDimitry Andric#include <type_traits>
1780b57cec5SDimitry Andric#include <version>
1790b57cec5SDimitry Andric
180*81ad6265SDimitry Andric#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
181*81ad6265SDimitry Andric#  include <atomic>
182*81ad6265SDimitry Andric#  include <chrono>
183*81ad6265SDimitry Andric#  include <climits>
184*81ad6265SDimitry Andric#  include <concepts>
185*81ad6265SDimitry Andric#  include <ctime>
186*81ad6265SDimitry Andric#  include <iterator>
187*81ad6265SDimitry Andric#  include <memory>
188*81ad6265SDimitry Andric#  include <ratio>
189*81ad6265SDimitry Andric#  include <tuple>
190*81ad6265SDimitry Andric#  include <typeinfo>
191*81ad6265SDimitry Andric#  include <utility>
192*81ad6265SDimitry Andric#  include <variant>
193*81ad6265SDimitry Andric#endif
194*81ad6265SDimitry Andric
195*81ad6265SDimitry Andric// standard-mandated includes
196*81ad6265SDimitry Andric#include <compare>
197*81ad6265SDimitry Andric
1980b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1990b57cec5SDimitry Andric#  pragma GCC system_header
2000b57cec5SDimitry Andric#endif
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andricnamespace std  // purposefully not using versioning namespace
2030b57cec5SDimitry Andric{
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access
2060b57cec5SDimitry Andric    : public exception
2070b57cec5SDimitry Andric{
2080b57cec5SDimitry Andricpublic:
2090b57cec5SDimitry Andric    // Get the key function ~bad_optional_access() into the dylib
2100b57cec5SDimitry Andric    virtual ~bad_optional_access() _NOEXCEPT;
2110b57cec5SDimitry Andric    virtual const char* what() const _NOEXCEPT;
2120b57cec5SDimitry Andric};
2130b57cec5SDimitry Andric
2140eae32dcSDimitry Andric} // namespace std
2150b57cec5SDimitry Andric
2160b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric_LIBCPP_NORETURN
2210b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2220b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
2230b57cec5SDimitry Andricvoid __throw_bad_optional_access() {
2240b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
2250b57cec5SDimitry Andric        throw bad_optional_access();
2260b57cec5SDimitry Andric#else
2270b57cec5SDimitry Andric        _VSTD::abort();
2280b57cec5SDimitry Andric#endif
2290b57cec5SDimitry Andric}
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andricstruct nullopt_t
2320b57cec5SDimitry Andric{
2330b57cec5SDimitry Andric    struct __secret_tag { _LIBCPP_INLINE_VISIBILITY explicit __secret_tag() = default; };
2340b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
2350b57cec5SDimitry Andric};
2360b57cec5SDimitry Andric
237349cc55cSDimitry Andricinline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
2380b57cec5SDimitry Andric
2390eae32dcSDimitry Andricstruct __optional_construct_from_invoke_tag {};
2400eae32dcSDimitry Andric
2410b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_destructible<_Tp>::value>
2420b57cec5SDimitry Andricstruct __optional_destruct_base;
2430b57cec5SDimitry Andric
2440b57cec5SDimitry Andrictemplate <class _Tp>
2450b57cec5SDimitry Andricstruct __optional_destruct_base<_Tp, false>
2460b57cec5SDimitry Andric{
2470b57cec5SDimitry Andric    typedef _Tp value_type;
2480b57cec5SDimitry Andric    static_assert(is_object_v<value_type>,
2490b57cec5SDimitry Andric        "instantiation of optional with a non-object type is undefined behavior");
2500b57cec5SDimitry Andric    union
2510b57cec5SDimitry Andric    {
2520b57cec5SDimitry Andric        char __null_state_;
2530b57cec5SDimitry Andric        value_type __val_;
2540b57cec5SDimitry Andric    };
2550b57cec5SDimitry Andric    bool __engaged_;
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
258fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 ~__optional_destruct_base()
2590b57cec5SDimitry Andric    {
2600b57cec5SDimitry Andric        if (__engaged_)
2610b57cec5SDimitry Andric            __val_.~value_type();
2620b57cec5SDimitry Andric    }
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2650b57cec5SDimitry Andric    constexpr __optional_destruct_base() noexcept
2660b57cec5SDimitry Andric        :  __null_state_(),
2670b57cec5SDimitry Andric           __engaged_(false) {}
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric    template <class... _Args>
2700b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2710b57cec5SDimitry Andric    constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
2720b57cec5SDimitry Andric        :  __val_(_VSTD::forward<_Args>(__args)...),
2730b57cec5SDimitry Andric           __engaged_(true) {}
2740b57cec5SDimitry Andric
2750eae32dcSDimitry Andric#if _LIBCPP_STD_VER > 20
2760eae32dcSDimitry Andric  template <class _Fp, class... _Args>
2770eae32dcSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
2780eae32dcSDimitry Andric  constexpr __optional_destruct_base(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
2790eae32dcSDimitry Andric      : __val_(_VSTD::invoke(_VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...)), __engaged_(true) {}
2800eae32dcSDimitry Andric#endif
2810eae32dcSDimitry Andric
2820b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
283fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept
2840b57cec5SDimitry Andric    {
2850b57cec5SDimitry Andric        if (__engaged_)
2860b57cec5SDimitry Andric        {
2870b57cec5SDimitry Andric            __val_.~value_type();
2880b57cec5SDimitry Andric            __engaged_ = false;
2890b57cec5SDimitry Andric        }
2900b57cec5SDimitry Andric    }
2910b57cec5SDimitry Andric};
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andrictemplate <class _Tp>
2940b57cec5SDimitry Andricstruct __optional_destruct_base<_Tp, true>
2950b57cec5SDimitry Andric{
2960b57cec5SDimitry Andric    typedef _Tp value_type;
2970b57cec5SDimitry Andric    static_assert(is_object_v<value_type>,
2980b57cec5SDimitry Andric        "instantiation of optional with a non-object type is undefined behavior");
2990b57cec5SDimitry Andric    union
3000b57cec5SDimitry Andric    {
3010b57cec5SDimitry Andric        char __null_state_;
3020b57cec5SDimitry Andric        value_type __val_;
3030b57cec5SDimitry Andric    };
3040b57cec5SDimitry Andric    bool __engaged_;
3050b57cec5SDimitry Andric
3060b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3070b57cec5SDimitry Andric    constexpr __optional_destruct_base() noexcept
3080b57cec5SDimitry Andric        :  __null_state_(),
3090b57cec5SDimitry Andric           __engaged_(false) {}
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric    template <class... _Args>
3120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3130b57cec5SDimitry Andric    constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
3140b57cec5SDimitry Andric        :  __val_(_VSTD::forward<_Args>(__args)...),
3150b57cec5SDimitry Andric           __engaged_(true) {}
3160b57cec5SDimitry Andric
3170eae32dcSDimitry Andric#if _LIBCPP_STD_VER > 20
3180eae32dcSDimitry Andric  template <class _Fp, class... _Args>
3190eae32dcSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
3200eae32dcSDimitry Andric  constexpr __optional_destruct_base(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
3210eae32dcSDimitry Andric      : __val_(_VSTD::invoke(_VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...)), __engaged_(true) {}
3220eae32dcSDimitry Andric#endif
3230eae32dcSDimitry Andric
3240b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
325fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept
3260b57cec5SDimitry Andric    {
3270b57cec5SDimitry Andric        if (__engaged_)
3280b57cec5SDimitry Andric        {
3290b57cec5SDimitry Andric            __engaged_ = false;
3300b57cec5SDimitry Andric        }
3310b57cec5SDimitry Andric    }
3320b57cec5SDimitry Andric};
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andrictemplate <class _Tp, bool = is_reference<_Tp>::value>
3350b57cec5SDimitry Andricstruct __optional_storage_base : __optional_destruct_base<_Tp>
3360b57cec5SDimitry Andric{
3370b57cec5SDimitry Andric    using __base = __optional_destruct_base<_Tp>;
3380b57cec5SDimitry Andric    using value_type = _Tp;
3390b57cec5SDimitry Andric    using __base::__base;
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3420b57cec5SDimitry Andric    constexpr bool has_value() const noexcept
3430b57cec5SDimitry Andric    {
3440b57cec5SDimitry Andric        return this->__engaged_;
3450b57cec5SDimitry Andric    }
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3480b57cec5SDimitry Andric    constexpr value_type& __get() & noexcept
3490b57cec5SDimitry Andric    {
3500b57cec5SDimitry Andric        return this->__val_;
3510b57cec5SDimitry Andric    }
3520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3530b57cec5SDimitry Andric    constexpr const value_type& __get() const& noexcept
3540b57cec5SDimitry Andric    {
3550b57cec5SDimitry Andric        return this->__val_;
3560b57cec5SDimitry Andric    }
3570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3580b57cec5SDimitry Andric    constexpr value_type&& __get() && noexcept
3590b57cec5SDimitry Andric    {
3600b57cec5SDimitry Andric        return _VSTD::move(this->__val_);
3610b57cec5SDimitry Andric    }
3620b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3630b57cec5SDimitry Andric    constexpr const value_type&& __get() const&& noexcept
3640b57cec5SDimitry Andric    {
3650b57cec5SDimitry Andric        return _VSTD::move(this->__val_);
3660b57cec5SDimitry Andric    }
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric    template <class... _Args>
3690b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
370fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct(_Args&&... __args)
3710b57cec5SDimitry Andric    {
3720b57cec5SDimitry Andric        _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage");
373fe6060f1SDimitry Andric#if _LIBCPP_STD_VER > 17
374fe6060f1SDimitry Andric        _VSTD::construct_at(_VSTD::addressof(this->__val_), _VSTD::forward<_Args>(__args)...);
375fe6060f1SDimitry Andric#else
3760b57cec5SDimitry Andric        ::new ((void*)_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Args>(__args)...);
377fe6060f1SDimitry Andric#endif
3780b57cec5SDimitry Andric        this->__engaged_ = true;
3790b57cec5SDimitry Andric    }
3800b57cec5SDimitry Andric
3810b57cec5SDimitry Andric    template <class _That>
3820b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
383fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct_from(_That&& __opt)
3840b57cec5SDimitry Andric    {
3850b57cec5SDimitry Andric        if (__opt.has_value())
3860b57cec5SDimitry Andric            __construct(_VSTD::forward<_That>(__opt).__get());
3870b57cec5SDimitry Andric    }
3880b57cec5SDimitry Andric
3890b57cec5SDimitry Andric    template <class _That>
3900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
391fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void __assign_from(_That&& __opt)
3920b57cec5SDimitry Andric    {
3930b57cec5SDimitry Andric        if (this->__engaged_ == __opt.has_value())
3940b57cec5SDimitry Andric        {
3950b57cec5SDimitry Andric            if (this->__engaged_)
3960b57cec5SDimitry Andric                this->__val_ = _VSTD::forward<_That>(__opt).__get();
3970b57cec5SDimitry Andric        }
3980b57cec5SDimitry Andric        else
3990b57cec5SDimitry Andric        {
4000b57cec5SDimitry Andric            if (this->__engaged_)
4010b57cec5SDimitry Andric                this->reset();
4020b57cec5SDimitry Andric            else
4030b57cec5SDimitry Andric                __construct(_VSTD::forward<_That>(__opt).__get());
4040b57cec5SDimitry Andric        }
4050b57cec5SDimitry Andric    }
4060b57cec5SDimitry Andric};
4070b57cec5SDimitry Andric
408*81ad6265SDimitry Andric// optional<T&> is currently required to be ill-formed. However, it may
409*81ad6265SDimitry Andric// be allowed in the future. For this reason, it has already been implemented
410*81ad6265SDimitry Andric// to ensure we can make the change in an ABI-compatible manner.
4110b57cec5SDimitry Andrictemplate <class _Tp>
4120b57cec5SDimitry Andricstruct __optional_storage_base<_Tp, true>
4130b57cec5SDimitry Andric{
4140b57cec5SDimitry Andric    using value_type = _Tp;
4150b57cec5SDimitry Andric    using __raw_type = remove_reference_t<_Tp>;
4160b57cec5SDimitry Andric    __raw_type* __value_;
4170b57cec5SDimitry Andric
4180b57cec5SDimitry Andric    template <class _Up>
4190b57cec5SDimitry Andric    static constexpr bool __can_bind_reference() {
4200b57cec5SDimitry Andric        using _RawUp = typename remove_reference<_Up>::type;
4210b57cec5SDimitry Andric        using _UpPtr = _RawUp*;
4220b57cec5SDimitry Andric        using _RawTp = typename remove_reference<_Tp>::type;
4230b57cec5SDimitry Andric        using _TpPtr = _RawTp*;
4240b57cec5SDimitry Andric        using _CheckLValueArg = integral_constant<bool,
4250b57cec5SDimitry Andric            (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value)
4260b57cec5SDimitry Andric        ||  is_same<_RawUp, reference_wrapper<_RawTp>>::value
4270b57cec5SDimitry Andric        ||  is_same<_RawUp, reference_wrapper<typename remove_const<_RawTp>::type>>::value
4280b57cec5SDimitry Andric        >;
4290b57cec5SDimitry Andric        return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value)
4300b57cec5SDimitry Andric            || (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value &&
4310b57cec5SDimitry Andric                is_convertible<_UpPtr, _TpPtr>::value);
4320b57cec5SDimitry Andric    }
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4350b57cec5SDimitry Andric    constexpr __optional_storage_base() noexcept
4360b57cec5SDimitry Andric        :  __value_(nullptr) {}
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andric    template <class _UArg>
4390b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4400b57cec5SDimitry Andric    constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg)
4410b57cec5SDimitry Andric        :  __value_(_VSTD::addressof(__uarg))
4420b57cec5SDimitry Andric    {
4430b57cec5SDimitry Andric      static_assert(__can_bind_reference<_UArg>(),
4440b57cec5SDimitry Andric        "Attempted to construct a reference element in tuple from a "
4450b57cec5SDimitry Andric        "possible temporary");
4460b57cec5SDimitry Andric    }
4470b57cec5SDimitry Andric
4480b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
449fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept { __value_ = nullptr; }
4500b57cec5SDimitry Andric
4510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4520b57cec5SDimitry Andric    constexpr bool has_value() const noexcept
4530b57cec5SDimitry Andric      { return __value_ != nullptr; }
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4560b57cec5SDimitry Andric    constexpr value_type& __get() const& noexcept
4570b57cec5SDimitry Andric      { return *__value_; }
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4600b57cec5SDimitry Andric    constexpr value_type&& __get() const&& noexcept
4610b57cec5SDimitry Andric      { return _VSTD::forward<value_type>(*__value_); }
4620b57cec5SDimitry Andric
4630b57cec5SDimitry Andric    template <class _UArg>
4640b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
465fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct(_UArg&& __val)
4660b57cec5SDimitry Andric    {
4670b57cec5SDimitry Andric        _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage");
4680b57cec5SDimitry Andric        static_assert(__can_bind_reference<_UArg>(),
4690b57cec5SDimitry Andric            "Attempted to construct a reference element in tuple from a "
4700b57cec5SDimitry Andric            "possible temporary");
4710b57cec5SDimitry Andric        __value_ = _VSTD::addressof(__val);
4720b57cec5SDimitry Andric    }
4730b57cec5SDimitry Andric
4740b57cec5SDimitry Andric    template <class _That>
4750b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
476fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct_from(_That&& __opt)
4770b57cec5SDimitry Andric    {
4780b57cec5SDimitry Andric        if (__opt.has_value())
4790b57cec5SDimitry Andric            __construct(_VSTD::forward<_That>(__opt).__get());
4800b57cec5SDimitry Andric    }
4810b57cec5SDimitry Andric
4820b57cec5SDimitry Andric    template <class _That>
4830b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
484fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void __assign_from(_That&& __opt)
4850b57cec5SDimitry Andric    {
4860b57cec5SDimitry Andric        if (has_value() == __opt.has_value())
4870b57cec5SDimitry Andric        {
4880b57cec5SDimitry Andric            if (has_value())
4890b57cec5SDimitry Andric                *__value_ = _VSTD::forward<_That>(__opt).__get();
4900b57cec5SDimitry Andric        }
4910b57cec5SDimitry Andric        else
4920b57cec5SDimitry Andric        {
4930b57cec5SDimitry Andric            if (has_value())
4940b57cec5SDimitry Andric                reset();
4950b57cec5SDimitry Andric            else
4960b57cec5SDimitry Andric                __construct(_VSTD::forward<_That>(__opt).__get());
4970b57cec5SDimitry Andric        }
4980b57cec5SDimitry Andric    }
4990b57cec5SDimitry Andric};
5000b57cec5SDimitry Andric
5010b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value>
5020b57cec5SDimitry Andricstruct __optional_copy_base : __optional_storage_base<_Tp>
5030b57cec5SDimitry Andric{
5040b57cec5SDimitry Andric    using __optional_storage_base<_Tp>::__optional_storage_base;
5050b57cec5SDimitry Andric};
5060b57cec5SDimitry Andric
5070b57cec5SDimitry Andrictemplate <class _Tp>
5080b57cec5SDimitry Andricstruct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp>
5090b57cec5SDimitry Andric{
5100b57cec5SDimitry Andric    using __optional_storage_base<_Tp>::__optional_storage_base;
5110b57cec5SDimitry Andric
5120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5130b57cec5SDimitry Andric    __optional_copy_base() = default;
5140b57cec5SDimitry Andric
5150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
516fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_copy_base(const __optional_copy_base& __opt)
5170b57cec5SDimitry Andric    {
5180b57cec5SDimitry Andric        this->__construct_from(__opt);
5190b57cec5SDimitry Andric    }
5200b57cec5SDimitry Andric
5210b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5220b57cec5SDimitry Andric    __optional_copy_base(__optional_copy_base&&) = default;
5230b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5240b57cec5SDimitry Andric    __optional_copy_base& operator=(const __optional_copy_base&) = default;
5250b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5260b57cec5SDimitry Andric    __optional_copy_base& operator=(__optional_copy_base&&) = default;
5270b57cec5SDimitry Andric};
5280b57cec5SDimitry Andric
5290b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_move_constructible<_Tp>::value>
5300b57cec5SDimitry Andricstruct __optional_move_base : __optional_copy_base<_Tp>
5310b57cec5SDimitry Andric{
5320b57cec5SDimitry Andric    using __optional_copy_base<_Tp>::__optional_copy_base;
5330b57cec5SDimitry Andric};
5340b57cec5SDimitry Andric
5350b57cec5SDimitry Andrictemplate <class _Tp>
5360b57cec5SDimitry Andricstruct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp>
5370b57cec5SDimitry Andric{
5380b57cec5SDimitry Andric    using value_type = _Tp;
5390b57cec5SDimitry Andric    using __optional_copy_base<_Tp>::__optional_copy_base;
5400b57cec5SDimitry Andric
5410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5420b57cec5SDimitry Andric    __optional_move_base() = default;
5430b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5440b57cec5SDimitry Andric    __optional_move_base(const __optional_move_base&) = default;
5450b57cec5SDimitry Andric
5460b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
547fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_move_base(__optional_move_base&& __opt)
5480b57cec5SDimitry Andric        noexcept(is_nothrow_move_constructible_v<value_type>)
5490b57cec5SDimitry Andric    {
5500b57cec5SDimitry Andric        this->__construct_from(_VSTD::move(__opt));
5510b57cec5SDimitry Andric    }
5520b57cec5SDimitry Andric
5530b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5540b57cec5SDimitry Andric    __optional_move_base& operator=(const __optional_move_base&) = default;
5550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5560b57cec5SDimitry Andric    __optional_move_base& operator=(__optional_move_base&&) = default;
5570b57cec5SDimitry Andric};
5580b57cec5SDimitry Andric
5590b57cec5SDimitry Andrictemplate <class _Tp, bool =
5600b57cec5SDimitry Andric    is_trivially_destructible<_Tp>::value &&
5610b57cec5SDimitry Andric    is_trivially_copy_constructible<_Tp>::value &&
5620b57cec5SDimitry Andric    is_trivially_copy_assignable<_Tp>::value>
5630b57cec5SDimitry Andricstruct __optional_copy_assign_base : __optional_move_base<_Tp>
5640b57cec5SDimitry Andric{
5650b57cec5SDimitry Andric    using __optional_move_base<_Tp>::__optional_move_base;
5660b57cec5SDimitry Andric};
5670b57cec5SDimitry Andric
5680b57cec5SDimitry Andrictemplate <class _Tp>
5690b57cec5SDimitry Andricstruct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp>
5700b57cec5SDimitry Andric{
5710b57cec5SDimitry Andric    using __optional_move_base<_Tp>::__optional_move_base;
5720b57cec5SDimitry Andric
5730b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5740b57cec5SDimitry Andric    __optional_copy_assign_base() = default;
5750b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5760b57cec5SDimitry Andric    __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
5770b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5780b57cec5SDimitry Andric    __optional_copy_assign_base(__optional_copy_assign_base&&) = default;
5790b57cec5SDimitry Andric
5800b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
581fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_copy_assign_base& operator=(const __optional_copy_assign_base& __opt)
5820b57cec5SDimitry Andric    {
5830b57cec5SDimitry Andric        this->__assign_from(__opt);
5840b57cec5SDimitry Andric        return *this;
5850b57cec5SDimitry Andric    }
5860b57cec5SDimitry Andric
5870b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5880b57cec5SDimitry Andric    __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
5890b57cec5SDimitry Andric};
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andrictemplate <class _Tp, bool =
5920b57cec5SDimitry Andric    is_trivially_destructible<_Tp>::value &&
5930b57cec5SDimitry Andric    is_trivially_move_constructible<_Tp>::value &&
5940b57cec5SDimitry Andric    is_trivially_move_assignable<_Tp>::value>
5950b57cec5SDimitry Andricstruct __optional_move_assign_base : __optional_copy_assign_base<_Tp>
5960b57cec5SDimitry Andric{
5970b57cec5SDimitry Andric    using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
5980b57cec5SDimitry Andric};
5990b57cec5SDimitry Andric
6000b57cec5SDimitry Andrictemplate <class _Tp>
6010b57cec5SDimitry Andricstruct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp>
6020b57cec5SDimitry Andric{
6030b57cec5SDimitry Andric    using value_type = _Tp;
6040b57cec5SDimitry Andric    using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
6050b57cec5SDimitry Andric
6060b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6070b57cec5SDimitry Andric    __optional_move_assign_base() = default;
6080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6090b57cec5SDimitry Andric    __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;
6100b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6110b57cec5SDimitry Andric    __optional_move_assign_base(__optional_move_assign_base&&) = default;
6120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
6130b57cec5SDimitry Andric    __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
6140b57cec5SDimitry Andric
6150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
616fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_move_assign_base& operator=(__optional_move_assign_base&& __opt)
6170b57cec5SDimitry Andric        noexcept(is_nothrow_move_assignable_v<value_type> &&
6180b57cec5SDimitry Andric                 is_nothrow_move_constructible_v<value_type>)
6190b57cec5SDimitry Andric    {
6200b57cec5SDimitry Andric        this->__assign_from(_VSTD::move(__opt));
6210b57cec5SDimitry Andric        return *this;
6220b57cec5SDimitry Andric    }
6230b57cec5SDimitry Andric};
6240b57cec5SDimitry Andric
6250b57cec5SDimitry Andrictemplate <class _Tp>
6260b57cec5SDimitry Andricusing __optional_sfinae_ctor_base_t = __sfinae_ctor_base<
6270b57cec5SDimitry Andric    is_copy_constructible<_Tp>::value,
6280b57cec5SDimitry Andric    is_move_constructible<_Tp>::value
6290b57cec5SDimitry Andric>;
6300b57cec5SDimitry Andric
6310b57cec5SDimitry Andrictemplate <class _Tp>
6320b57cec5SDimitry Andricusing __optional_sfinae_assign_base_t = __sfinae_assign_base<
6330b57cec5SDimitry Andric    (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),
6340b57cec5SDimitry Andric    (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value)
6350b57cec5SDimitry Andric>;
6360b57cec5SDimitry Andric
6370b57cec5SDimitry Andrictemplate<class _Tp>
6380eae32dcSDimitry Andricclass optional;
6390eae32dcSDimitry Andrictemplate <class _Tp>
6400eae32dcSDimitry Andricstruct __is_std_optional : false_type {};
6410eae32dcSDimitry Andrictemplate <class _Tp> struct __is_std_optional<optional<_Tp>> : true_type {};
6420eae32dcSDimitry Andric
6430eae32dcSDimitry Andrictemplate <class _Tp>
6440b57cec5SDimitry Andricclass optional
6450b57cec5SDimitry Andric    : private __optional_move_assign_base<_Tp>
6460b57cec5SDimitry Andric    , private __optional_sfinae_ctor_base_t<_Tp>
6470b57cec5SDimitry Andric    , private __optional_sfinae_assign_base_t<_Tp>
6480b57cec5SDimitry Andric{
6490b57cec5SDimitry Andric    using __base = __optional_move_assign_base<_Tp>;
6500b57cec5SDimitry Andricpublic:
6510b57cec5SDimitry Andric    using value_type = _Tp;
6520b57cec5SDimitry Andric
6530b57cec5SDimitry Andricprivate:
6540b57cec5SDimitry Andric     // Disable the reference extension using this static assert.
6550b57cec5SDimitry Andric    static_assert(!is_same_v<__uncvref_t<value_type>, in_place_t>,
6560b57cec5SDimitry Andric        "instantiation of optional with in_place_t is ill-formed");
6570b57cec5SDimitry Andric    static_assert(!is_same_v<__uncvref_t<value_type>, nullopt_t>,
6580b57cec5SDimitry Andric        "instantiation of optional with nullopt_t is ill-formed");
6590b57cec5SDimitry Andric    static_assert(!is_reference_v<value_type>,
6600b57cec5SDimitry Andric        "instantiation of optional with a reference type is ill-formed");
6610b57cec5SDimitry Andric    static_assert(is_destructible_v<value_type>,
6620b57cec5SDimitry Andric        "instantiation of optional with a non-destructible type is ill-formed");
6630b57cec5SDimitry Andric    static_assert(!is_array_v<value_type>,
6640b57cec5SDimitry Andric        "instantiation of optional with an array type is ill-formed");
6650b57cec5SDimitry Andric
6660b57cec5SDimitry Andric    // LWG2756: conditionally explicit conversion from _Up
6670b57cec5SDimitry Andric    struct _CheckOptionalArgsConstructor {
6680b57cec5SDimitry Andric      template <class _Up>
6690b57cec5SDimitry Andric      static constexpr bool __enable_implicit() {
6700b57cec5SDimitry Andric          return is_constructible_v<_Tp, _Up&&> &&
6710b57cec5SDimitry Andric                 is_convertible_v<_Up&&, _Tp>;
6720b57cec5SDimitry Andric      }
6730b57cec5SDimitry Andric
6740b57cec5SDimitry Andric      template <class _Up>
6750b57cec5SDimitry Andric      static constexpr bool __enable_explicit() {
6760b57cec5SDimitry Andric          return is_constructible_v<_Tp, _Up&&> &&
6770b57cec5SDimitry Andric                 !is_convertible_v<_Up&&, _Tp>;
6780b57cec5SDimitry Andric      }
6790b57cec5SDimitry Andric    };
6800b57cec5SDimitry Andric    template <class _Up>
6810b57cec5SDimitry Andric    using _CheckOptionalArgsCtor = _If<
6820b57cec5SDimitry Andric        _IsNotSame<__uncvref_t<_Up>, in_place_t>::value &&
6830b57cec5SDimitry Andric        _IsNotSame<__uncvref_t<_Up>, optional>::value,
6840b57cec5SDimitry Andric        _CheckOptionalArgsConstructor,
6850b57cec5SDimitry Andric        __check_tuple_constructor_fail
6860b57cec5SDimitry Andric    >;
6870b57cec5SDimitry Andric    template <class _QualUp>
6880b57cec5SDimitry Andric    struct _CheckOptionalLikeConstructor {
6890b57cec5SDimitry Andric      template <class _Up, class _Opt = optional<_Up>>
6900b57cec5SDimitry Andric      using __check_constructible_from_opt = _Or<
6910b57cec5SDimitry Andric          is_constructible<_Tp, _Opt&>,
6920b57cec5SDimitry Andric          is_constructible<_Tp, _Opt const&>,
6930b57cec5SDimitry Andric          is_constructible<_Tp, _Opt&&>,
6940b57cec5SDimitry Andric          is_constructible<_Tp, _Opt const&&>,
6950b57cec5SDimitry Andric          is_convertible<_Opt&, _Tp>,
6960b57cec5SDimitry Andric          is_convertible<_Opt const&, _Tp>,
6970b57cec5SDimitry Andric          is_convertible<_Opt&&, _Tp>,
6980b57cec5SDimitry Andric          is_convertible<_Opt const&&, _Tp>
6990b57cec5SDimitry Andric      >;
7000b57cec5SDimitry Andric      template <class _Up, class _Opt = optional<_Up>>
7010b57cec5SDimitry Andric      using __check_assignable_from_opt = _Or<
7020b57cec5SDimitry Andric          is_assignable<_Tp&, _Opt&>,
7030b57cec5SDimitry Andric          is_assignable<_Tp&, _Opt const&>,
7040b57cec5SDimitry Andric          is_assignable<_Tp&, _Opt&&>,
7050b57cec5SDimitry Andric          is_assignable<_Tp&, _Opt const&&>
7060b57cec5SDimitry Andric      >;
7070b57cec5SDimitry Andric      template <class _Up, class _QUp = _QualUp>
7080b57cec5SDimitry Andric      static constexpr bool __enable_implicit() {
7090b57cec5SDimitry Andric          return is_convertible<_QUp, _Tp>::value &&
7100b57cec5SDimitry Andric              !__check_constructible_from_opt<_Up>::value;
7110b57cec5SDimitry Andric      }
7120b57cec5SDimitry Andric      template <class _Up, class _QUp = _QualUp>
7130b57cec5SDimitry Andric      static constexpr bool __enable_explicit() {
7140b57cec5SDimitry Andric          return !is_convertible<_QUp, _Tp>::value &&
7150b57cec5SDimitry Andric              !__check_constructible_from_opt<_Up>::value;
7160b57cec5SDimitry Andric      }
7170b57cec5SDimitry Andric      template <class _Up, class _QUp = _QualUp>
7180b57cec5SDimitry Andric      static constexpr bool __enable_assign() {
719e8d8bef9SDimitry Andric          // Construction and assignability of _QUp to _Tp has already been
7200b57cec5SDimitry Andric          // checked.
7210b57cec5SDimitry Andric          return !__check_constructible_from_opt<_Up>::value &&
7220b57cec5SDimitry Andric              !__check_assignable_from_opt<_Up>::value;
7230b57cec5SDimitry Andric      }
7240b57cec5SDimitry Andric    };
7250b57cec5SDimitry Andric
7260b57cec5SDimitry Andric    template <class _Up, class _QualUp>
7270b57cec5SDimitry Andric    using _CheckOptionalLikeCtor = _If<
7280b57cec5SDimitry Andric      _And<
7290b57cec5SDimitry Andric         _IsNotSame<_Up, _Tp>,
7300b57cec5SDimitry Andric          is_constructible<_Tp, _QualUp>
7310b57cec5SDimitry Andric      >::value,
7320b57cec5SDimitry Andric      _CheckOptionalLikeConstructor<_QualUp>,
7330b57cec5SDimitry Andric      __check_tuple_constructor_fail
7340b57cec5SDimitry Andric    >;
7350b57cec5SDimitry Andric    template <class _Up, class _QualUp>
7360b57cec5SDimitry Andric    using _CheckOptionalLikeAssign = _If<
7370b57cec5SDimitry Andric      _And<
7380b57cec5SDimitry Andric          _IsNotSame<_Up, _Tp>,
7390b57cec5SDimitry Andric          is_constructible<_Tp, _QualUp>,
7400b57cec5SDimitry Andric          is_assignable<_Tp&, _QualUp>
7410b57cec5SDimitry Andric      >::value,
7420b57cec5SDimitry Andric      _CheckOptionalLikeConstructor<_QualUp>,
7430b57cec5SDimitry Andric      __check_tuple_constructor_fail
7440b57cec5SDimitry Andric    >;
7450eae32dcSDimitry Andric
7460b57cec5SDimitry Andricpublic:
7470b57cec5SDimitry Andric
7480b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
7490b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
7500b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default;
7510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
7520b57cec5SDimitry Andric
753349cc55cSDimitry Andric    template <class _InPlaceT, class... _Args, class = enable_if_t<
7540b57cec5SDimitry Andric          _And<
7550b57cec5SDimitry Andric              _IsSame<_InPlaceT, in_place_t>,
7560b57cec5SDimitry Andric              is_constructible<value_type, _Args...>
7570b57cec5SDimitry Andric            >::value
7580b57cec5SDimitry Andric        >
7590b57cec5SDimitry Andric    >
7600b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7610b57cec5SDimitry Andric    constexpr explicit optional(_InPlaceT, _Args&&... __args)
7620b57cec5SDimitry Andric        : __base(in_place, _VSTD::forward<_Args>(__args)...) {}
7630b57cec5SDimitry Andric
764349cc55cSDimitry Andric    template <class _Up, class... _Args, class = enable_if_t<
7650b57cec5SDimitry Andric        is_constructible_v<value_type, initializer_list<_Up>&, _Args...>>
7660b57cec5SDimitry Andric    >
7670b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7680b57cec5SDimitry Andric    constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
7690b57cec5SDimitry Andric        : __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {}
7700b57cec5SDimitry Andric
771349cc55cSDimitry Andric    template <class _Up = value_type, enable_if_t<
7720b57cec5SDimitry Andric        _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>()
7730b57cec5SDimitry Andric    , int> = 0>
7740b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7750b57cec5SDimitry Andric    constexpr optional(_Up&& __v)
7760b57cec5SDimitry Andric        : __base(in_place, _VSTD::forward<_Up>(__v)) {}
7770b57cec5SDimitry Andric
778349cc55cSDimitry Andric    template <class _Up, enable_if_t<
7790b57cec5SDimitry Andric        _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>()
7800b57cec5SDimitry Andric    , int> = 0>
7810b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
7820b57cec5SDimitry Andric    constexpr explicit optional(_Up&& __v)
7830b57cec5SDimitry Andric        : __base(in_place, _VSTD::forward<_Up>(__v)) {}
7840b57cec5SDimitry Andric
7850b57cec5SDimitry Andric    // LWG2756: conditionally explicit conversion from const optional<_Up>&
786349cc55cSDimitry Andric    template <class _Up, enable_if_t<
7870b57cec5SDimitry Andric        _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>()
7880b57cec5SDimitry Andric    , int> = 0>
7890b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
790fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(const optional<_Up>& __v)
7910b57cec5SDimitry Andric    {
7920b57cec5SDimitry Andric        this->__construct_from(__v);
7930b57cec5SDimitry Andric    }
794349cc55cSDimitry Andric    template <class _Up, enable_if_t<
7950b57cec5SDimitry Andric        _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>()
7960b57cec5SDimitry Andric    , int> = 0>
7970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
798fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(const optional<_Up>& __v)
7990b57cec5SDimitry Andric    {
8000b57cec5SDimitry Andric        this->__construct_from(__v);
8010b57cec5SDimitry Andric    }
8020b57cec5SDimitry Andric
8030b57cec5SDimitry Andric    // LWG2756: conditionally explicit conversion from optional<_Up>&&
804349cc55cSDimitry Andric    template <class _Up, enable_if_t<
8050b57cec5SDimitry Andric        _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_implicit<_Up>()
8060b57cec5SDimitry Andric    , int> = 0>
8070b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
808fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(optional<_Up>&& __v)
8090b57cec5SDimitry Andric    {
8100b57cec5SDimitry Andric        this->__construct_from(_VSTD::move(__v));
8110b57cec5SDimitry Andric    }
812349cc55cSDimitry Andric    template <class _Up, enable_if_t<
8130b57cec5SDimitry Andric        _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_explicit<_Up>()
8140b57cec5SDimitry Andric    , int> = 0>
8150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
816fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(optional<_Up>&& __v)
8170b57cec5SDimitry Andric    {
8180b57cec5SDimitry Andric        this->__construct_from(_VSTD::move(__v));
8190b57cec5SDimitry Andric    }
8200b57cec5SDimitry Andric
8210eae32dcSDimitry Andric#if _LIBCPP_STD_VER > 20
8220eae32dcSDimitry Andric  template<class _Fp, class... _Args>
8230eae32dcSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8240eae32dcSDimitry Andric  constexpr explicit optional(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
8250eae32dcSDimitry Andric      : __base(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...) {
8260eae32dcSDimitry Andric  }
8270eae32dcSDimitry Andric#endif
8280eae32dcSDimitry Andric
8290b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
830fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& operator=(nullopt_t) noexcept
8310b57cec5SDimitry Andric    {
8320b57cec5SDimitry Andric        reset();
8330b57cec5SDimitry Andric        return *this;
8340b57cec5SDimitry Andric    }
8350b57cec5SDimitry Andric
8360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY optional& operator=(const optional&) = default;
8370b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY optional& operator=(optional&&) = default;
8380b57cec5SDimitry Andric
8390b57cec5SDimitry Andric    // LWG2756
8400b57cec5SDimitry Andric    template <class _Up = value_type,
841349cc55cSDimitry Andric              class = enable_if_t<
8420b57cec5SDimitry Andric                      _And<
8430b57cec5SDimitry Andric                          _IsNotSame<__uncvref_t<_Up>, optional>,
8440b57cec5SDimitry Andric                          _Or<
8450b57cec5SDimitry Andric                              _IsNotSame<__uncvref_t<_Up>, value_type>,
8460b57cec5SDimitry Andric                              _Not<is_scalar<value_type>>
8470b57cec5SDimitry Andric                          >,
8480b57cec5SDimitry Andric                          is_constructible<value_type, _Up>,
8490b57cec5SDimitry Andric                          is_assignable<value_type&, _Up>
8500b57cec5SDimitry Andric                      >::value>
8510b57cec5SDimitry Andric             >
8520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
853fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional&
8540b57cec5SDimitry Andric    operator=(_Up&& __v)
8550b57cec5SDimitry Andric    {
8560b57cec5SDimitry Andric        if (this->has_value())
8570b57cec5SDimitry Andric            this->__get() = _VSTD::forward<_Up>(__v);
8580b57cec5SDimitry Andric        else
8590b57cec5SDimitry Andric            this->__construct(_VSTD::forward<_Up>(__v));
8600b57cec5SDimitry Andric        return *this;
8610b57cec5SDimitry Andric    }
8620b57cec5SDimitry Andric
8630b57cec5SDimitry Andric    // LWG2756
864349cc55cSDimitry Andric    template <class _Up, enable_if_t<
8650b57cec5SDimitry Andric        _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>()
8660b57cec5SDimitry Andric    , int> = 0>
8670b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
868fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional&
8690b57cec5SDimitry Andric    operator=(const optional<_Up>& __v)
8700b57cec5SDimitry Andric    {
8710b57cec5SDimitry Andric        this->__assign_from(__v);
8720b57cec5SDimitry Andric        return *this;
8730b57cec5SDimitry Andric    }
8740b57cec5SDimitry Andric
8750b57cec5SDimitry Andric    // LWG2756
876349cc55cSDimitry Andric    template <class _Up, enable_if_t<
8770b57cec5SDimitry Andric        _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_assign<_Up>()
8780b57cec5SDimitry Andric    , int> = 0>
8790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
880fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional&
8810b57cec5SDimitry Andric    operator=(optional<_Up>&& __v)
8820b57cec5SDimitry Andric    {
8830b57cec5SDimitry Andric        this->__assign_from(_VSTD::move(__v));
8840b57cec5SDimitry Andric        return *this;
8850b57cec5SDimitry Andric    }
8860b57cec5SDimitry Andric
8870b57cec5SDimitry Andric    template <class... _Args,
888349cc55cSDimitry Andric              class = enable_if_t
8890b57cec5SDimitry Andric                      <
8900b57cec5SDimitry Andric                          is_constructible_v<value_type, _Args...>
8910b57cec5SDimitry Andric                      >
8920b57cec5SDimitry Andric             >
8930b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
894fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 _Tp &
8950b57cec5SDimitry Andric    emplace(_Args&&... __args)
8960b57cec5SDimitry Andric    {
8970b57cec5SDimitry Andric        reset();
8980b57cec5SDimitry Andric        this->__construct(_VSTD::forward<_Args>(__args)...);
8990b57cec5SDimitry Andric        return this->__get();
9000b57cec5SDimitry Andric    }
9010b57cec5SDimitry Andric
9020b57cec5SDimitry Andric    template <class _Up, class... _Args,
903349cc55cSDimitry Andric              class = enable_if_t
9040b57cec5SDimitry Andric                      <
9050b57cec5SDimitry Andric                          is_constructible_v<value_type, initializer_list<_Up>&, _Args...>
9060b57cec5SDimitry Andric                      >
9070b57cec5SDimitry Andric             >
9080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
909fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 _Tp &
9100b57cec5SDimitry Andric    emplace(initializer_list<_Up> __il, _Args&&... __args)
9110b57cec5SDimitry Andric    {
9120b57cec5SDimitry Andric        reset();
9130b57cec5SDimitry Andric        this->__construct(__il, _VSTD::forward<_Args>(__args)...);
9140b57cec5SDimitry Andric        return this->__get();
9150b57cec5SDimitry Andric    }
9160b57cec5SDimitry Andric
9170b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
918fe6060f1SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void swap(optional& __opt)
9190b57cec5SDimitry Andric        noexcept(is_nothrow_move_constructible_v<value_type> &&
9200b57cec5SDimitry Andric                 is_nothrow_swappable_v<value_type>)
9210b57cec5SDimitry Andric    {
9220b57cec5SDimitry Andric        if (this->has_value() == __opt.has_value())
9230b57cec5SDimitry Andric        {
9240b57cec5SDimitry Andric            using _VSTD::swap;
9250b57cec5SDimitry Andric            if (this->has_value())
9260b57cec5SDimitry Andric                swap(this->__get(), __opt.__get());
9270b57cec5SDimitry Andric        }
9280b57cec5SDimitry Andric        else
9290b57cec5SDimitry Andric        {
9300b57cec5SDimitry Andric            if (this->has_value())
9310b57cec5SDimitry Andric            {
9320b57cec5SDimitry Andric                __opt.__construct(_VSTD::move(this->__get()));
9330b57cec5SDimitry Andric                reset();
9340b57cec5SDimitry Andric            }
9350b57cec5SDimitry Andric            else
9360b57cec5SDimitry Andric            {
9370b57cec5SDimitry Andric                this->__construct(_VSTD::move(__opt.__get()));
9380b57cec5SDimitry Andric                __opt.reset();
9390b57cec5SDimitry Andric            }
9400b57cec5SDimitry Andric        }
9410b57cec5SDimitry Andric    }
9420b57cec5SDimitry Andric
9430b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
9440b57cec5SDimitry Andric    constexpr
9450b57cec5SDimitry Andric    add_pointer_t<value_type const>
9460b57cec5SDimitry Andric    operator->() const
9470b57cec5SDimitry Andric    {
948fe6060f1SDimitry Andric        _LIBCPP_ASSERT(this->has_value(), "optional operator-> called on a disengaged value");
9490b57cec5SDimitry Andric        return _VSTD::addressof(this->__get());
9500b57cec5SDimitry Andric    }
9510b57cec5SDimitry Andric
9520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
9530b57cec5SDimitry Andric    constexpr
9540b57cec5SDimitry Andric    add_pointer_t<value_type>
9550b57cec5SDimitry Andric    operator->()
9560b57cec5SDimitry Andric    {
957fe6060f1SDimitry Andric        _LIBCPP_ASSERT(this->has_value(), "optional operator-> called on a disengaged value");
9580b57cec5SDimitry Andric        return _VSTD::addressof(this->__get());
9590b57cec5SDimitry Andric    }
9600b57cec5SDimitry Andric
9610b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
9620b57cec5SDimitry Andric    constexpr
9630b57cec5SDimitry Andric    const value_type&
964fe6060f1SDimitry Andric    operator*() const& noexcept
9650b57cec5SDimitry Andric    {
966fe6060f1SDimitry Andric        _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value");
9670b57cec5SDimitry Andric        return this->__get();
9680b57cec5SDimitry Andric    }
9690b57cec5SDimitry Andric
9700b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
9710b57cec5SDimitry Andric    constexpr
9720b57cec5SDimitry Andric    value_type&
973fe6060f1SDimitry Andric    operator*() & noexcept
9740b57cec5SDimitry Andric    {
975fe6060f1SDimitry Andric        _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value");
9760b57cec5SDimitry Andric        return this->__get();
9770b57cec5SDimitry Andric    }
9780b57cec5SDimitry Andric
9790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
9800b57cec5SDimitry Andric    constexpr
9810b57cec5SDimitry Andric    value_type&&
982fe6060f1SDimitry Andric    operator*() && noexcept
9830b57cec5SDimitry Andric    {
984fe6060f1SDimitry Andric        _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value");
9850b57cec5SDimitry Andric        return _VSTD::move(this->__get());
9860b57cec5SDimitry Andric    }
9870b57cec5SDimitry Andric
9880b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
9890b57cec5SDimitry Andric    constexpr
9900b57cec5SDimitry Andric    const value_type&&
991fe6060f1SDimitry Andric    operator*() const&& noexcept
9920b57cec5SDimitry Andric    {
993fe6060f1SDimitry Andric        _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value");
9940b57cec5SDimitry Andric        return _VSTD::move(this->__get());
9950b57cec5SDimitry Andric    }
9960b57cec5SDimitry Andric
9970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
9980b57cec5SDimitry Andric    constexpr explicit operator bool() const noexcept { return has_value(); }
9990b57cec5SDimitry Andric
10000b57cec5SDimitry Andric    using __base::has_value;
10010b57cec5SDimitry Andric    using __base::__get;
10020b57cec5SDimitry Andric
10030b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10040b57cec5SDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
10050b57cec5SDimitry Andric    constexpr value_type const& value() const&
10060b57cec5SDimitry Andric    {
10070b57cec5SDimitry Andric        if (!this->has_value())
10080b57cec5SDimitry Andric            __throw_bad_optional_access();
10090b57cec5SDimitry Andric        return this->__get();
10100b57cec5SDimitry Andric    }
10110b57cec5SDimitry Andric
10120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10130b57cec5SDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
10140b57cec5SDimitry Andric    constexpr value_type& value() &
10150b57cec5SDimitry Andric    {
10160b57cec5SDimitry Andric        if (!this->has_value())
10170b57cec5SDimitry Andric            __throw_bad_optional_access();
10180b57cec5SDimitry Andric        return this->__get();
10190b57cec5SDimitry Andric    }
10200b57cec5SDimitry Andric
10210b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10220b57cec5SDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
10230b57cec5SDimitry Andric    constexpr value_type&& value() &&
10240b57cec5SDimitry Andric    {
10250b57cec5SDimitry Andric        if (!this->has_value())
10260b57cec5SDimitry Andric            __throw_bad_optional_access();
10270b57cec5SDimitry Andric        return _VSTD::move(this->__get());
10280b57cec5SDimitry Andric    }
10290b57cec5SDimitry Andric
10300b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10310b57cec5SDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
10320b57cec5SDimitry Andric    constexpr value_type const&& value() const&&
10330b57cec5SDimitry Andric    {
10340b57cec5SDimitry Andric        if (!this->has_value())
10350b57cec5SDimitry Andric            __throw_bad_optional_access();
10360b57cec5SDimitry Andric        return _VSTD::move(this->__get());
10370b57cec5SDimitry Andric    }
10380b57cec5SDimitry Andric
10390b57cec5SDimitry Andric    template <class _Up>
10400b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10410b57cec5SDimitry Andric    constexpr value_type value_or(_Up&& __v) const&
10420b57cec5SDimitry Andric    {
10430b57cec5SDimitry Andric        static_assert(is_copy_constructible_v<value_type>,
10440b57cec5SDimitry Andric                      "optional<T>::value_or: T must be copy constructible");
10450b57cec5SDimitry Andric        static_assert(is_convertible_v<_Up, value_type>,
10460b57cec5SDimitry Andric                      "optional<T>::value_or: U must be convertible to T");
10470b57cec5SDimitry Andric        return this->has_value() ? this->__get() :
10480b57cec5SDimitry Andric                                  static_cast<value_type>(_VSTD::forward<_Up>(__v));
10490b57cec5SDimitry Andric    }
10500b57cec5SDimitry Andric
10510b57cec5SDimitry Andric    template <class _Up>
10520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10530b57cec5SDimitry Andric    constexpr value_type value_or(_Up&& __v) &&
10540b57cec5SDimitry Andric    {
10550b57cec5SDimitry Andric        static_assert(is_move_constructible_v<value_type>,
10560b57cec5SDimitry Andric                      "optional<T>::value_or: T must be move constructible");
10570b57cec5SDimitry Andric        static_assert(is_convertible_v<_Up, value_type>,
10580b57cec5SDimitry Andric                      "optional<T>::value_or: U must be convertible to T");
10590b57cec5SDimitry Andric        return this->has_value() ? _VSTD::move(this->__get()) :
10600b57cec5SDimitry Andric                                  static_cast<value_type>(_VSTD::forward<_Up>(__v));
10610b57cec5SDimitry Andric    }
10620b57cec5SDimitry Andric
10630eae32dcSDimitry Andric#if _LIBCPP_STD_VER > 20
10640eae32dcSDimitry Andric  template<class _Func>
1065*81ad6265SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
10660eae32dcSDimitry Andric  constexpr auto and_then(_Func&& __f) & {
10670eae32dcSDimitry Andric    using _Up = invoke_result_t<_Func, value_type&>;
10680eae32dcSDimitry Andric    static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10690eae32dcSDimitry Andric                  "Result of f(value()) must be a specialization of std::optional");
10700eae32dcSDimitry Andric    if (*this)
10710eae32dcSDimitry Andric      return _VSTD::invoke(_VSTD::forward<_Func>(__f), value());
10720eae32dcSDimitry Andric    return remove_cvref_t<_Up>();
10730eae32dcSDimitry Andric  }
10740eae32dcSDimitry Andric
10750eae32dcSDimitry Andric  template<class _Func>
1076*81ad6265SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
10770eae32dcSDimitry Andric  constexpr auto and_then(_Func&& __f) const& {
10780eae32dcSDimitry Andric    using _Up = invoke_result_t<_Func, const value_type&>;
10790eae32dcSDimitry Andric    static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10800eae32dcSDimitry Andric                  "Result of f(value()) must be a specialization of std::optional");
10810eae32dcSDimitry Andric    if (*this)
10820eae32dcSDimitry Andric      return _VSTD::invoke(_VSTD::forward<_Func>(__f), value());
10830eae32dcSDimitry Andric    return remove_cvref_t<_Up>();
10840eae32dcSDimitry Andric  }
10850eae32dcSDimitry Andric
10860eae32dcSDimitry Andric  template<class _Func>
1087*81ad6265SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
10880eae32dcSDimitry Andric  constexpr auto and_then(_Func&& __f) && {
10890eae32dcSDimitry Andric    using _Up = invoke_result_t<_Func, value_type&&>;
10900eae32dcSDimitry Andric    static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10910eae32dcSDimitry Andric                  "Result of f(std::move(value())) must be a specialization of std::optional");
10920eae32dcSDimitry Andric    if (*this)
10930eae32dcSDimitry Andric      return _VSTD::invoke(_VSTD::forward<_Func>(__f), _VSTD::move(value()));
10940eae32dcSDimitry Andric    return remove_cvref_t<_Up>();
10950eae32dcSDimitry Andric  }
10960eae32dcSDimitry Andric
10970eae32dcSDimitry Andric  template<class _Func>
10980eae32dcSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
10990eae32dcSDimitry Andric  constexpr auto and_then(_Func&& __f) const&& {
11000eae32dcSDimitry Andric    using _Up = invoke_result_t<_Func, const value_type&&>;
11010eae32dcSDimitry Andric    static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
11020eae32dcSDimitry Andric                  "Result of f(std::move(value())) must be a specialization of std::optional");
11030eae32dcSDimitry Andric    if (*this)
11040eae32dcSDimitry Andric      return _VSTD::invoke(_VSTD::forward<_Func>(__f), _VSTD::move(value()));
11050eae32dcSDimitry Andric    return remove_cvref_t<_Up>();
11060eae32dcSDimitry Andric  }
11070eae32dcSDimitry Andric
11080eae32dcSDimitry Andric  template<class _Func>
1109*81ad6265SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
11100eae32dcSDimitry Andric  constexpr auto transform(_Func&& __f) & {
11110eae32dcSDimitry Andric    using _Up = remove_cv_t<invoke_result_t<_Func, value_type&>>;
11120eae32dcSDimitry Andric    static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
11130eae32dcSDimitry Andric    static_assert(!is_same_v<_Up, in_place_t>,
11140eae32dcSDimitry Andric                  "Result of f(value()) should not be std::in_place_t");
11150eae32dcSDimitry Andric    static_assert(!is_same_v<_Up, nullopt_t>,
11160eae32dcSDimitry Andric                  "Result of f(value()) should not be std::nullopt_t");
11170eae32dcSDimitry Andric    static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
11180eae32dcSDimitry Andric    if (*this)
11190eae32dcSDimitry Andric      return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), value());
11200eae32dcSDimitry Andric    return optional<_Up>();
11210eae32dcSDimitry Andric  }
11220eae32dcSDimitry Andric
11230eae32dcSDimitry Andric  template<class _Func>
1124*81ad6265SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
11250eae32dcSDimitry Andric  constexpr auto transform(_Func&& __f) const& {
11260eae32dcSDimitry Andric    using _Up = remove_cv_t<invoke_result_t<_Func, const value_type&>>;
11270eae32dcSDimitry Andric    static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
11280eae32dcSDimitry Andric    static_assert(!is_same_v<_Up, in_place_t>,
11290eae32dcSDimitry Andric                  "Result of f(value()) should not be std::in_place_t");
11300eae32dcSDimitry Andric    static_assert(!is_same_v<_Up, nullopt_t>,
11310eae32dcSDimitry Andric                  "Result of f(value()) should not be std::nullopt_t");
11320eae32dcSDimitry Andric    static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
11330eae32dcSDimitry Andric    if (*this)
11340eae32dcSDimitry Andric      return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), value());
11350eae32dcSDimitry Andric    return optional<_Up>();
11360eae32dcSDimitry Andric  }
11370eae32dcSDimitry Andric
11380eae32dcSDimitry Andric  template<class _Func>
1139*81ad6265SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
11400eae32dcSDimitry Andric  constexpr auto transform(_Func&& __f) && {
11410eae32dcSDimitry Andric    using _Up = remove_cv_t<invoke_result_t<_Func, value_type&&>>;
11420eae32dcSDimitry Andric    static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
11430eae32dcSDimitry Andric    static_assert(!is_same_v<_Up, in_place_t>,
11440eae32dcSDimitry Andric                  "Result of f(std::move(value())) should not be std::in_place_t");
11450eae32dcSDimitry Andric    static_assert(!is_same_v<_Up, nullopt_t>,
11460eae32dcSDimitry Andric                  "Result of f(std::move(value())) should not be std::nullopt_t");
11470eae32dcSDimitry Andric    static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
11480eae32dcSDimitry Andric    if (*this)
11490eae32dcSDimitry Andric      return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), _VSTD::move(value()));
11500eae32dcSDimitry Andric    return optional<_Up>();
11510eae32dcSDimitry Andric  }
11520eae32dcSDimitry Andric
11530eae32dcSDimitry Andric  template<class _Func>
1154*81ad6265SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
11550eae32dcSDimitry Andric  constexpr auto transform(_Func&& __f) const&& {
11560eae32dcSDimitry Andric    using _Up = remove_cvref_t<invoke_result_t<_Func, const value_type&&>>;
11570eae32dcSDimitry Andric    static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
11580eae32dcSDimitry Andric    static_assert(!is_same_v<_Up, in_place_t>,
11590eae32dcSDimitry Andric                  "Result of f(std::move(value())) should not be std::in_place_t");
11600eae32dcSDimitry Andric    static_assert(!is_same_v<_Up, nullopt_t>,
11610eae32dcSDimitry Andric                  "Result of f(std::move(value())) should not be std::nullopt_t");
11620eae32dcSDimitry Andric    static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
11630eae32dcSDimitry Andric    if (*this)
11640eae32dcSDimitry Andric      return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), _VSTD::move(value()));
11650eae32dcSDimitry Andric    return optional<_Up>();
11660eae32dcSDimitry Andric  }
11670eae32dcSDimitry Andric
11680eae32dcSDimitry Andric  template<invocable _Func>
11690eae32dcSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
11700eae32dcSDimitry Andric  constexpr optional or_else(_Func&& __f) const& requires is_copy_constructible_v<value_type> {
11710eae32dcSDimitry Andric    static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
11720eae32dcSDimitry Andric                  "Result of f() should be the same type as this optional");
11730eae32dcSDimitry Andric    if (*this)
11740eae32dcSDimitry Andric      return *this;
11750eae32dcSDimitry Andric    return _VSTD::forward<_Func>(__f)();
11760eae32dcSDimitry Andric  }
11770eae32dcSDimitry Andric
11780eae32dcSDimitry Andric  template<invocable _Func>
11790eae32dcSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
11800eae32dcSDimitry Andric  constexpr optional or_else(_Func&& __f) && requires is_move_constructible_v<value_type> {
11810eae32dcSDimitry Andric    static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
11820eae32dcSDimitry Andric                  "Result of f() should be the same type as this optional");
11830eae32dcSDimitry Andric    if (*this)
11840eae32dcSDimitry Andric      return _VSTD::move(*this);
11850eae32dcSDimitry Andric    return _VSTD::forward<_Func>(__f)();
11860eae32dcSDimitry Andric  }
11870eae32dcSDimitry Andric#endif // _LIBCPP_STD_VER > 20
11880eae32dcSDimitry Andric
11890b57cec5SDimitry Andric    using __base::reset;
11900b57cec5SDimitry Andric};
11910b57cec5SDimitry Andric
1192349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17
119304eeddc0SDimitry Andrictemplate<class _Tp>
119404eeddc0SDimitry Andric    optional(_Tp) -> optional<_Tp>;
11950b57cec5SDimitry Andric#endif
11960b57cec5SDimitry Andric
11970b57cec5SDimitry Andric// Comparisons between optionals
11980b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
11990b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1200349cc55cSDimitry Andricenable_if_t<
1201fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() ==
1202fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
12030b57cec5SDimitry Andric    bool
12040b57cec5SDimitry Andric>
12050b57cec5SDimitry Andricoperator==(const optional<_Tp>& __x, const optional<_Up>& __y)
12060b57cec5SDimitry Andric{
12070b57cec5SDimitry Andric    if (static_cast<bool>(__x) != static_cast<bool>(__y))
12080b57cec5SDimitry Andric        return false;
12090b57cec5SDimitry Andric    if (!static_cast<bool>(__x))
12100b57cec5SDimitry Andric        return true;
12110b57cec5SDimitry Andric    return *__x == *__y;
12120b57cec5SDimitry Andric}
12130b57cec5SDimitry Andric
12140b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
12150b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1216349cc55cSDimitry Andricenable_if_t<
1217fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() !=
1218fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
12190b57cec5SDimitry Andric    bool
12200b57cec5SDimitry Andric>
12210b57cec5SDimitry Andricoperator!=(const optional<_Tp>& __x, const optional<_Up>& __y)
12220b57cec5SDimitry Andric{
12230b57cec5SDimitry Andric    if (static_cast<bool>(__x) != static_cast<bool>(__y))
12240b57cec5SDimitry Andric        return true;
12250b57cec5SDimitry Andric    if (!static_cast<bool>(__x))
12260b57cec5SDimitry Andric        return false;
12270b57cec5SDimitry Andric    return *__x != *__y;
12280b57cec5SDimitry Andric}
12290b57cec5SDimitry Andric
12300b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
12310b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1232349cc55cSDimitry Andricenable_if_t<
1233fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() <
1234fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
12350b57cec5SDimitry Andric    bool
12360b57cec5SDimitry Andric>
12370b57cec5SDimitry Andricoperator<(const optional<_Tp>& __x, const optional<_Up>& __y)
12380b57cec5SDimitry Andric{
12390b57cec5SDimitry Andric    if (!static_cast<bool>(__y))
12400b57cec5SDimitry Andric        return false;
12410b57cec5SDimitry Andric    if (!static_cast<bool>(__x))
12420b57cec5SDimitry Andric        return true;
12430b57cec5SDimitry Andric    return *__x < *__y;
12440b57cec5SDimitry Andric}
12450b57cec5SDimitry Andric
12460b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
12470b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1248349cc55cSDimitry Andricenable_if_t<
1249fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() >
1250fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
12510b57cec5SDimitry Andric    bool
12520b57cec5SDimitry Andric>
12530b57cec5SDimitry Andricoperator>(const optional<_Tp>& __x, const optional<_Up>& __y)
12540b57cec5SDimitry Andric{
12550b57cec5SDimitry Andric    if (!static_cast<bool>(__x))
12560b57cec5SDimitry Andric        return false;
12570b57cec5SDimitry Andric    if (!static_cast<bool>(__y))
12580b57cec5SDimitry Andric        return true;
12590b57cec5SDimitry Andric    return *__x > *__y;
12600b57cec5SDimitry Andric}
12610b57cec5SDimitry Andric
12620b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
12630b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1264349cc55cSDimitry Andricenable_if_t<
1265fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() <=
1266fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
12670b57cec5SDimitry Andric    bool
12680b57cec5SDimitry Andric>
12690b57cec5SDimitry Andricoperator<=(const optional<_Tp>& __x, const optional<_Up>& __y)
12700b57cec5SDimitry Andric{
12710b57cec5SDimitry Andric    if (!static_cast<bool>(__x))
12720b57cec5SDimitry Andric        return true;
12730b57cec5SDimitry Andric    if (!static_cast<bool>(__y))
12740b57cec5SDimitry Andric        return false;
12750b57cec5SDimitry Andric    return *__x <= *__y;
12760b57cec5SDimitry Andric}
12770b57cec5SDimitry Andric
12780b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
12790b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1280349cc55cSDimitry Andricenable_if_t<
1281fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() >=
1282fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
12830b57cec5SDimitry Andric    bool
12840b57cec5SDimitry Andric>
12850b57cec5SDimitry Andricoperator>=(const optional<_Tp>& __x, const optional<_Up>& __y)
12860b57cec5SDimitry Andric{
12870b57cec5SDimitry Andric    if (!static_cast<bool>(__y))
12880b57cec5SDimitry Andric        return true;
12890b57cec5SDimitry Andric    if (!static_cast<bool>(__x))
12900b57cec5SDimitry Andric        return false;
12910b57cec5SDimitry Andric    return *__x >= *__y;
12920b57cec5SDimitry Andric}
12930b57cec5SDimitry Andric
12940b57cec5SDimitry Andric// Comparisons with nullopt
12950b57cec5SDimitry Andrictemplate <class _Tp>
12960b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
12970b57cec5SDimitry Andricbool
12980b57cec5SDimitry Andricoperator==(const optional<_Tp>& __x, nullopt_t) noexcept
12990b57cec5SDimitry Andric{
13000b57cec5SDimitry Andric    return !static_cast<bool>(__x);
13010b57cec5SDimitry Andric}
13020b57cec5SDimitry Andric
13030b57cec5SDimitry Andrictemplate <class _Tp>
13040b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13050b57cec5SDimitry Andricbool
13060b57cec5SDimitry Andricoperator==(nullopt_t, const optional<_Tp>& __x) noexcept
13070b57cec5SDimitry Andric{
13080b57cec5SDimitry Andric    return !static_cast<bool>(__x);
13090b57cec5SDimitry Andric}
13100b57cec5SDimitry Andric
13110b57cec5SDimitry Andrictemplate <class _Tp>
13120b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13130b57cec5SDimitry Andricbool
13140b57cec5SDimitry Andricoperator!=(const optional<_Tp>& __x, nullopt_t) noexcept
13150b57cec5SDimitry Andric{
13160b57cec5SDimitry Andric    return static_cast<bool>(__x);
13170b57cec5SDimitry Andric}
13180b57cec5SDimitry Andric
13190b57cec5SDimitry Andrictemplate <class _Tp>
13200b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13210b57cec5SDimitry Andricbool
13220b57cec5SDimitry Andricoperator!=(nullopt_t, const optional<_Tp>& __x) noexcept
13230b57cec5SDimitry Andric{
13240b57cec5SDimitry Andric    return static_cast<bool>(__x);
13250b57cec5SDimitry Andric}
13260b57cec5SDimitry Andric
13270b57cec5SDimitry Andrictemplate <class _Tp>
13280b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13290b57cec5SDimitry Andricbool
13300b57cec5SDimitry Andricoperator<(const optional<_Tp>&, nullopt_t) noexcept
13310b57cec5SDimitry Andric{
13320b57cec5SDimitry Andric    return false;
13330b57cec5SDimitry Andric}
13340b57cec5SDimitry Andric
13350b57cec5SDimitry Andrictemplate <class _Tp>
13360b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13370b57cec5SDimitry Andricbool
13380b57cec5SDimitry Andricoperator<(nullopt_t, const optional<_Tp>& __x) noexcept
13390b57cec5SDimitry Andric{
13400b57cec5SDimitry Andric    return static_cast<bool>(__x);
13410b57cec5SDimitry Andric}
13420b57cec5SDimitry Andric
13430b57cec5SDimitry Andrictemplate <class _Tp>
13440b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13450b57cec5SDimitry Andricbool
13460b57cec5SDimitry Andricoperator<=(const optional<_Tp>& __x, nullopt_t) noexcept
13470b57cec5SDimitry Andric{
13480b57cec5SDimitry Andric    return !static_cast<bool>(__x);
13490b57cec5SDimitry Andric}
13500b57cec5SDimitry Andric
13510b57cec5SDimitry Andrictemplate <class _Tp>
13520b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13530b57cec5SDimitry Andricbool
13540b57cec5SDimitry Andricoperator<=(nullopt_t, const optional<_Tp>&) noexcept
13550b57cec5SDimitry Andric{
13560b57cec5SDimitry Andric    return true;
13570b57cec5SDimitry Andric}
13580b57cec5SDimitry Andric
13590b57cec5SDimitry Andrictemplate <class _Tp>
13600b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13610b57cec5SDimitry Andricbool
13620b57cec5SDimitry Andricoperator>(const optional<_Tp>& __x, nullopt_t) noexcept
13630b57cec5SDimitry Andric{
13640b57cec5SDimitry Andric    return static_cast<bool>(__x);
13650b57cec5SDimitry Andric}
13660b57cec5SDimitry Andric
13670b57cec5SDimitry Andrictemplate <class _Tp>
13680b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13690b57cec5SDimitry Andricbool
13700b57cec5SDimitry Andricoperator>(nullopt_t, const optional<_Tp>&) noexcept
13710b57cec5SDimitry Andric{
13720b57cec5SDimitry Andric    return false;
13730b57cec5SDimitry Andric}
13740b57cec5SDimitry Andric
13750b57cec5SDimitry Andrictemplate <class _Tp>
13760b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13770b57cec5SDimitry Andricbool
13780b57cec5SDimitry Andricoperator>=(const optional<_Tp>&, nullopt_t) noexcept
13790b57cec5SDimitry Andric{
13800b57cec5SDimitry Andric    return true;
13810b57cec5SDimitry Andric}
13820b57cec5SDimitry Andric
13830b57cec5SDimitry Andrictemplate <class _Tp>
13840b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
13850b57cec5SDimitry Andricbool
13860b57cec5SDimitry Andricoperator>=(nullopt_t, const optional<_Tp>& __x) noexcept
13870b57cec5SDimitry Andric{
13880b57cec5SDimitry Andric    return !static_cast<bool>(__x);
13890b57cec5SDimitry Andric}
13900b57cec5SDimitry Andric
13910b57cec5SDimitry Andric// Comparisons with T
13920b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
13930b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1394349cc55cSDimitry Andricenable_if_t<
1395fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() ==
1396fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
13970b57cec5SDimitry Andric    bool
13980b57cec5SDimitry Andric>
13990b57cec5SDimitry Andricoperator==(const optional<_Tp>& __x, const _Up& __v)
14000b57cec5SDimitry Andric{
14010b57cec5SDimitry Andric    return static_cast<bool>(__x) ? *__x == __v : false;
14020b57cec5SDimitry Andric}
14030b57cec5SDimitry Andric
14040b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
14050b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1406349cc55cSDimitry Andricenable_if_t<
1407fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() ==
1408fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
14090b57cec5SDimitry Andric    bool
14100b57cec5SDimitry Andric>
14110b57cec5SDimitry Andricoperator==(const _Tp& __v, const optional<_Up>& __x)
14120b57cec5SDimitry Andric{
14130b57cec5SDimitry Andric    return static_cast<bool>(__x) ? __v == *__x : false;
14140b57cec5SDimitry Andric}
14150b57cec5SDimitry Andric
14160b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
14170b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1418349cc55cSDimitry Andricenable_if_t<
1419fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() !=
1420fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
14210b57cec5SDimitry Andric    bool
14220b57cec5SDimitry Andric>
14230b57cec5SDimitry Andricoperator!=(const optional<_Tp>& __x, const _Up& __v)
14240b57cec5SDimitry Andric{
14250b57cec5SDimitry Andric    return static_cast<bool>(__x) ? *__x != __v : true;
14260b57cec5SDimitry Andric}
14270b57cec5SDimitry Andric
14280b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
14290b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1430349cc55cSDimitry Andricenable_if_t<
1431fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() !=
1432fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
14330b57cec5SDimitry Andric    bool
14340b57cec5SDimitry Andric>
14350b57cec5SDimitry Andricoperator!=(const _Tp& __v, const optional<_Up>& __x)
14360b57cec5SDimitry Andric{
14370b57cec5SDimitry Andric    return static_cast<bool>(__x) ? __v != *__x : true;
14380b57cec5SDimitry Andric}
14390b57cec5SDimitry Andric
14400b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
14410b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1442349cc55cSDimitry Andricenable_if_t<
1443fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() <
1444fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
14450b57cec5SDimitry Andric    bool
14460b57cec5SDimitry Andric>
14470b57cec5SDimitry Andricoperator<(const optional<_Tp>& __x, const _Up& __v)
14480b57cec5SDimitry Andric{
14490b57cec5SDimitry Andric    return static_cast<bool>(__x) ? *__x < __v : true;
14500b57cec5SDimitry Andric}
14510b57cec5SDimitry Andric
14520b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
14530b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1454349cc55cSDimitry Andricenable_if_t<
1455fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() <
1456fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
14570b57cec5SDimitry Andric    bool
14580b57cec5SDimitry Andric>
14590b57cec5SDimitry Andricoperator<(const _Tp& __v, const optional<_Up>& __x)
14600b57cec5SDimitry Andric{
14610b57cec5SDimitry Andric    return static_cast<bool>(__x) ? __v < *__x : false;
14620b57cec5SDimitry Andric}
14630b57cec5SDimitry Andric
14640b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
14650b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1466349cc55cSDimitry Andricenable_if_t<
1467fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() <=
1468fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
14690b57cec5SDimitry Andric    bool
14700b57cec5SDimitry Andric>
14710b57cec5SDimitry Andricoperator<=(const optional<_Tp>& __x, const _Up& __v)
14720b57cec5SDimitry Andric{
14730b57cec5SDimitry Andric    return static_cast<bool>(__x) ? *__x <= __v : true;
14740b57cec5SDimitry Andric}
14750b57cec5SDimitry Andric
14760b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
14770b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1478349cc55cSDimitry Andricenable_if_t<
1479fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() <=
1480fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
14810b57cec5SDimitry Andric    bool
14820b57cec5SDimitry Andric>
14830b57cec5SDimitry Andricoperator<=(const _Tp& __v, const optional<_Up>& __x)
14840b57cec5SDimitry Andric{
14850b57cec5SDimitry Andric    return static_cast<bool>(__x) ? __v <= *__x : false;
14860b57cec5SDimitry Andric}
14870b57cec5SDimitry Andric
14880b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
14890b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1490349cc55cSDimitry Andricenable_if_t<
1491fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() >
1492fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
14930b57cec5SDimitry Andric    bool
14940b57cec5SDimitry Andric>
14950b57cec5SDimitry Andricoperator>(const optional<_Tp>& __x, const _Up& __v)
14960b57cec5SDimitry Andric{
14970b57cec5SDimitry Andric    return static_cast<bool>(__x) ? *__x > __v : false;
14980b57cec5SDimitry Andric}
14990b57cec5SDimitry Andric
15000b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
15010b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1502349cc55cSDimitry Andricenable_if_t<
1503fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() >
1504fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
15050b57cec5SDimitry Andric    bool
15060b57cec5SDimitry Andric>
15070b57cec5SDimitry Andricoperator>(const _Tp& __v, const optional<_Up>& __x)
15080b57cec5SDimitry Andric{
15090b57cec5SDimitry Andric    return static_cast<bool>(__x) ? __v > *__x : true;
15100b57cec5SDimitry Andric}
15110b57cec5SDimitry Andric
15120b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
15130b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1514349cc55cSDimitry Andricenable_if_t<
1515fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() >=
1516fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
15170b57cec5SDimitry Andric    bool
15180b57cec5SDimitry Andric>
15190b57cec5SDimitry Andricoperator>=(const optional<_Tp>& __x, const _Up& __v)
15200b57cec5SDimitry Andric{
15210b57cec5SDimitry Andric    return static_cast<bool>(__x) ? *__x >= __v : false;
15220b57cec5SDimitry Andric}
15230b57cec5SDimitry Andric
15240b57cec5SDimitry Andrictemplate <class _Tp, class _Up>
15250b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
1526349cc55cSDimitry Andricenable_if_t<
1527fe6060f1SDimitry Andric    is_convertible_v<decltype(declval<const _Tp&>() >=
1528fe6060f1SDimitry Andric        declval<const _Up&>()), bool>,
15290b57cec5SDimitry Andric    bool
15300b57cec5SDimitry Andric>
15310b57cec5SDimitry Andricoperator>=(const _Tp& __v, const optional<_Up>& __x)
15320b57cec5SDimitry Andric{
15330b57cec5SDimitry Andric    return static_cast<bool>(__x) ? __v >= *__x : true;
15340b57cec5SDimitry Andric}
15350b57cec5SDimitry Andric
15360b57cec5SDimitry Andric
15370b57cec5SDimitry Andrictemplate <class _Tp>
1538fe6060f1SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1539349cc55cSDimitry Andricenable_if_t<
15400b57cec5SDimitry Andric    is_move_constructible_v<_Tp> && is_swappable_v<_Tp>,
15410b57cec5SDimitry Andric    void
15420b57cec5SDimitry Andric>
15430b57cec5SDimitry Andricswap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y)))
15440b57cec5SDimitry Andric{
15450b57cec5SDimitry Andric    __x.swap(__y);
15460b57cec5SDimitry Andric}
15470b57cec5SDimitry Andric
15480b57cec5SDimitry Andrictemplate <class _Tp>
15490b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
15500b57cec5SDimitry Andricoptional<decay_t<_Tp>> make_optional(_Tp&& __v)
15510b57cec5SDimitry Andric{
15520b57cec5SDimitry Andric    return optional<decay_t<_Tp>>(_VSTD::forward<_Tp>(__v));
15530b57cec5SDimitry Andric}
15540b57cec5SDimitry Andric
15550b57cec5SDimitry Andrictemplate <class _Tp, class... _Args>
15560b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
15570b57cec5SDimitry Andricoptional<_Tp> make_optional(_Args&&... __args)
15580b57cec5SDimitry Andric{
15590b57cec5SDimitry Andric    return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...);
15600b57cec5SDimitry Andric}
15610b57cec5SDimitry Andric
15620b57cec5SDimitry Andrictemplate <class _Tp, class _Up, class... _Args>
15630b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
15640b57cec5SDimitry Andricoptional<_Tp> make_optional(initializer_list<_Up> __il,  _Args&&... __args)
15650b57cec5SDimitry Andric{
15660b57cec5SDimitry Andric    return optional<_Tp>(in_place, __il, _VSTD::forward<_Args>(__args)...);
15670b57cec5SDimitry Andric}
15680b57cec5SDimitry Andric
15690b57cec5SDimitry Andrictemplate <class _Tp>
15700b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<
15710b57cec5SDimitry Andric    __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>>
15720b57cec5SDimitry Andric>
15730b57cec5SDimitry Andric{
1574fe6060f1SDimitry Andric#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1575fe6060f1SDimitry Andric    _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type;
1576fe6060f1SDimitry Andric    _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t        result_type;
1577fe6060f1SDimitry Andric#endif
15780b57cec5SDimitry Andric
15790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1580fe6060f1SDimitry Andric    size_t operator()(const optional<_Tp>& __opt) const
15810b57cec5SDimitry Andric    {
15820b57cec5SDimitry Andric        return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
15830b57cec5SDimitry Andric    }
15840b57cec5SDimitry Andric};
15850b57cec5SDimitry Andric
15860b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
15870b57cec5SDimitry Andric
15880b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 14
15890b57cec5SDimitry Andric
15900b57cec5SDimitry Andric#endif // _LIBCPP_OPTIONAL
1591