/* SPDX-License-Identifier: 0BSD */

/* Copyright (C) 2026 Mintsuki and contributors.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */


#ifndef __FREESTANDING_CXX_HDRS_OPTIONAL
#define __FREESTANDING_CXX_HDRS_OPTIONAL 1

#pragma GCC system_header

#include <__fcxx/config.h>

#if _FCXX_STD < 2023
#  error "<optional> is not a freestanding header before C++26"
#endif

#define _FCXX_VERSION_OPTIONAL
#include <__fcxx/version.h>
#undef _FCXX_VERSION_OPTIONAL

#if _FCXX_STD >= 2023

#include <initializer_list>
#include <type_traits>
#include <utility>
#include <__fcxx/addressof.h>
#include <__fcxx/construct_at.h>
#include <__fcxx/converts_from_any_cvref.h>
#include <__fcxx/exception.h>
#include <__fcxx/hash.h>
#include <__fcxx/special_members.h>
#include <compare>
#include <concepts>
#include <__fcxx/comparison_constraints.h>
#include <__fcxx/ranges_base.h>
/* optional is a range from C++26 on, and <optional> provides the range
 * access functions of <iterator> since. */
#include <__fcxx/range_access.h>

/* The comparisons are constrained from C++26 on, where they are written
 * with requires-clauses. */

namespace std {

/* [optional.nullopt], no-value state indicator */

struct nullopt_t {
    /* The argument of the constructor, as nullopt_t has no default
     * constructor, so that "o = {}" is not ambiguous. */
    struct __fcxx_tag {
        explicit __fcxx_tag() = default;
    };

    constexpr explicit nullopt_t(__fcxx_tag) noexcept {}

    friend constexpr bool operator==(nullopt_t, nullopt_t) noexcept {
        return true;
    }

    friend constexpr strong_ordering operator<=>(nullopt_t, nullopt_t) noexcept {
        return strong_ordering::equal;
    }
};

inline constexpr nullopt_t nullopt{nullopt_t::__fcxx_tag()};

/* [optional.bad.access], class bad_optional_access, whose members are all
 * defined here, as no C++ runtime defines them */

class bad_optional_access : public exception {
public:
    constexpr bad_optional_access() noexcept {}
    constexpr bad_optional_access(const bad_optional_access&) noexcept = default;
    constexpr bad_optional_access& operator=(const bad_optional_access&) noexcept = default;

    constexpr const char* what() const noexcept override {
        return "std::bad_optional_access";
    }
};

/* [optional.optional], class template optional */

template <class _Tp>
class optional;

template <class _Tp>
struct __fcxx_is_optional : false_type {};

template <class _Tp>
struct __fcxx_is_optional<optional<_Tp> > : true_type {};

/* Whether a type can be held by optional: an object type other than an
 * array, in_place_t and nullopt_t, or, from C++26 on, an lvalue reference to
 * any other type. */
template <class _Tp>
struct __fcxx_optional_valid
    : __fcxx_bool_constant<(
                               is_lvalue_reference<_Tp>::value ||
                               (is_object<_Tp>::value && !is_array<_Tp>::value))
                           && !is_same<__fcxx_remove_cvref_t<_Tp>, in_place_t>::value
                           && !is_same<__fcxx_remove_cvref_t<_Tp>, nullopt_t>::value> {};

/* The tag of the constructors that hold the result of calling a function,
 * for transform. */
struct __fcxx_optional_invoke_tag {
    explicit __fcxx_optional_invoke_tag() = default;
};

/* The storage of the contained value, whose destructor is trivial where the
 * value's is. */
template <class _Vp, bool = is_trivially_destructible<_Vp>::value>
union __fcxx_optional_union {
    __fcxx_union_empty __empty;
    _Vp __val;

#  if _FCXX_GCC >= 1400 && _FCXX_GCC < 1500
    /* Workaround: GCC 14, all targets: where it cannot tell which of two
     * optionals one is, as with the result of a conditional expression of
     * one that holds a value and one that does not, it warns in the code of
     * the caller that the value may be used uninitialized
     * (-Wmaybe-uninitialized) where only one that holds a value reads it, so
     * an optional of a scalar type that holds none initializes it too. */
    constexpr __fcxx_optional_union() noexcept
        requires (!is_scalar<_Vp>::value)
        : __empty() {}

    constexpr __fcxx_optional_union() noexcept
        requires (is_scalar<_Vp>::value)
        : __val() {}
#  else
    constexpr __fcxx_optional_union() noexcept : __empty() {}
#  endif

    template <class... _Args>
    constexpr explicit __fcxx_optional_union(in_place_t, _Args&&... __args) : __val(std::forward<_Args>(__args)...) {}

    template <class _Fp, class... _Args>
    constexpr __fcxx_optional_union(__fcxx_optional_invoke_tag, _Fp&& __f, _Args&&... __args)
        : __val(std::__fcxx_invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)) {}
};

template <class _Vp>
union __fcxx_optional_union<_Vp, false> {
    __fcxx_union_empty __empty;
    _Vp __val;

    constexpr __fcxx_optional_union() noexcept : __empty() {}

    template <class... _Args>
    constexpr explicit __fcxx_optional_union(in_place_t, _Args&&... __args) : __val(std::forward<_Args>(__args)...) {}

    template <class _Fp, class... _Args>
    constexpr __fcxx_optional_union(__fcxx_optional_invoke_tag, _Fp&& __f, _Args&&... __args)
        : __val(std::__fcxx_invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)) {}

    __fcxx_optional_union(const __fcxx_optional_union&) = default;
    __fcxx_optional_union(__fcxx_optional_union&&) = default;
    __fcxx_optional_union& operator=(const __fcxx_optional_union&) = default;
    __fcxx_optional_union& operator=(__fcxx_optional_union&&) = default;

    constexpr ~__fcxx_optional_union() {}
};

/* The contained value, if any, and what optional does with it. The value is
 * an object of the type without its cv-qualifiers, which C++26 uses as such,
 * where the earlier revisions use it through the type itself. */
template <class _Tp>
struct __fcxx_optional_payload {
    typedef __fcxx_remove_cv_t<_Tp> _Vp;
    typedef _Vp _Access;

    __fcxx_optional_union<_Vp> __u;
    bool __engaged;

    constexpr explicit __fcxx_optional_payload(__fcxx_no_value_tag) noexcept : __u(), __engaged(false) {}

    template <class... _Args>
    constexpr explicit __fcxx_optional_payload(in_place_t __tag, _Args&&... __args)
        : __u(__tag, std::forward<_Args>(__args)...), __engaged(true) {}

    template <class _Fp, class... _Args>
    constexpr __fcxx_optional_payload(__fcxx_optional_invoke_tag __tag, _Fp&& __f, _Args&&... __args)
        : __u(__tag, std::forward<_Fp>(__f), std::forward<_Args>(__args)...), __engaged(true) {}

    constexpr _Access& __get() noexcept {
        return __u.__val;
    }

    constexpr const _Access& __get() const noexcept {
        return __u.__val;
    }

    /* Constructs the value, which there must not be yet. */
    template <class... _Args>
    constexpr void __construct(_Args&&... __args) {
        std::construct_at(std::addressof(__u.__val), std::forward<_Args>(__args)...);
        __engaged = true;
    }

    constexpr void __reset() noexcept {
        if (__engaged) {
            if constexpr (!is_trivially_destructible<_Vp>::value) {
                __u.__val.~_Vp();
            }
            __engaged = false;
        }
    }

    /* The copy and move constructors and assignment operators. */
    constexpr __fcxx_optional_payload(__fcxx_smf_construct_tag, const __fcxx_optional_payload& __other)
        : __u(), __engaged(false) {
        if (__other.__engaged) {
            __construct(__other.__get());
        }
    }

    constexpr __fcxx_optional_payload(__fcxx_smf_construct_tag, __fcxx_optional_payload&& __other)
        noexcept(is_nothrow_move_constructible<_Tp>::value)
        : __u(), __engaged(false) {
        if (__other.__engaged) {
            __construct(std::move(__other.__get()));
        }
    }

    constexpr void __fcxx_assign_from(const __fcxx_optional_payload& __other) {
        if (__engaged && __other.__engaged) {
            __get() = __other.__get();
        } else if (__other.__engaged) {
            __construct(__other.__get());
        } else {
            __reset();
        }
    }

    constexpr void __fcxx_assign_from(__fcxx_optional_payload&& __other)
        noexcept(is_nothrow_move_assignable<_Tp>::value && is_nothrow_move_constructible<_Tp>::value) {
        if (__engaged && __other.__engaged) {
            __get() = std::move(__other.__get());
        } else if (__other.__engaged) {
            __construct(std::move(__other.__get()));
        } else {
            __reset();
        }
    }
};

template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
struct __fcxx_optional_storage : __fcxx_optional_payload<_Tp> {
    using __fcxx_optional_payload<_Tp>::__fcxx_optional_payload;
};

template <class _Tp>
struct __fcxx_optional_storage<_Tp, false> : __fcxx_optional_payload<_Tp> {
    using __fcxx_optional_payload<_Tp>::__fcxx_optional_payload;

    __fcxx_optional_storage(const __fcxx_optional_storage&) = default;
    __fcxx_optional_storage(__fcxx_optional_storage&&) = default;
    __fcxx_optional_storage& operator=(const __fcxx_optional_storage&) = default;
    __fcxx_optional_storage& operator=(__fcxx_optional_storage&&) = default;

    constexpr ~__fcxx_optional_storage() {
        this->__reset();
    }
};

/* The base class of optional, with its copy and move constructors and
 * assignment operators, for the types it can hold. */
template <class _Tp>
struct __fcxx_optional_smf {
    typedef __fcxx_smf<__fcxx_optional_storage<_Tp>,
                       __fcxx_smf_select<is_copy_constructible<_Tp>::value,
                                         is_trivially_copy_constructible<_Tp>::value, __fcxx_smf_deleted>::value,
                       __fcxx_smf_select<is_move_constructible<_Tp>::value,
                                         is_trivially_move_constructible<_Tp>::value, __fcxx_smf_absent>::value,
                       __fcxx_smf_select<is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value,
                                         is_trivially_copy_constructible<_Tp>::value
                                             && is_trivially_copy_assignable<_Tp>::value
                                             && is_trivially_destructible<_Tp>::value,
                                         __fcxx_smf_deleted>::value,
                       __fcxx_smf_select<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value,
                                         is_trivially_move_constructible<_Tp>::value
                                             && is_trivially_move_assignable<_Tp>::value
                                             && is_trivially_destructible<_Tp>::value,
                                         __fcxx_smf_absent>::value>
        type;
};

struct __fcxx_optional_invalid {};

template <class _Tp>
using __fcxx_optional_base_t
    = typename __fcxx_conditional_t<__fcxx_optional_valid<_Tp>::value && !is_reference<_Tp>::value,
                                    __fcxx_optional_smf<_Tp>, __fcxx_type_identity<__fcxx_optional_invalid> >::type;

/* The constraints of the converting constructors and assignment operators,
 * which leave out as soon as they can what does not apply, lest the traits
 * of the other types be needed. */
template <class _Tp, class _Up>
struct __fcxx_optional_from_value {
    typedef __fcxx_remove_cvref_t<_Up> _Uq;
    static constexpr bool __ok = __fcxx_and<__fcxx_not<is_same<_Uq, in_place_t> >,
                                            __fcxx_not<is_same<_Uq, optional<_Tp> > >,
                                            __fcxx_not<__fcxx_and<is_same<__fcxx_remove_cv_t<_Tp>, bool>,
                                                                  __fcxx_is_optional<_Uq> > >,
                                            is_constructible<_Tp, _Up> >::value;
    static constexpr bool __implicit = __ok && is_convertible<_Up, _Tp>::value;
    static constexpr bool __explicit = __ok && !is_convertible<_Up, _Tp>::value;
};

template <class _Tp, class _Up, class _Arg>
struct __fcxx_optional_from_optional {
    static constexpr bool __ok = __fcxx_and<is_constructible<_Tp, _Arg>,
                                            __fcxx_or<is_same<__fcxx_remove_cv_t<_Tp>, bool>,
                                                      __fcxx_not<__fcxx_converts_from_any_cvref<_Tp, optional<_Up> > > >
                                            >::value;
    static constexpr bool __implicit = __ok && is_convertible<_Arg, _Tp>::value;
    static constexpr bool __explicit = __ok && !is_convertible<_Arg, _Tp>::value;
};

template <class _Tp, class _Up>
struct __fcxx_optional_assign_value
    : __fcxx_and<__fcxx_not<is_same<__fcxx_remove_cvref_t<_Up>, optional<_Tp> > >,
                 __fcxx_not<__fcxx_and<is_scalar<_Tp>, is_same<_Tp, __fcxx_decay_t<_Up> > > >,
                 is_constructible<_Tp, _Up>, is_assignable<_Tp&, _Up> > {};

template <class _Tp, class _Up, class _Arg>
struct __fcxx_optional_assign_optional
    : __fcxx_and<is_constructible<_Tp, _Arg>, is_assignable<_Tp&, _Arg>,
                 __fcxx_not<__fcxx_converts_from_any_cvref<_Tp, optional<_Up> > >,
                 __fcxx_not<is_assignable<_Tp&, optional<_Up>&> >, __fcxx_not<is_assignable<_Tp&, optional<_Up>&&> >,
                 __fcxx_not<is_assignable<_Tp&, const optional<_Up>&> >,
                 __fcxx_not<is_assignable<_Tp&, const optional<_Up>&&> > > {};

template <class _Tp>
class optional : private __fcxx_optional_base_t<_Tp> {
    typedef __fcxx_optional_base_t<_Tp> _Base;

    static_assert(__fcxx_optional_valid<_Tp>::value && !is_reference<_Tp>::value,
                  "std::optional of a type it cannot hold");

    /* The default template argument of the converting constructor and
     * assignment operator, and of value_or. */
    typedef __fcxx_remove_cv_t<_Tp> _Dp;

    template <class _Up>
    friend class optional;

public:
    typedef _Tp value_type;
    typedef _Tp* iterator;
    typedef const _Tp* const_iterator;

    /* [optional.ctor], constructors */

    constexpr optional() noexcept : _Base(__fcxx_no_value_tag()) {}

    constexpr optional(nullopt_t) noexcept : _Base(__fcxx_no_value_tag()) {}

    optional(const optional&) = default;
    optional(optional&&) = default;

    template <class... _Args, __fcxx_enable_if_t<is_constructible<_Tp, _Args...>::value, int> = 0>
    constexpr explicit optional(in_place_t __tag, _Args&&... __args) : _Base(__tag, std::forward<_Args>(__args)...) {}

    template <class _Up, class... _Args,
              __fcxx_enable_if_t<is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value, int> = 0>
    constexpr explicit optional(in_place_t __tag, initializer_list<_Up> __il, _Args&&... __args)
        : _Base(__tag, __il, std::forward<_Args>(__args)...) {}

    template <class _Up = _Dp, __fcxx_enable_if_t<__fcxx_optional_from_value<_Tp, _Up>::__implicit, int> = 0>
    constexpr optional(_Up&& __v) : _Base(in_place_t(), std::forward<_Up>(__v)) {}

    template <class _Up = _Dp, __fcxx_enable_if_t<__fcxx_optional_from_value<_Tp, _Up>::__explicit, int> = 0>
    constexpr explicit optional(_Up&& __v) : _Base(in_place_t(), std::forward<_Up>(__v)) {}

    template <class _Up, __fcxx_enable_if_t<__fcxx_optional_from_optional<_Tp, _Up, const _Up&>::__implicit, int> = 0>
    constexpr optional(const optional<_Up>& __rhs) : _Base(__fcxx_no_value_tag()) {
        if (__rhs.has_value()) {
            this->__construct(*__rhs);
        }
    }

    template <class _Up, __fcxx_enable_if_t<__fcxx_optional_from_optional<_Tp, _Up, const _Up&>::__explicit, int> = 0>
    constexpr explicit optional(const optional<_Up>& __rhs) : _Base(__fcxx_no_value_tag()) {
        if (__rhs.has_value()) {
            this->__construct(*__rhs);
        }
    }

    template <class _Up, __fcxx_enable_if_t<__fcxx_optional_from_optional<_Tp, _Up, _Up>::__implicit, int> = 0>
    constexpr optional(optional<_Up>&& __rhs) : _Base(__fcxx_no_value_tag()) {
        if (__rhs.has_value()) {
            this->__construct(*std::move(__rhs));
        }
    }

    template <class _Up, __fcxx_enable_if_t<__fcxx_optional_from_optional<_Tp, _Up, _Up>::__explicit, int> = 0>
    constexpr explicit optional(optional<_Up>&& __rhs) : _Base(__fcxx_no_value_tag()) {
        if (__rhs.has_value()) {
            this->__construct(*std::move(__rhs));
        }
    }

    /* [optional.assign], assignment */

    constexpr optional& operator=(nullopt_t) noexcept {
        this->__reset();
        return *this;
    }

    optional& operator=(const optional&) = default;
    optional& operator=(optional&&) = default;

    template <class _Up = _Dp, __fcxx_enable_if_t<__fcxx_optional_assign_value<_Tp, _Up>::value, int> = 0>
    constexpr optional& operator=(_Up&& __v) {
        if (this->__engaged) {
            this->__get() = std::forward<_Up>(__v);
        } else {
            this->__construct(std::forward<_Up>(__v));
        }
        return *this;
    }

    template <class _Up, __fcxx_enable_if_t<__fcxx_optional_assign_optional<_Tp, _Up, const _Up&>::value, int> = 0>
    constexpr optional& operator=(const optional<_Up>& __rhs) {
        if (this->__engaged && __rhs.has_value()) {
            this->__get() = *__rhs;
        } else if (__rhs.has_value()) {
            this->__construct(*__rhs);
        } else {
            this->__reset();
        }
        return *this;
    }

    template <class _Up, __fcxx_enable_if_t<__fcxx_optional_assign_optional<_Tp, _Up, _Up>::value, int> = 0>
    constexpr optional& operator=(optional<_Up>&& __rhs) {
        if (this->__engaged && __rhs.has_value()) {
            this->__get() = *std::move(__rhs);
        } else if (__rhs.has_value()) {
            this->__construct(*std::move(__rhs));
        } else {
            this->__reset();
        }
        return *this;
    }

    /* Constrained from C++26 on, where the earlier revisions require the
     * arguments to construct the value from. */
    template <class... _Args, __fcxx_enable_if_t<is_constructible<_Tp, _Args...>::value, int> = 0>
    constexpr _Tp& emplace(_Args&&... __args) {
        static_assert(is_constructible<_Tp, _Args...>::value, "std::optional::emplace of a value it cannot construct");
        this->__reset();
        this->__construct(std::forward<_Args>(__args)...);
        return this->__u.__val;
    }

    template <class _Up, class... _Args,
              __fcxx_enable_if_t<is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value, int> = 0>
    constexpr _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
        this->__reset();
        this->__construct(__il, std::forward<_Args>(__args)...);
        return this->__u.__val;
    }

    /* [optional.swap], swap */

    constexpr void swap(optional& __rhs)
        noexcept(is_nothrow_move_constructible<_Tp>::value && __fcxx_is_nothrow_swappable<_Tp>::value) {
        static_assert(is_move_constructible<_Tp>::value, "std::optional::swap of a type that cannot be moved");
        if (this->__engaged && __rhs.__engaged) {
            using std::swap;
            swap(this->__get(), __rhs.__get());
        } else if (this->__engaged) {
            __rhs.__construct(std::move(this->__get()));
            this->__reset();
        } else if (__rhs.__engaged) {
            this->__construct(std::move(__rhs.__get()));
            __rhs.__reset();
        }
    }

    /* [optional.iterators], iterator support */

    constexpr iterator begin() noexcept {
        return std::addressof(this->__u.__val);
    }

    constexpr const_iterator begin() const noexcept {
        return std::addressof(this->__u.__val);
    }

    constexpr iterator end() noexcept {
        return this->__engaged ? begin() + 1 : begin();
    }

    constexpr const_iterator end() const noexcept {
        return this->__engaged ? begin() + 1 : begin();
    }

    /* [optional.observe], observers */

    constexpr const _Tp* operator->() const noexcept {
        return std::addressof(this->__u.__val);
    }

    constexpr _Tp* operator->() noexcept {
        return std::addressof(this->__u.__val);
    }

    constexpr const _Tp& operator*() const& noexcept {
        return this->__u.__val;
    }

    constexpr _Tp& operator*() & noexcept {
        return this->__u.__val;
    }

    constexpr _Tp&& operator*() && noexcept {
        return std::move(this->__u.__val);
    }

    constexpr const _Tp&& operator*() const&& noexcept {
        return std::move(this->__u.__val);
    }

    constexpr explicit operator bool() const noexcept {
        return this->__engaged;
    }

    constexpr bool has_value() const noexcept {
        return this->__engaged;
    }

    /* freestanding-deleted */
    constexpr const _Tp& value() const& = delete;
    constexpr _Tp& value() & = delete;
    constexpr _Tp&& value() && = delete;
    constexpr const _Tp&& value() const&& = delete;

    template <class _Up = _Dp>
    constexpr _Tp value_or(_Up&& __v) const& {
        static_assert(is_copy_constructible<_Tp>::value && is_convertible<_Up&&, _Tp>::value,
                      "std::optional::value_or of a value that cannot be copied or converted to");
        if (this->__engaged) {
            return this->__get();
        }
        return static_cast<_Tp>(std::forward<_Up>(__v));
    }

    template <class _Up = _Dp>
    constexpr _Tp value_or(_Up&& __v) && {
        static_assert(is_move_constructible<_Tp>::value && is_convertible<_Up&&, _Tp>::value,
                      "std::optional::value_or of a value that cannot be moved or converted to");
        if (this->__engaged) {
            return std::move(this->__get());
        }
        return static_cast<_Tp>(std::forward<_Up>(__v));
    }

    /* [optional.monadic], monadic operations */

    template <class _Fp>
    constexpr auto and_then(_Fp&& __f) & {
        return __and_then(*this, std::forward<_Fp>(__f));
    }

    template <class _Fp>
    constexpr auto and_then(_Fp&& __f) && {
        return __and_then(std::move(*this), std::forward<_Fp>(__f));
    }

    template <class _Fp>
    constexpr auto and_then(_Fp&& __f) const& {
        return __and_then(*this, std::forward<_Fp>(__f));
    }

    template <class _Fp>
    constexpr auto and_then(_Fp&& __f) const&& {
        return __and_then(std::move(*this), std::forward<_Fp>(__f));
    }

    template <class _Fp>
    constexpr auto transform(_Fp&& __f) & {
        return __transform(*this, std::forward<_Fp>(__f));
    }

    template <class _Fp>
    constexpr auto transform(_Fp&& __f) && {
        return __transform(std::move(*this), std::forward<_Fp>(__f));
    }

    template <class _Fp>
    constexpr auto transform(_Fp&& __f) const& {
        return __transform(*this, std::forward<_Fp>(__f));
    }

    template <class _Fp>
    constexpr auto transform(_Fp&& __f) const&& {
        return __transform(std::move(*this), std::forward<_Fp>(__f));
    }

    template <class _Fp>
        requires (invocable<_Fp> && move_constructible<_Tp>)
    constexpr optional or_else(_Fp&& __f) && {
        static_assert(is_same<__fcxx_remove_cvref_t<invoke_result_t<_Fp> >, optional>::value,
                      "std::optional::or_else of a function not returning the same std::optional");
        if (this->__engaged) {
            return std::move(*this);
        }
        return std::forward<_Fp>(__f)();
    }

    template <class _Fp>
        requires (invocable<_Fp> && copy_constructible<_Tp>)
    constexpr optional or_else(_Fp&& __f) const& {
        static_assert(is_same<__fcxx_remove_cvref_t<invoke_result_t<_Fp> >, optional>::value,
                      "std::optional::or_else of a function not returning the same std::optional");
        if (this->__engaged) {
            return *this;
        }
        return std::forward<_Fp>(__f)();
    }

    /* [optional.mod], modifiers */

    constexpr void reset() noexcept {
        this->__reset();
    }

private:
    template <class _Fp, class... _Args>
    constexpr optional(__fcxx_optional_invoke_tag __tag, _Fp&& __f, _Args&&... __args)
        : _Base(__tag, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}

    /* The value of an optional of any value category, as the monadic
     * operations take it. */
    template <class _Self>
    static constexpr decltype(auto) __value_of(_Self&& __self) noexcept {
        if constexpr (is_lvalue_reference<_Self>::value) {
            return (__self.__get());
        } else {
            return std::move(__self.__get());
        }
    }

    template <class _Self, class _Fp>
    static constexpr auto __and_then(_Self&& __self, _Fp&& __f) {
        typedef invoke_result_t<_Fp, decltype(optional::__value_of(std::forward<_Self>(__self)))> _Up;
        static_assert(__fcxx_is_optional<__fcxx_remove_cvref_t<_Up> >::value,
                      "std::optional::and_then of a function not returning a std::optional");
        if (__self.__engaged) {
            return std::__fcxx_invoke(std::forward<_Fp>(__f), optional::__value_of(std::forward<_Self>(__self)));
        }
        return __fcxx_remove_cvref_t<_Up>();
    }

    template <class _Self, class _Fp>
    static constexpr auto __transform(_Self&& __self, _Fp&& __f) {
        typedef __fcxx_remove_cv_t<invoke_result_t<_Fp, decltype(optional::__value_of(std::forward<_Self>(__self)))> >
            _Up;
        static_assert(__fcxx_optional_valid<_Up>::value,
                      "std::optional::transform of a function returning what std::optional cannot hold");
        if (__self.__engaged) {
            return optional<_Up>(__fcxx_optional_invoke_tag(), std::forward<_Fp>(__f),
                                 optional::__value_of(std::forward<_Self>(__self)));
        }
        return optional<_Up>();
    }
};

/* [optional.optional.ref], partial specialization of optional for lvalue
 * reference types */

/* The iterator type, only for the types of objects other than arrays of
 * unknown bound. */
template <class _Tp, bool = is_object<_Tp>::value && !is_unbounded_array<_Tp>::value>
struct __fcxx_optional_ref_iterator {};

template <class _Tp>
struct __fcxx_optional_ref_iterator<_Tp, true> {
    using iterator = _Tp*;
};

/* The return type of value_or, which is left unspecified where the value
 * cannot be returned. */
template <class _Tp, bool = is_object<_Tp>::value && !is_array<_Tp>::value>
struct __fcxx_optional_ref_value {
    using type = void;
};

template <class _Tp>
struct __fcxx_optional_ref_value<_Tp, true> {
    using type = __fcxx_remove_cv_t<_Tp>;
};

template <class _Tp, class _Up>
concept __fcxx_optional_ref_dangles = __fcxx_reference_constructs_from_temporary<_Tp&, _Up>::value;

template <class _Tp>
class optional<_Tp&> : public __fcxx_optional_ref_iterator<_Tp> {
    template <class _Up>
    friend class optional;

    _Tp* __ptr = nullptr;

    template <class _Up>
    constexpr void __convert_ref_init_val(_Up&& __u) {
        _Tp& __r(std::forward<_Up>(__u));
        __ptr = std::addressof(__r);
    }

    template <class _Fp, class... _Args>
    constexpr optional(__fcxx_optional_invoke_tag, _Fp&& __f, _Args&&... __args) {
        __convert_ref_init_val(std::__fcxx_invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
    }

public:
    using value_type = _Tp;

    /* [optional.ref.ctor], constructors */

    constexpr optional() noexcept = default;

    constexpr optional(nullopt_t) noexcept : optional() {}

    constexpr optional(const optional&) noexcept = default;

    template <class _Arg>
        requires (is_constructible<_Tp&, _Arg>::value && !__fcxx_optional_ref_dangles<_Tp, _Arg>)
    constexpr explicit optional(in_place_t, _Arg&& __arg) {
        __convert_ref_init_val(std::forward<_Arg>(__arg));
    }

    template <class _Up>
        requires (!is_same<__fcxx_remove_cvref_t<_Up>, optional>::value
                  && !is_same<__fcxx_remove_cvref_t<_Up>, in_place_t>::value && is_constructible<_Tp&, _Up>::value
                  && !__fcxx_optional_ref_dangles<_Tp, _Up>)
    constexpr explicit(!is_convertible<_Up, _Tp&>::value) optional(_Up&& __u)
        noexcept(is_nothrow_constructible<_Tp&, _Up>::value) {
        __convert_ref_init_val(std::forward<_Up>(__u));
    }

    template <class _Up>
        requires (!is_same<__fcxx_remove_cvref_t<_Up>, optional>::value
                  && !is_same<__fcxx_remove_cvref_t<_Up>, in_place_t>::value && is_constructible<_Tp&, _Up>::value
                  && __fcxx_optional_ref_dangles<_Tp, _Up>)
    constexpr explicit(!is_convertible<_Up, _Tp&>::value) optional(_Up&&)
        noexcept(is_nothrow_constructible<_Tp&, _Up>::value) = delete;

    template <class _Up>
        requires (!is_same<__fcxx_remove_cv_t<_Tp>, optional<_Up> >::value && !is_same<_Tp&, _Up>::value
                  && is_constructible<_Tp&, _Up&>::value && !__fcxx_optional_ref_dangles<_Tp, _Up&>)
    constexpr explicit(!is_convertible<_Up&, _Tp&>::value) optional(optional<_Up>& __rhs)
        noexcept(is_nothrow_constructible<_Tp&, _Up&>::value) {
        if (__rhs.has_value()) {
            __convert_ref_init_val(__rhs.operator*());
        }
    }

    template <class _Up>
        requires (!is_same<__fcxx_remove_cv_t<_Tp>, optional<_Up> >::value && !is_same<_Tp&, _Up>::value
                  && is_constructible<_Tp&, _Up&>::value && __fcxx_optional_ref_dangles<_Tp, _Up&>)
    constexpr explicit(!is_convertible<_Up&, _Tp&>::value) optional(optional<_Up>&)
        noexcept(is_nothrow_constructible<_Tp&, _Up&>::value) = delete;

    template <class _Up>
        requires (!is_same<__fcxx_remove_cv_t<_Tp>, optional<_Up> >::value && !is_same<_Tp&, _Up>::value
                  && is_constructible<_Tp&, const _Up&>::value && !__fcxx_optional_ref_dangles<_Tp, const _Up&>)
    constexpr explicit(!is_convertible<const _Up&, _Tp&>::value) optional(const optional<_Up>& __rhs)
        noexcept(is_nothrow_constructible<_Tp&, const _Up&>::value) {
        if (__rhs.has_value()) {
            __convert_ref_init_val(__rhs.operator*());
        }
    }

    template <class _Up>
        requires (!is_same<__fcxx_remove_cv_t<_Tp>, optional<_Up> >::value && !is_same<_Tp&, _Up>::value
                  && is_constructible<_Tp&, const _Up&>::value && __fcxx_optional_ref_dangles<_Tp, const _Up&>)
    constexpr explicit(!is_convertible<const _Up&, _Tp&>::value) optional(const optional<_Up>&)
        noexcept(is_nothrow_constructible<_Tp&, const _Up&>::value) = delete;

    template <class _Up>
        requires (!is_same<__fcxx_remove_cv_t<_Tp>, optional<_Up> >::value && !is_same<_Tp&, _Up>::value
                  && is_constructible<_Tp&, _Up>::value && !__fcxx_optional_ref_dangles<_Tp, _Up>)
    constexpr explicit(!is_convertible<_Up, _Tp&>::value) optional(optional<_Up>&& __rhs)
        noexcept(is_nothrow_constructible<_Tp&, _Up>::value) {
        if (__rhs.has_value()) {
            __convert_ref_init_val(std::move(__rhs).operator*());
        }
    }

    template <class _Up>
        requires (!is_same<__fcxx_remove_cv_t<_Tp>, optional<_Up> >::value && !is_same<_Tp&, _Up>::value
                  && is_constructible<_Tp&, _Up>::value && __fcxx_optional_ref_dangles<_Tp, _Up>)
    constexpr explicit(!is_convertible<_Up, _Tp&>::value) optional(optional<_Up>&&)
        noexcept(is_nothrow_constructible<_Tp&, _Up>::value) = delete;

    template <class _Up>
        requires (!is_same<__fcxx_remove_cv_t<_Tp>, optional<_Up> >::value && !is_same<_Tp&, _Up>::value
                  && is_constructible<_Tp&, const _Up>::value && !__fcxx_optional_ref_dangles<_Tp, const _Up>)
    constexpr explicit(!is_convertible<const _Up, _Tp&>::value) optional(const optional<_Up>&& __rhs)
        noexcept(is_nothrow_constructible<_Tp&, const _Up>::value) {
        if (__rhs.has_value()) {
            __convert_ref_init_val(std::move(__rhs).operator*());
        }
    }

    template <class _Up>
        requires (!is_same<__fcxx_remove_cv_t<_Tp>, optional<_Up> >::value && !is_same<_Tp&, _Up>::value
                  && is_constructible<_Tp&, const _Up>::value && __fcxx_optional_ref_dangles<_Tp, const _Up>)
    constexpr explicit(!is_convertible<const _Up, _Tp&>::value) optional(const optional<_Up>&&)
        noexcept(is_nothrow_constructible<_Tp&, const _Up>::value) = delete;

    constexpr ~optional() = default;

    /* [optional.ref.assign], assignment */

    constexpr optional& operator=(nullopt_t) noexcept {
        __ptr = nullptr;
        return *this;
    }

    constexpr optional& operator=(const optional&) noexcept = default;

    template <class _Up>
        requires (is_constructible<_Tp&, _Up>::value && !__fcxx_optional_ref_dangles<_Tp, _Up>)
    constexpr _Tp& emplace(_Up&& __u) noexcept(is_nothrow_constructible<_Tp&, _Up>::value) {
        __convert_ref_init_val(std::forward<_Up>(__u));
        return *__ptr;
    }

    /* [optional.ref.swap], swap */

    constexpr void swap(optional& __rhs) noexcept {
        _Tp* __tmp = __ptr;
        __ptr = __rhs.__ptr;
        __rhs.__ptr = __tmp;
    }

    /* [optional.ref.iterators], iterator support */

    constexpr auto begin() const noexcept
        requires (is_object<_Tp>::value && !is_unbounded_array<_Tp>::value)
    {
        return typename optional::iterator(__ptr);
    }

    constexpr auto end() const noexcept
        requires (is_object<_Tp>::value && !is_unbounded_array<_Tp>::value)
    {
        return begin() + has_value();
    }

    /* [optional.ref.observe], observers */

    constexpr _Tp* operator->() const noexcept {
        return __ptr;
    }

    constexpr _Tp& operator*() const noexcept {
        return *__ptr;
    }

    constexpr explicit operator bool() const noexcept {
        return __ptr != nullptr;
    }

    constexpr bool has_value() const noexcept {
        return __ptr != nullptr;
    }

    /* freestanding-deleted */
    constexpr _Tp& value() const = delete;

    template <class _Up = __fcxx_remove_cv_t<_Tp> >
        requires (is_object<_Tp>::value && !is_array<_Tp>::value)
    constexpr typename __fcxx_optional_ref_value<_Tp>::type value_or(_Up&& __u) const {
        typedef __fcxx_remove_cv_t<_Tp> _Xp;
        static_assert(is_constructible<_Xp, _Tp&>::value && is_convertible<_Up, _Xp>::value,
                      "std::optional::value_or of a value that cannot be copied or converted to");
        if (has_value()) {
            return *__ptr;
        }
        return static_cast<_Xp>(std::forward<_Up>(__u));
    }

    /* [optional.ref.monadic], monadic operations */

    template <class _Fp>
    constexpr auto and_then(_Fp&& __f) const {
        typedef invoke_result_t<_Fp, _Tp&> _Up;
        static_assert(__fcxx_is_optional<__fcxx_remove_cvref_t<_Up> >::value,
                      "std::optional::and_then of a function not returning a std::optional");
        if (has_value()) {
            return std::__fcxx_invoke(std::forward<_Fp>(__f), *__ptr);
        }
        return __fcxx_remove_cvref_t<_Up>();
    }

    template <class _Fp>
    constexpr optional<__fcxx_remove_cv_t<invoke_result_t<_Fp, _Tp&> > > transform(_Fp&& __f) const {
        typedef __fcxx_remove_cv_t<invoke_result_t<_Fp, _Tp&> > _Up;
        static_assert(__fcxx_optional_valid<_Up>::value,
                      "std::optional::transform of a function returning what std::optional cannot hold");
        if (has_value()) {
            return optional<_Up>(__fcxx_optional_invoke_tag(), std::forward<_Fp>(__f), *__ptr);
        }
        return optional<_Up>();
    }

    template <class _Fp>
        requires invocable<_Fp>
    constexpr optional or_else(_Fp&& __f) const {
        static_assert(is_same<__fcxx_remove_cvref_t<invoke_result_t<_Fp> >, optional>::value,
                      "std::optional::or_else of a function not returning the same std::optional");
        if (has_value()) {
            return *__ptr;
        }
        return std::forward<_Fp>(__f)();
    }

    /* [optional.ref.mod], modifiers */

    constexpr void reset() noexcept {
        __ptr = nullptr;
    }
};

namespace ranges {

template <class _Tp>
inline constexpr bool enable_view<optional<_Tp> > = true;

template <class _Tp>
inline constexpr bool enable_borrowed_range<optional<_Tp&> > = true;

} /* namespace ranges */

template <class _Tp>
optional(_Tp) -> optional<_Tp>;

/* [optional.relops], relational operators, constrained from C++26 on */

template <class _Tp, class _Up>
    requires (__fcxx_eq_comparable<_Tp, _Up>)
constexpr bool operator==(const optional<_Tp>& __x, const optional<_Up>& __y) {
    if (__x.has_value() != __y.has_value()) {
        return false;
    }
    if (!__x.has_value()) {
        return true;
    }
    return *__x == *__y;
}

template <class _Tp, class _Up>
    requires (__fcxx_ne_comparable<_Tp, _Up>)
constexpr bool operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) {
    if (__x.has_value() != __y.has_value()) {
        return true;
    }
    if (!__x.has_value()) {
        return false;
    }
    return *__x != *__y;
}

template <class _Tp, class _Up>
    requires (__fcxx_lt_comparable<_Tp, _Up>)
constexpr bool operator<(const optional<_Tp>& __x, const optional<_Up>& __y) {
    if (!__y.has_value()) {
        return false;
    }
    if (!__x.has_value()) {
        return true;
    }
    return *__x < *__y;
}

template <class _Tp, class _Up>
    requires (__fcxx_gt_comparable<_Tp, _Up>)
constexpr bool operator>(const optional<_Tp>& __x, const optional<_Up>& __y) {
    if (!__x.has_value()) {
        return false;
    }
    if (!__y.has_value()) {
        return true;
    }
    return *__x > *__y;
}

template <class _Tp, class _Up>
    requires (__fcxx_le_comparable<_Tp, _Up>)
constexpr bool operator<=(const optional<_Tp>& __x, const optional<_Up>& __y) {
    if (!__x.has_value()) {
        return true;
    }
    if (!__y.has_value()) {
        return false;
    }
    return *__x <= *__y;
}

template <class _Tp, class _Up>
    requires (__fcxx_ge_comparable<_Tp, _Up>)
constexpr bool operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) {
    if (!__y.has_value()) {
        return true;
    }
    if (!__x.has_value()) {
        return false;
    }
    return *__x >= *__y;
}

/* The values are compared with the __fcxx_three_way of <compare>, which is
 * their <=> but for floating-point values with some versions of GCC.
 * Workaround: GCC 14 to 16, all targets, from -O2 on: <=> of floating-point
 * values evaluated with other comparisons of the same values in a function,
 * as after inlining, is taken to be equivalent where they are unordered, and
 * so can the other comparisons be (see __fcxx_three_way). */
template <class _Tp, three_way_comparable_with<_Tp> _Up>
constexpr compare_three_way_result_t<_Tp, _Up> operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) {
    if (__x.has_value() && __y.has_value()) {
        return std::__fcxx_three_way(*__x, *__y);
    }
    return __x.has_value() <=> __y.has_value();
}

/* [optional.nullops], comparison with nullopt, of which C++20 only keeps
 * == and <=> */

template <class _Tp>
constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept {
    return !__x.has_value();
}

template <class _Tp>
constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept {
    return __x.has_value() <=> false;
}

/* [optional.comp.with.t], comparison with T, whose requirements on the
 * comparisons of the values are constraints, along with that the value is
 * no optional, which keep the rewritten comparisons from taking, say,
 * "nullopt == o" to compare the value of o with nullopt. They are
 * requires-clauses, as conditions on the result type would leave the
 * operators that take the optional first less constrained than the <=> that
 * does, which "o < v" would then be rewritten to call in place of <. */
#define _FCXX_OPTIONAL_CMP_OK(name, op) \
    template <class _Tp, class _Up, class = void> \
    struct name : false_type {}; \
    template <class _Tp, class _Up> \
    struct name<_Tp, _Up, __fcxx_void_t<decltype(std::declval<const _Tp&>() op std::declval<const _Up&>())> > \
        : is_convertible<decltype(std::declval<const _Tp&>() op std::declval<const _Up&>()), bool> {};
_FCXX_OPTIONAL_CMP_OK(__fcxx_optional_eq_ok, ==)
_FCXX_OPTIONAL_CMP_OK(__fcxx_optional_ne_ok, !=)
_FCXX_OPTIONAL_CMP_OK(__fcxx_optional_lt_ok, <)
_FCXX_OPTIONAL_CMP_OK(__fcxx_optional_gt_ok, >)
_FCXX_OPTIONAL_CMP_OK(__fcxx_optional_le_ok, <=)
_FCXX_OPTIONAL_CMP_OK(__fcxx_optional_ge_ok, >=)
#undef _FCXX_OPTIONAL_CMP_OK

template <class _Ok, class _Value>
struct __fcxx_optional_cmp_with
    : __fcxx_and<__fcxx_not<__fcxx_is_optional<_Value> >, _Ok> {
};

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_eq_ok<_Tp, _Up>, _Up>::value)
constexpr bool operator==(const optional<_Tp>& __x, const _Up& __v) {
    if (__x.has_value()) {
        return *__x == __v;
    }
    return false;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_eq_ok<_Tp, _Up>, _Tp>::value)
constexpr bool operator==(const _Tp& __v, const optional<_Up>& __x) {
    if (__x.has_value()) {
        return __v == *__x;
    }
    return false;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_ne_ok<_Tp, _Up>, _Up>::value)
constexpr bool operator!=(const optional<_Tp>& __x, const _Up& __v) {
    if (__x.has_value()) {
        return *__x != __v;
    }
    return true;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_ne_ok<_Tp, _Up>, _Tp>::value)
constexpr bool operator!=(const _Tp& __v, const optional<_Up>& __x) {
    if (__x.has_value()) {
        return __v != *__x;
    }
    return true;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_lt_ok<_Tp, _Up>, _Up>::value)
constexpr bool operator<(const optional<_Tp>& __x, const _Up& __v) {
    if (__x.has_value()) {
        return *__x < __v;
    }
    return true;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_lt_ok<_Tp, _Up>, _Tp>::value)
constexpr bool operator<(const _Tp& __v, const optional<_Up>& __x) {
    if (__x.has_value()) {
        return __v < *__x;
    }
    return false;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_gt_ok<_Tp, _Up>, _Up>::value)
constexpr bool operator>(const optional<_Tp>& __x, const _Up& __v) {
    if (__x.has_value()) {
        return *__x > __v;
    }
    return false;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_gt_ok<_Tp, _Up>, _Tp>::value)
constexpr bool operator>(const _Tp& __v, const optional<_Up>& __x) {
    if (__x.has_value()) {
        return __v > *__x;
    }
    return true;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_le_ok<_Tp, _Up>, _Up>::value)
constexpr bool operator<=(const optional<_Tp>& __x, const _Up& __v) {
    if (__x.has_value()) {
        return *__x <= __v;
    }
    return true;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_le_ok<_Tp, _Up>, _Tp>::value)
constexpr bool operator<=(const _Tp& __v, const optional<_Up>& __x) {
    if (__x.has_value()) {
        return __v <= *__x;
    }
    return false;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_ge_ok<_Tp, _Up>, _Up>::value)
constexpr bool operator>=(const optional<_Tp>& __x, const _Up& __v) {
    if (__x.has_value()) {
        return *__x >= __v;
    }
    return false;
}

template <class _Tp, class _Up>
    requires (__fcxx_optional_cmp_with<__fcxx_optional_ge_ok<_Tp, _Up>, _Tp>::value)
constexpr bool operator>=(const _Tp& __v, const optional<_Up>& __x) {
    if (__x.has_value()) {
        return __v >= *__x;
    }
    return true;
}

/* Whether a type is optional or derives from it, which the comparison with
 * a value leaves out lest it recurse, as C++23 has it. */
template <class _Up>
void __fcxx_optional_base_test(const optional<_Up>&);

template <class _Tp>
concept __fcxx_derived_from_optional = requires(const _Tp& __t) { std::__fcxx_optional_base_test(__t); };

/* The value is compared with __fcxx_three_way, as above. */
template <class _Tp, class _Up>
    requires (!__fcxx_derived_from_optional<_Up> && three_way_comparable_with<_Tp, _Up>)
constexpr compare_three_way_result_t<_Tp, _Up> operator<=>(const optional<_Tp>& __x, const _Up& __v) {
    if (__x.has_value()) {
        return std::__fcxx_three_way(*__x, __v);
    }
    return strong_ordering::less;
}

/* [optional.specalg], specialized algorithms */

template <class _Tp>
constexpr __fcxx_enable_if_t<
    is_reference<_Tp>::value ||
    (is_move_constructible<_Tp>::value && __fcxx_is_swappable<_Tp>::value)>
swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
    __x.swap(__y);
}

/* From C++26 on, the first form is not used where a type is given as the
 * first template argument, for which the second then makes an optional
 * holding a reference. */
template <int&... _NoTypeArguments, class _Tp>
constexpr optional<__fcxx_decay_t<_Tp> > make_optional(_Tp&& __v) {
    return optional<__fcxx_decay_t<_Tp> >(std::forward<_Tp>(__v));
}

template <class _Tp, class... _Args>
constexpr optional<_Tp> make_optional(_Args&&... __args) {
    return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
}

template <class _Tp, class _Up, class... _Args>
constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) {
    return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
}

/* [optional.hash], hash support, enabled where that of the type is */

template <class _Tp, bool = __fcxx_is_hash_enabled<typename remove_const<_Tp>::type>::value>
struct __fcxx_optional_hash : __fcxx_disabled_hash {};

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

template <class _Tp>
struct __fcxx_optional_hash<_Tp, true> {

    size_t operator()(const optional<_Tp>& __o) const {
        if (__o.has_value()) {
            return hash<typename remove_const<_Tp>::type>()(*__o);
        }
        return static_cast<size_t>(0x6f7074);
    }
};

#pragma GCC diagnostic pop

template <class _Tp>
struct hash<optional<_Tp> > : __fcxx_optional_hash<_Tp> {};

} /* namespace std */

#endif

#endif
