10b57cec5SDimitry Andric// -*- C++ -*- 2*349cc55cSDimitry 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> 960b57cec5SDimitry Andric constexpr EXPLICIT optional(U &&); 970b57cec5SDimitry Andric template <class U> 98fe6060f1SDimitry Andric EXPLICIT optional(const optional<U> &); // constexpr in C++20 990b57cec5SDimitry Andric template <class U> 100fe6060f1SDimitry Andric EXPLICIT 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 1350b57cec5SDimitry Andric // 23.6.3.6, modifiers 136fe6060f1SDimitry Andric void reset() noexcept; // constexpr in C++20 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric private: 1390b57cec5SDimitry Andric T *val; // exposition only 1400b57cec5SDimitry Andric }; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andrictemplate<class T> 1430b57cec5SDimitry Andric optional(T) -> optional<T>; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric} // namespace std 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric*/ 1480b57cec5SDimitry Andric 149e8d8bef9SDimitry Andric#include <__availability> 150fe6060f1SDimitry Andric#include <__config> 1510b57cec5SDimitry Andric#include <__debug> 1520b57cec5SDimitry Andric#include <__functional_base> 153fe6060f1SDimitry Andric#include <compare> 1540b57cec5SDimitry Andric#include <functional> 1550b57cec5SDimitry Andric#include <initializer_list> 1560b57cec5SDimitry Andric#include <new> 1570b57cec5SDimitry Andric#include <stdexcept> 1580b57cec5SDimitry Andric#include <type_traits> 1590b57cec5SDimitry Andric#include <utility> 1600b57cec5SDimitry Andric#include <version> 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1630b57cec5SDimitry Andric#pragma GCC system_header 1640b57cec5SDimitry Andric#endif 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andricnamespace std // purposefully not using versioning namespace 1670b57cec5SDimitry Andric{ 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access 1700b57cec5SDimitry Andric : public exception 1710b57cec5SDimitry Andric{ 1720b57cec5SDimitry Andricpublic: 1730b57cec5SDimitry Andric // Get the key function ~bad_optional_access() into the dylib 1740b57cec5SDimitry Andric virtual ~bad_optional_access() _NOEXCEPT; 1750b57cec5SDimitry Andric virtual const char* what() const _NOEXCEPT; 1760b57cec5SDimitry Andric}; 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric} // std 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric_LIBCPP_NORETURN 1850b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 1860b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS 1870b57cec5SDimitry Andricvoid __throw_bad_optional_access() { 1880b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 1890b57cec5SDimitry Andric throw bad_optional_access(); 1900b57cec5SDimitry Andric#else 1910b57cec5SDimitry Andric _VSTD::abort(); 1920b57cec5SDimitry Andric#endif 1930b57cec5SDimitry Andric} 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andricstruct nullopt_t 1960b57cec5SDimitry Andric{ 1970b57cec5SDimitry Andric struct __secret_tag { _LIBCPP_INLINE_VISIBILITY explicit __secret_tag() = default; }; 1980b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {} 1990b57cec5SDimitry Andric}; 2000b57cec5SDimitry Andric 201*349cc55cSDimitry Andricinline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}}; 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_destructible<_Tp>::value> 2040b57cec5SDimitry Andricstruct __optional_destruct_base; 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andrictemplate <class _Tp> 2070b57cec5SDimitry Andricstruct __optional_destruct_base<_Tp, false> 2080b57cec5SDimitry Andric{ 2090b57cec5SDimitry Andric typedef _Tp value_type; 2100b57cec5SDimitry Andric static_assert(is_object_v<value_type>, 2110b57cec5SDimitry Andric "instantiation of optional with a non-object type is undefined behavior"); 2120b57cec5SDimitry Andric union 2130b57cec5SDimitry Andric { 2140b57cec5SDimitry Andric char __null_state_; 2150b57cec5SDimitry Andric value_type __val_; 2160b57cec5SDimitry Andric }; 2170b57cec5SDimitry Andric bool __engaged_; 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 220fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 ~__optional_destruct_base() 2210b57cec5SDimitry Andric { 2220b57cec5SDimitry Andric if (__engaged_) 2230b57cec5SDimitry Andric __val_.~value_type(); 2240b57cec5SDimitry Andric } 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2270b57cec5SDimitry Andric constexpr __optional_destruct_base() noexcept 2280b57cec5SDimitry Andric : __null_state_(), 2290b57cec5SDimitry Andric __engaged_(false) {} 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric template <class... _Args> 2320b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2330b57cec5SDimitry Andric constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args) 2340b57cec5SDimitry Andric : __val_(_VSTD::forward<_Args>(__args)...), 2350b57cec5SDimitry Andric __engaged_(true) {} 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 238fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept 2390b57cec5SDimitry Andric { 2400b57cec5SDimitry Andric if (__engaged_) 2410b57cec5SDimitry Andric { 2420b57cec5SDimitry Andric __val_.~value_type(); 2430b57cec5SDimitry Andric __engaged_ = false; 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric}; 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andrictemplate <class _Tp> 2490b57cec5SDimitry Andricstruct __optional_destruct_base<_Tp, true> 2500b57cec5SDimitry Andric{ 2510b57cec5SDimitry Andric typedef _Tp value_type; 2520b57cec5SDimitry Andric static_assert(is_object_v<value_type>, 2530b57cec5SDimitry Andric "instantiation of optional with a non-object type is undefined behavior"); 2540b57cec5SDimitry Andric union 2550b57cec5SDimitry Andric { 2560b57cec5SDimitry Andric char __null_state_; 2570b57cec5SDimitry Andric value_type __val_; 2580b57cec5SDimitry Andric }; 2590b57cec5SDimitry Andric bool __engaged_; 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2620b57cec5SDimitry Andric constexpr __optional_destruct_base() noexcept 2630b57cec5SDimitry Andric : __null_state_(), 2640b57cec5SDimitry Andric __engaged_(false) {} 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric template <class... _Args> 2670b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2680b57cec5SDimitry Andric constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args) 2690b57cec5SDimitry Andric : __val_(_VSTD::forward<_Args>(__args)...), 2700b57cec5SDimitry Andric __engaged_(true) {} 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 273fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept 2740b57cec5SDimitry Andric { 2750b57cec5SDimitry Andric if (__engaged_) 2760b57cec5SDimitry Andric { 2770b57cec5SDimitry Andric __engaged_ = false; 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric}; 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andrictemplate <class _Tp, bool = is_reference<_Tp>::value> 2830b57cec5SDimitry Andricstruct __optional_storage_base : __optional_destruct_base<_Tp> 2840b57cec5SDimitry Andric{ 2850b57cec5SDimitry Andric using __base = __optional_destruct_base<_Tp>; 2860b57cec5SDimitry Andric using value_type = _Tp; 2870b57cec5SDimitry Andric using __base::__base; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2900b57cec5SDimitry Andric constexpr bool has_value() const noexcept 2910b57cec5SDimitry Andric { 2920b57cec5SDimitry Andric return this->__engaged_; 2930b57cec5SDimitry Andric } 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2960b57cec5SDimitry Andric constexpr value_type& __get() & noexcept 2970b57cec5SDimitry Andric { 2980b57cec5SDimitry Andric return this->__val_; 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3010b57cec5SDimitry Andric constexpr const value_type& __get() const& noexcept 3020b57cec5SDimitry Andric { 3030b57cec5SDimitry Andric return this->__val_; 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3060b57cec5SDimitry Andric constexpr value_type&& __get() && noexcept 3070b57cec5SDimitry Andric { 3080b57cec5SDimitry Andric return _VSTD::move(this->__val_); 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3110b57cec5SDimitry Andric constexpr const value_type&& __get() const&& noexcept 3120b57cec5SDimitry Andric { 3130b57cec5SDimitry Andric return _VSTD::move(this->__val_); 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric template <class... _Args> 3170b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 318fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct(_Args&&... __args) 3190b57cec5SDimitry Andric { 3200b57cec5SDimitry Andric _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage"); 321fe6060f1SDimitry Andric#if _LIBCPP_STD_VER > 17 322fe6060f1SDimitry Andric _VSTD::construct_at(_VSTD::addressof(this->__val_), _VSTD::forward<_Args>(__args)...); 323fe6060f1SDimitry Andric#else 3240b57cec5SDimitry Andric ::new ((void*)_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Args>(__args)...); 325fe6060f1SDimitry Andric#endif 3260b57cec5SDimitry Andric this->__engaged_ = true; 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric template <class _That> 3300b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 331fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct_from(_That&& __opt) 3320b57cec5SDimitry Andric { 3330b57cec5SDimitry Andric if (__opt.has_value()) 3340b57cec5SDimitry Andric __construct(_VSTD::forward<_That>(__opt).__get()); 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric template <class _That> 3380b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 339fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void __assign_from(_That&& __opt) 3400b57cec5SDimitry Andric { 3410b57cec5SDimitry Andric if (this->__engaged_ == __opt.has_value()) 3420b57cec5SDimitry Andric { 3430b57cec5SDimitry Andric if (this->__engaged_) 3440b57cec5SDimitry Andric this->__val_ = _VSTD::forward<_That>(__opt).__get(); 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric else 3470b57cec5SDimitry Andric { 3480b57cec5SDimitry Andric if (this->__engaged_) 3490b57cec5SDimitry Andric this->reset(); 3500b57cec5SDimitry Andric else 3510b57cec5SDimitry Andric __construct(_VSTD::forward<_That>(__opt).__get()); 3520b57cec5SDimitry Andric } 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric}; 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric// optional<T&> is currently required ill-formed, however it may to be in the 3570b57cec5SDimitry Andric// future. For this reason it has already been implemented to ensure we can 3580b57cec5SDimitry Andric// make the change in an ABI compatible manner. 3590b57cec5SDimitry Andrictemplate <class _Tp> 3600b57cec5SDimitry Andricstruct __optional_storage_base<_Tp, true> 3610b57cec5SDimitry Andric{ 3620b57cec5SDimitry Andric using value_type = _Tp; 3630b57cec5SDimitry Andric using __raw_type = remove_reference_t<_Tp>; 3640b57cec5SDimitry Andric __raw_type* __value_; 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric template <class _Up> 3670b57cec5SDimitry Andric static constexpr bool __can_bind_reference() { 3680b57cec5SDimitry Andric using _RawUp = typename remove_reference<_Up>::type; 3690b57cec5SDimitry Andric using _UpPtr = _RawUp*; 3700b57cec5SDimitry Andric using _RawTp = typename remove_reference<_Tp>::type; 3710b57cec5SDimitry Andric using _TpPtr = _RawTp*; 3720b57cec5SDimitry Andric using _CheckLValueArg = integral_constant<bool, 3730b57cec5SDimitry Andric (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value) 3740b57cec5SDimitry Andric || is_same<_RawUp, reference_wrapper<_RawTp>>::value 3750b57cec5SDimitry Andric || is_same<_RawUp, reference_wrapper<typename remove_const<_RawTp>::type>>::value 3760b57cec5SDimitry Andric >; 3770b57cec5SDimitry Andric return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value) 3780b57cec5SDimitry Andric || (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value && 3790b57cec5SDimitry Andric is_convertible<_UpPtr, _TpPtr>::value); 3800b57cec5SDimitry Andric } 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3830b57cec5SDimitry Andric constexpr __optional_storage_base() noexcept 3840b57cec5SDimitry Andric : __value_(nullptr) {} 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric template <class _UArg> 3870b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3880b57cec5SDimitry Andric constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg) 3890b57cec5SDimitry Andric : __value_(_VSTD::addressof(__uarg)) 3900b57cec5SDimitry Andric { 3910b57cec5SDimitry Andric static_assert(__can_bind_reference<_UArg>(), 3920b57cec5SDimitry Andric "Attempted to construct a reference element in tuple from a " 3930b57cec5SDimitry Andric "possible temporary"); 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 397fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept { __value_ = nullptr; } 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4000b57cec5SDimitry Andric constexpr bool has_value() const noexcept 4010b57cec5SDimitry Andric { return __value_ != nullptr; } 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4040b57cec5SDimitry Andric constexpr value_type& __get() const& noexcept 4050b57cec5SDimitry Andric { return *__value_; } 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4080b57cec5SDimitry Andric constexpr value_type&& __get() const&& noexcept 4090b57cec5SDimitry Andric { return _VSTD::forward<value_type>(*__value_); } 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric template <class _UArg> 4120b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 413fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct(_UArg&& __val) 4140b57cec5SDimitry Andric { 4150b57cec5SDimitry Andric _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage"); 4160b57cec5SDimitry Andric static_assert(__can_bind_reference<_UArg>(), 4170b57cec5SDimitry Andric "Attempted to construct a reference element in tuple from a " 4180b57cec5SDimitry Andric "possible temporary"); 4190b57cec5SDimitry Andric __value_ = _VSTD::addressof(__val); 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric template <class _That> 4230b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 424fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct_from(_That&& __opt) 4250b57cec5SDimitry Andric { 4260b57cec5SDimitry Andric if (__opt.has_value()) 4270b57cec5SDimitry Andric __construct(_VSTD::forward<_That>(__opt).__get()); 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric template <class _That> 4310b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 432fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void __assign_from(_That&& __opt) 4330b57cec5SDimitry Andric { 4340b57cec5SDimitry Andric if (has_value() == __opt.has_value()) 4350b57cec5SDimitry Andric { 4360b57cec5SDimitry Andric if (has_value()) 4370b57cec5SDimitry Andric *__value_ = _VSTD::forward<_That>(__opt).__get(); 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric else 4400b57cec5SDimitry Andric { 4410b57cec5SDimitry Andric if (has_value()) 4420b57cec5SDimitry Andric reset(); 4430b57cec5SDimitry Andric else 4440b57cec5SDimitry Andric __construct(_VSTD::forward<_That>(__opt).__get()); 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric}; 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value> 4500b57cec5SDimitry Andricstruct __optional_copy_base : __optional_storage_base<_Tp> 4510b57cec5SDimitry Andric{ 4520b57cec5SDimitry Andric using __optional_storage_base<_Tp>::__optional_storage_base; 4530b57cec5SDimitry Andric}; 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andrictemplate <class _Tp> 4560b57cec5SDimitry Andricstruct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> 4570b57cec5SDimitry Andric{ 4580b57cec5SDimitry Andric using __optional_storage_base<_Tp>::__optional_storage_base; 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4610b57cec5SDimitry Andric __optional_copy_base() = default; 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 464fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_copy_base(const __optional_copy_base& __opt) 4650b57cec5SDimitry Andric { 4660b57cec5SDimitry Andric this->__construct_from(__opt); 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4700b57cec5SDimitry Andric __optional_copy_base(__optional_copy_base&&) = default; 4710b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4720b57cec5SDimitry Andric __optional_copy_base& operator=(const __optional_copy_base&) = default; 4730b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4740b57cec5SDimitry Andric __optional_copy_base& operator=(__optional_copy_base&&) = default; 4750b57cec5SDimitry Andric}; 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andrictemplate <class _Tp, bool = is_trivially_move_constructible<_Tp>::value> 4780b57cec5SDimitry Andricstruct __optional_move_base : __optional_copy_base<_Tp> 4790b57cec5SDimitry Andric{ 4800b57cec5SDimitry Andric using __optional_copy_base<_Tp>::__optional_copy_base; 4810b57cec5SDimitry Andric}; 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andrictemplate <class _Tp> 4840b57cec5SDimitry Andricstruct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> 4850b57cec5SDimitry Andric{ 4860b57cec5SDimitry Andric using value_type = _Tp; 4870b57cec5SDimitry Andric using __optional_copy_base<_Tp>::__optional_copy_base; 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4900b57cec5SDimitry Andric __optional_move_base() = default; 4910b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4920b57cec5SDimitry Andric __optional_move_base(const __optional_move_base&) = default; 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 495fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_move_base(__optional_move_base&& __opt) 4960b57cec5SDimitry Andric noexcept(is_nothrow_move_constructible_v<value_type>) 4970b57cec5SDimitry Andric { 4980b57cec5SDimitry Andric this->__construct_from(_VSTD::move(__opt)); 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5020b57cec5SDimitry Andric __optional_move_base& operator=(const __optional_move_base&) = default; 5030b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5040b57cec5SDimitry Andric __optional_move_base& operator=(__optional_move_base&&) = default; 5050b57cec5SDimitry Andric}; 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andrictemplate <class _Tp, bool = 5080b57cec5SDimitry Andric is_trivially_destructible<_Tp>::value && 5090b57cec5SDimitry Andric is_trivially_copy_constructible<_Tp>::value && 5100b57cec5SDimitry Andric is_trivially_copy_assignable<_Tp>::value> 5110b57cec5SDimitry Andricstruct __optional_copy_assign_base : __optional_move_base<_Tp> 5120b57cec5SDimitry Andric{ 5130b57cec5SDimitry Andric using __optional_move_base<_Tp>::__optional_move_base; 5140b57cec5SDimitry Andric}; 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andrictemplate <class _Tp> 5170b57cec5SDimitry Andricstruct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> 5180b57cec5SDimitry Andric{ 5190b57cec5SDimitry Andric using __optional_move_base<_Tp>::__optional_move_base; 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5220b57cec5SDimitry Andric __optional_copy_assign_base() = default; 5230b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5240b57cec5SDimitry Andric __optional_copy_assign_base(const __optional_copy_assign_base&) = default; 5250b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5260b57cec5SDimitry Andric __optional_copy_assign_base(__optional_copy_assign_base&&) = default; 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 529fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_copy_assign_base& operator=(const __optional_copy_assign_base& __opt) 5300b57cec5SDimitry Andric { 5310b57cec5SDimitry Andric this->__assign_from(__opt); 5320b57cec5SDimitry Andric return *this; 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5360b57cec5SDimitry Andric __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default; 5370b57cec5SDimitry Andric}; 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andrictemplate <class _Tp, bool = 5400b57cec5SDimitry Andric is_trivially_destructible<_Tp>::value && 5410b57cec5SDimitry Andric is_trivially_move_constructible<_Tp>::value && 5420b57cec5SDimitry Andric is_trivially_move_assignable<_Tp>::value> 5430b57cec5SDimitry Andricstruct __optional_move_assign_base : __optional_copy_assign_base<_Tp> 5440b57cec5SDimitry Andric{ 5450b57cec5SDimitry Andric using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; 5460b57cec5SDimitry Andric}; 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andrictemplate <class _Tp> 5490b57cec5SDimitry Andricstruct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> 5500b57cec5SDimitry Andric{ 5510b57cec5SDimitry Andric using value_type = _Tp; 5520b57cec5SDimitry Andric using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5550b57cec5SDimitry Andric __optional_move_assign_base() = default; 5560b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5570b57cec5SDimitry Andric __optional_move_assign_base(const __optional_move_assign_base& __opt) = default; 5580b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5590b57cec5SDimitry Andric __optional_move_assign_base(__optional_move_assign_base&&) = default; 5600b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5610b57cec5SDimitry Andric __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default; 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 564fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_move_assign_base& operator=(__optional_move_assign_base&& __opt) 5650b57cec5SDimitry Andric noexcept(is_nothrow_move_assignable_v<value_type> && 5660b57cec5SDimitry Andric is_nothrow_move_constructible_v<value_type>) 5670b57cec5SDimitry Andric { 5680b57cec5SDimitry Andric this->__assign_from(_VSTD::move(__opt)); 5690b57cec5SDimitry Andric return *this; 5700b57cec5SDimitry Andric } 5710b57cec5SDimitry Andric}; 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andrictemplate <class _Tp> 5740b57cec5SDimitry Andricusing __optional_sfinae_ctor_base_t = __sfinae_ctor_base< 5750b57cec5SDimitry Andric is_copy_constructible<_Tp>::value, 5760b57cec5SDimitry Andric is_move_constructible<_Tp>::value 5770b57cec5SDimitry Andric>; 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andrictemplate <class _Tp> 5800b57cec5SDimitry Andricusing __optional_sfinae_assign_base_t = __sfinae_assign_base< 5810b57cec5SDimitry Andric (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value), 5820b57cec5SDimitry Andric (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value) 5830b57cec5SDimitry Andric>; 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andrictemplate <class _Tp> 5860b57cec5SDimitry Andricclass optional 5870b57cec5SDimitry Andric : private __optional_move_assign_base<_Tp> 5880b57cec5SDimitry Andric , private __optional_sfinae_ctor_base_t<_Tp> 5890b57cec5SDimitry Andric , private __optional_sfinae_assign_base_t<_Tp> 5900b57cec5SDimitry Andric{ 5910b57cec5SDimitry Andric using __base = __optional_move_assign_base<_Tp>; 5920b57cec5SDimitry Andricpublic: 5930b57cec5SDimitry Andric using value_type = _Tp; 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andricprivate: 5960b57cec5SDimitry Andric // Disable the reference extension using this static assert. 5970b57cec5SDimitry Andric static_assert(!is_same_v<__uncvref_t<value_type>, in_place_t>, 5980b57cec5SDimitry Andric "instantiation of optional with in_place_t is ill-formed"); 5990b57cec5SDimitry Andric static_assert(!is_same_v<__uncvref_t<value_type>, nullopt_t>, 6000b57cec5SDimitry Andric "instantiation of optional with nullopt_t is ill-formed"); 6010b57cec5SDimitry Andric static_assert(!is_reference_v<value_type>, 6020b57cec5SDimitry Andric "instantiation of optional with a reference type is ill-formed"); 6030b57cec5SDimitry Andric static_assert(is_destructible_v<value_type>, 6040b57cec5SDimitry Andric "instantiation of optional with a non-destructible type is ill-formed"); 6050b57cec5SDimitry Andric static_assert(!is_array_v<value_type>, 6060b57cec5SDimitry Andric "instantiation of optional with an array type is ill-formed"); 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric // LWG2756: conditionally explicit conversion from _Up 6090b57cec5SDimitry Andric struct _CheckOptionalArgsConstructor { 6100b57cec5SDimitry Andric template <class _Up> 6110b57cec5SDimitry Andric static constexpr bool __enable_implicit() { 6120b57cec5SDimitry Andric return is_constructible_v<_Tp, _Up&&> && 6130b57cec5SDimitry Andric is_convertible_v<_Up&&, _Tp>; 6140b57cec5SDimitry Andric } 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric template <class _Up> 6170b57cec5SDimitry Andric static constexpr bool __enable_explicit() { 6180b57cec5SDimitry Andric return is_constructible_v<_Tp, _Up&&> && 6190b57cec5SDimitry Andric !is_convertible_v<_Up&&, _Tp>; 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric }; 6220b57cec5SDimitry Andric template <class _Up> 6230b57cec5SDimitry Andric using _CheckOptionalArgsCtor = _If< 6240b57cec5SDimitry Andric _IsNotSame<__uncvref_t<_Up>, in_place_t>::value && 6250b57cec5SDimitry Andric _IsNotSame<__uncvref_t<_Up>, optional>::value, 6260b57cec5SDimitry Andric _CheckOptionalArgsConstructor, 6270b57cec5SDimitry Andric __check_tuple_constructor_fail 6280b57cec5SDimitry Andric >; 6290b57cec5SDimitry Andric template <class _QualUp> 6300b57cec5SDimitry Andric struct _CheckOptionalLikeConstructor { 6310b57cec5SDimitry Andric template <class _Up, class _Opt = optional<_Up>> 6320b57cec5SDimitry Andric using __check_constructible_from_opt = _Or< 6330b57cec5SDimitry Andric is_constructible<_Tp, _Opt&>, 6340b57cec5SDimitry Andric is_constructible<_Tp, _Opt const&>, 6350b57cec5SDimitry Andric is_constructible<_Tp, _Opt&&>, 6360b57cec5SDimitry Andric is_constructible<_Tp, _Opt const&&>, 6370b57cec5SDimitry Andric is_convertible<_Opt&, _Tp>, 6380b57cec5SDimitry Andric is_convertible<_Opt const&, _Tp>, 6390b57cec5SDimitry Andric is_convertible<_Opt&&, _Tp>, 6400b57cec5SDimitry Andric is_convertible<_Opt const&&, _Tp> 6410b57cec5SDimitry Andric >; 6420b57cec5SDimitry Andric template <class _Up, class _Opt = optional<_Up>> 6430b57cec5SDimitry Andric using __check_assignable_from_opt = _Or< 6440b57cec5SDimitry Andric is_assignable<_Tp&, _Opt&>, 6450b57cec5SDimitry Andric is_assignable<_Tp&, _Opt const&>, 6460b57cec5SDimitry Andric is_assignable<_Tp&, _Opt&&>, 6470b57cec5SDimitry Andric is_assignable<_Tp&, _Opt const&&> 6480b57cec5SDimitry Andric >; 6490b57cec5SDimitry Andric template <class _Up, class _QUp = _QualUp> 6500b57cec5SDimitry Andric static constexpr bool __enable_implicit() { 6510b57cec5SDimitry Andric return is_convertible<_QUp, _Tp>::value && 6520b57cec5SDimitry Andric !__check_constructible_from_opt<_Up>::value; 6530b57cec5SDimitry Andric } 6540b57cec5SDimitry Andric template <class _Up, class _QUp = _QualUp> 6550b57cec5SDimitry Andric static constexpr bool __enable_explicit() { 6560b57cec5SDimitry Andric return !is_convertible<_QUp, _Tp>::value && 6570b57cec5SDimitry Andric !__check_constructible_from_opt<_Up>::value; 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric template <class _Up, class _QUp = _QualUp> 6600b57cec5SDimitry Andric static constexpr bool __enable_assign() { 661e8d8bef9SDimitry Andric // Construction and assignability of _QUp to _Tp has already been 6620b57cec5SDimitry Andric // checked. 6630b57cec5SDimitry Andric return !__check_constructible_from_opt<_Up>::value && 6640b57cec5SDimitry Andric !__check_assignable_from_opt<_Up>::value; 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric }; 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric template <class _Up, class _QualUp> 6690b57cec5SDimitry Andric using _CheckOptionalLikeCtor = _If< 6700b57cec5SDimitry Andric _And< 6710b57cec5SDimitry Andric _IsNotSame<_Up, _Tp>, 6720b57cec5SDimitry Andric is_constructible<_Tp, _QualUp> 6730b57cec5SDimitry Andric >::value, 6740b57cec5SDimitry Andric _CheckOptionalLikeConstructor<_QualUp>, 6750b57cec5SDimitry Andric __check_tuple_constructor_fail 6760b57cec5SDimitry Andric >; 6770b57cec5SDimitry Andric template <class _Up, class _QualUp> 6780b57cec5SDimitry Andric using _CheckOptionalLikeAssign = _If< 6790b57cec5SDimitry Andric _And< 6800b57cec5SDimitry Andric _IsNotSame<_Up, _Tp>, 6810b57cec5SDimitry Andric is_constructible<_Tp, _QualUp>, 6820b57cec5SDimitry Andric is_assignable<_Tp&, _QualUp> 6830b57cec5SDimitry Andric >::value, 6840b57cec5SDimitry Andric _CheckOptionalLikeConstructor<_QualUp>, 6850b57cec5SDimitry Andric __check_tuple_constructor_fail 6860b57cec5SDimitry Andric >; 6870b57cec5SDimitry Andricpublic: 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {} 6900b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default; 6910b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default; 6920b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {} 6930b57cec5SDimitry Andric 694*349cc55cSDimitry Andric template <class _InPlaceT, class... _Args, class = enable_if_t< 6950b57cec5SDimitry Andric _And< 6960b57cec5SDimitry Andric _IsSame<_InPlaceT, in_place_t>, 6970b57cec5SDimitry Andric is_constructible<value_type, _Args...> 6980b57cec5SDimitry Andric >::value 6990b57cec5SDimitry Andric > 7000b57cec5SDimitry Andric > 7010b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 7020b57cec5SDimitry Andric constexpr explicit optional(_InPlaceT, _Args&&... __args) 7030b57cec5SDimitry Andric : __base(in_place, _VSTD::forward<_Args>(__args)...) {} 7040b57cec5SDimitry Andric 705*349cc55cSDimitry Andric template <class _Up, class... _Args, class = enable_if_t< 7060b57cec5SDimitry Andric is_constructible_v<value_type, initializer_list<_Up>&, _Args...>> 7070b57cec5SDimitry Andric > 7080b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 7090b57cec5SDimitry Andric constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args) 7100b57cec5SDimitry Andric : __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {} 7110b57cec5SDimitry Andric 712*349cc55cSDimitry Andric template <class _Up = value_type, enable_if_t< 7130b57cec5SDimitry Andric _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>() 7140b57cec5SDimitry Andric , int> = 0> 7150b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 7160b57cec5SDimitry Andric constexpr optional(_Up&& __v) 7170b57cec5SDimitry Andric : __base(in_place, _VSTD::forward<_Up>(__v)) {} 7180b57cec5SDimitry Andric 719*349cc55cSDimitry Andric template <class _Up, enable_if_t< 7200b57cec5SDimitry Andric _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>() 7210b57cec5SDimitry Andric , int> = 0> 7220b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 7230b57cec5SDimitry Andric constexpr explicit optional(_Up&& __v) 7240b57cec5SDimitry Andric : __base(in_place, _VSTD::forward<_Up>(__v)) {} 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric // LWG2756: conditionally explicit conversion from const optional<_Up>& 727*349cc55cSDimitry Andric template <class _Up, enable_if_t< 7280b57cec5SDimitry Andric _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>() 7290b57cec5SDimitry Andric , int> = 0> 7300b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 731fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(const optional<_Up>& __v) 7320b57cec5SDimitry Andric { 7330b57cec5SDimitry Andric this->__construct_from(__v); 7340b57cec5SDimitry Andric } 735*349cc55cSDimitry Andric template <class _Up, enable_if_t< 7360b57cec5SDimitry Andric _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>() 7370b57cec5SDimitry Andric , int> = 0> 7380b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 739fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(const optional<_Up>& __v) 7400b57cec5SDimitry Andric { 7410b57cec5SDimitry Andric this->__construct_from(__v); 7420b57cec5SDimitry Andric } 7430b57cec5SDimitry Andric 7440b57cec5SDimitry Andric // LWG2756: conditionally explicit conversion from optional<_Up>&& 745*349cc55cSDimitry Andric template <class _Up, enable_if_t< 7460b57cec5SDimitry Andric _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_implicit<_Up>() 7470b57cec5SDimitry Andric , int> = 0> 7480b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 749fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(optional<_Up>&& __v) 7500b57cec5SDimitry Andric { 7510b57cec5SDimitry Andric this->__construct_from(_VSTD::move(__v)); 7520b57cec5SDimitry Andric } 753*349cc55cSDimitry Andric template <class _Up, enable_if_t< 7540b57cec5SDimitry Andric _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_explicit<_Up>() 7550b57cec5SDimitry Andric , int> = 0> 7560b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 757fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(optional<_Up>&& __v) 7580b57cec5SDimitry Andric { 7590b57cec5SDimitry Andric this->__construct_from(_VSTD::move(__v)); 7600b57cec5SDimitry Andric } 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 763fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& operator=(nullopt_t) noexcept 7640b57cec5SDimitry Andric { 7650b57cec5SDimitry Andric reset(); 7660b57cec5SDimitry Andric return *this; 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY optional& operator=(const optional&) = default; 7700b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY optional& operator=(optional&&) = default; 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric // LWG2756 7730b57cec5SDimitry Andric template <class _Up = value_type, 774*349cc55cSDimitry Andric class = enable_if_t< 7750b57cec5SDimitry Andric _And< 7760b57cec5SDimitry Andric _IsNotSame<__uncvref_t<_Up>, optional>, 7770b57cec5SDimitry Andric _Or< 7780b57cec5SDimitry Andric _IsNotSame<__uncvref_t<_Up>, value_type>, 7790b57cec5SDimitry Andric _Not<is_scalar<value_type>> 7800b57cec5SDimitry Andric >, 7810b57cec5SDimitry Andric is_constructible<value_type, _Up>, 7820b57cec5SDimitry Andric is_assignable<value_type&, _Up> 7830b57cec5SDimitry Andric >::value> 7840b57cec5SDimitry Andric > 7850b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 786fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& 7870b57cec5SDimitry Andric operator=(_Up&& __v) 7880b57cec5SDimitry Andric { 7890b57cec5SDimitry Andric if (this->has_value()) 7900b57cec5SDimitry Andric this->__get() = _VSTD::forward<_Up>(__v); 7910b57cec5SDimitry Andric else 7920b57cec5SDimitry Andric this->__construct(_VSTD::forward<_Up>(__v)); 7930b57cec5SDimitry Andric return *this; 7940b57cec5SDimitry Andric } 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric // LWG2756 797*349cc55cSDimitry Andric template <class _Up, enable_if_t< 7980b57cec5SDimitry Andric _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>() 7990b57cec5SDimitry Andric , int> = 0> 8000b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 801fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& 8020b57cec5SDimitry Andric operator=(const optional<_Up>& __v) 8030b57cec5SDimitry Andric { 8040b57cec5SDimitry Andric this->__assign_from(__v); 8050b57cec5SDimitry Andric return *this; 8060b57cec5SDimitry Andric } 8070b57cec5SDimitry Andric 8080b57cec5SDimitry Andric // LWG2756 809*349cc55cSDimitry Andric template <class _Up, enable_if_t< 8100b57cec5SDimitry Andric _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_assign<_Up>() 8110b57cec5SDimitry Andric , int> = 0> 8120b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 813fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& 8140b57cec5SDimitry Andric operator=(optional<_Up>&& __v) 8150b57cec5SDimitry Andric { 8160b57cec5SDimitry Andric this->__assign_from(_VSTD::move(__v)); 8170b57cec5SDimitry Andric return *this; 8180b57cec5SDimitry Andric } 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric template <class... _Args, 821*349cc55cSDimitry Andric class = enable_if_t 8220b57cec5SDimitry Andric < 8230b57cec5SDimitry Andric is_constructible_v<value_type, _Args...> 8240b57cec5SDimitry Andric > 8250b57cec5SDimitry Andric > 8260b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 827fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 _Tp & 8280b57cec5SDimitry Andric emplace(_Args&&... __args) 8290b57cec5SDimitry Andric { 8300b57cec5SDimitry Andric reset(); 8310b57cec5SDimitry Andric this->__construct(_VSTD::forward<_Args>(__args)...); 8320b57cec5SDimitry Andric return this->__get(); 8330b57cec5SDimitry Andric } 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andric template <class _Up, class... _Args, 836*349cc55cSDimitry Andric class = enable_if_t 8370b57cec5SDimitry Andric < 8380b57cec5SDimitry Andric is_constructible_v<value_type, initializer_list<_Up>&, _Args...> 8390b57cec5SDimitry Andric > 8400b57cec5SDimitry Andric > 8410b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 842fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 _Tp & 8430b57cec5SDimitry Andric emplace(initializer_list<_Up> __il, _Args&&... __args) 8440b57cec5SDimitry Andric { 8450b57cec5SDimitry Andric reset(); 8460b57cec5SDimitry Andric this->__construct(__il, _VSTD::forward<_Args>(__args)...); 8470b57cec5SDimitry Andric return this->__get(); 8480b57cec5SDimitry Andric } 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 851fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 void swap(optional& __opt) 8520b57cec5SDimitry Andric noexcept(is_nothrow_move_constructible_v<value_type> && 8530b57cec5SDimitry Andric is_nothrow_swappable_v<value_type>) 8540b57cec5SDimitry Andric { 8550b57cec5SDimitry Andric if (this->has_value() == __opt.has_value()) 8560b57cec5SDimitry Andric { 8570b57cec5SDimitry Andric using _VSTD::swap; 8580b57cec5SDimitry Andric if (this->has_value()) 8590b57cec5SDimitry Andric swap(this->__get(), __opt.__get()); 8600b57cec5SDimitry Andric } 8610b57cec5SDimitry Andric else 8620b57cec5SDimitry Andric { 8630b57cec5SDimitry Andric if (this->has_value()) 8640b57cec5SDimitry Andric { 8650b57cec5SDimitry Andric __opt.__construct(_VSTD::move(this->__get())); 8660b57cec5SDimitry Andric reset(); 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric else 8690b57cec5SDimitry Andric { 8700b57cec5SDimitry Andric this->__construct(_VSTD::move(__opt.__get())); 8710b57cec5SDimitry Andric __opt.reset(); 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric } 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 8770b57cec5SDimitry Andric constexpr 8780b57cec5SDimitry Andric add_pointer_t<value_type const> 8790b57cec5SDimitry Andric operator->() const 8800b57cec5SDimitry Andric { 881fe6060f1SDimitry Andric _LIBCPP_ASSERT(this->has_value(), "optional operator-> called on a disengaged value"); 8820b57cec5SDimitry Andric return _VSTD::addressof(this->__get()); 8830b57cec5SDimitry Andric } 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 8860b57cec5SDimitry Andric constexpr 8870b57cec5SDimitry Andric add_pointer_t<value_type> 8880b57cec5SDimitry Andric operator->() 8890b57cec5SDimitry Andric { 890fe6060f1SDimitry Andric _LIBCPP_ASSERT(this->has_value(), "optional operator-> called on a disengaged value"); 8910b57cec5SDimitry Andric return _VSTD::addressof(this->__get()); 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 8950b57cec5SDimitry Andric constexpr 8960b57cec5SDimitry Andric const value_type& 897fe6060f1SDimitry Andric operator*() const& noexcept 8980b57cec5SDimitry Andric { 899fe6060f1SDimitry Andric _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value"); 9000b57cec5SDimitry Andric return this->__get(); 9010b57cec5SDimitry Andric } 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9040b57cec5SDimitry Andric constexpr 9050b57cec5SDimitry Andric value_type& 906fe6060f1SDimitry Andric operator*() & noexcept 9070b57cec5SDimitry Andric { 908fe6060f1SDimitry Andric _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value"); 9090b57cec5SDimitry Andric return this->__get(); 9100b57cec5SDimitry Andric } 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9130b57cec5SDimitry Andric constexpr 9140b57cec5SDimitry Andric value_type&& 915fe6060f1SDimitry Andric operator*() && noexcept 9160b57cec5SDimitry Andric { 917fe6060f1SDimitry Andric _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value"); 9180b57cec5SDimitry Andric return _VSTD::move(this->__get()); 9190b57cec5SDimitry Andric } 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9220b57cec5SDimitry Andric constexpr 9230b57cec5SDimitry Andric const value_type&& 924fe6060f1SDimitry Andric operator*() const&& noexcept 9250b57cec5SDimitry Andric { 926fe6060f1SDimitry Andric _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value"); 9270b57cec5SDimitry Andric return _VSTD::move(this->__get()); 9280b57cec5SDimitry Andric } 9290b57cec5SDimitry Andric 9300b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9310b57cec5SDimitry Andric constexpr explicit operator bool() const noexcept { return has_value(); } 9320b57cec5SDimitry Andric 9330b57cec5SDimitry Andric using __base::has_value; 9340b57cec5SDimitry Andric using __base::__get; 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9370b57cec5SDimitry Andric _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS 9380b57cec5SDimitry Andric constexpr value_type const& value() const& 9390b57cec5SDimitry Andric { 9400b57cec5SDimitry Andric if (!this->has_value()) 9410b57cec5SDimitry Andric __throw_bad_optional_access(); 9420b57cec5SDimitry Andric return this->__get(); 9430b57cec5SDimitry Andric } 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9460b57cec5SDimitry Andric _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS 9470b57cec5SDimitry Andric constexpr value_type& value() & 9480b57cec5SDimitry Andric { 9490b57cec5SDimitry Andric if (!this->has_value()) 9500b57cec5SDimitry Andric __throw_bad_optional_access(); 9510b57cec5SDimitry Andric return this->__get(); 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric 9540b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9550b57cec5SDimitry Andric _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS 9560b57cec5SDimitry Andric constexpr value_type&& value() && 9570b57cec5SDimitry Andric { 9580b57cec5SDimitry Andric if (!this->has_value()) 9590b57cec5SDimitry Andric __throw_bad_optional_access(); 9600b57cec5SDimitry Andric return _VSTD::move(this->__get()); 9610b57cec5SDimitry Andric } 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9640b57cec5SDimitry Andric _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS 9650b57cec5SDimitry Andric constexpr value_type const&& value() const&& 9660b57cec5SDimitry Andric { 9670b57cec5SDimitry Andric if (!this->has_value()) 9680b57cec5SDimitry Andric __throw_bad_optional_access(); 9690b57cec5SDimitry Andric return _VSTD::move(this->__get()); 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric template <class _Up> 9730b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9740b57cec5SDimitry Andric constexpr value_type value_or(_Up&& __v) const& 9750b57cec5SDimitry Andric { 9760b57cec5SDimitry Andric static_assert(is_copy_constructible_v<value_type>, 9770b57cec5SDimitry Andric "optional<T>::value_or: T must be copy constructible"); 9780b57cec5SDimitry Andric static_assert(is_convertible_v<_Up, value_type>, 9790b57cec5SDimitry Andric "optional<T>::value_or: U must be convertible to T"); 9800b57cec5SDimitry Andric return this->has_value() ? this->__get() : 9810b57cec5SDimitry Andric static_cast<value_type>(_VSTD::forward<_Up>(__v)); 9820b57cec5SDimitry Andric } 9830b57cec5SDimitry Andric 9840b57cec5SDimitry Andric template <class _Up> 9850b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 9860b57cec5SDimitry Andric constexpr value_type value_or(_Up&& __v) && 9870b57cec5SDimitry Andric { 9880b57cec5SDimitry Andric static_assert(is_move_constructible_v<value_type>, 9890b57cec5SDimitry Andric "optional<T>::value_or: T must be move constructible"); 9900b57cec5SDimitry Andric static_assert(is_convertible_v<_Up, value_type>, 9910b57cec5SDimitry Andric "optional<T>::value_or: U must be convertible to T"); 9920b57cec5SDimitry Andric return this->has_value() ? _VSTD::move(this->__get()) : 9930b57cec5SDimitry Andric static_cast<value_type>(_VSTD::forward<_Up>(__v)); 9940b57cec5SDimitry Andric } 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric using __base::reset; 9970b57cec5SDimitry Andric}; 9980b57cec5SDimitry Andric 999*349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17 10000b57cec5SDimitry Andrictemplate<class T> 10010b57cec5SDimitry Andric optional(T) -> optional<T>; 10020b57cec5SDimitry Andric#endif 10030b57cec5SDimitry Andric 10040b57cec5SDimitry Andric// Comparisons between optionals 10050b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 10060b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1007*349cc55cSDimitry Andricenable_if_t< 1008fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() == 1009fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 10100b57cec5SDimitry Andric bool 10110b57cec5SDimitry Andric> 10120b57cec5SDimitry Andricoperator==(const optional<_Tp>& __x, const optional<_Up>& __y) 10130b57cec5SDimitry Andric{ 10140b57cec5SDimitry Andric if (static_cast<bool>(__x) != static_cast<bool>(__y)) 10150b57cec5SDimitry Andric return false; 10160b57cec5SDimitry Andric if (!static_cast<bool>(__x)) 10170b57cec5SDimitry Andric return true; 10180b57cec5SDimitry Andric return *__x == *__y; 10190b57cec5SDimitry Andric} 10200b57cec5SDimitry Andric 10210b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 10220b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1023*349cc55cSDimitry Andricenable_if_t< 1024fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() != 1025fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 10260b57cec5SDimitry Andric bool 10270b57cec5SDimitry Andric> 10280b57cec5SDimitry Andricoperator!=(const optional<_Tp>& __x, const optional<_Up>& __y) 10290b57cec5SDimitry Andric{ 10300b57cec5SDimitry Andric if (static_cast<bool>(__x) != static_cast<bool>(__y)) 10310b57cec5SDimitry Andric return true; 10320b57cec5SDimitry Andric if (!static_cast<bool>(__x)) 10330b57cec5SDimitry Andric return false; 10340b57cec5SDimitry Andric return *__x != *__y; 10350b57cec5SDimitry Andric} 10360b57cec5SDimitry Andric 10370b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 10380b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1039*349cc55cSDimitry Andricenable_if_t< 1040fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() < 1041fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 10420b57cec5SDimitry Andric bool 10430b57cec5SDimitry Andric> 10440b57cec5SDimitry Andricoperator<(const optional<_Tp>& __x, const optional<_Up>& __y) 10450b57cec5SDimitry Andric{ 10460b57cec5SDimitry Andric if (!static_cast<bool>(__y)) 10470b57cec5SDimitry Andric return false; 10480b57cec5SDimitry Andric if (!static_cast<bool>(__x)) 10490b57cec5SDimitry Andric return true; 10500b57cec5SDimitry Andric return *__x < *__y; 10510b57cec5SDimitry Andric} 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 10540b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1055*349cc55cSDimitry Andricenable_if_t< 1056fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() > 1057fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 10580b57cec5SDimitry Andric bool 10590b57cec5SDimitry Andric> 10600b57cec5SDimitry Andricoperator>(const optional<_Tp>& __x, const optional<_Up>& __y) 10610b57cec5SDimitry Andric{ 10620b57cec5SDimitry Andric if (!static_cast<bool>(__x)) 10630b57cec5SDimitry Andric return false; 10640b57cec5SDimitry Andric if (!static_cast<bool>(__y)) 10650b57cec5SDimitry Andric return true; 10660b57cec5SDimitry Andric return *__x > *__y; 10670b57cec5SDimitry Andric} 10680b57cec5SDimitry Andric 10690b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 10700b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1071*349cc55cSDimitry Andricenable_if_t< 1072fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() <= 1073fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 10740b57cec5SDimitry Andric bool 10750b57cec5SDimitry Andric> 10760b57cec5SDimitry Andricoperator<=(const optional<_Tp>& __x, const optional<_Up>& __y) 10770b57cec5SDimitry Andric{ 10780b57cec5SDimitry Andric if (!static_cast<bool>(__x)) 10790b57cec5SDimitry Andric return true; 10800b57cec5SDimitry Andric if (!static_cast<bool>(__y)) 10810b57cec5SDimitry Andric return false; 10820b57cec5SDimitry Andric return *__x <= *__y; 10830b57cec5SDimitry Andric} 10840b57cec5SDimitry Andric 10850b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 10860b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1087*349cc55cSDimitry Andricenable_if_t< 1088fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() >= 1089fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 10900b57cec5SDimitry Andric bool 10910b57cec5SDimitry Andric> 10920b57cec5SDimitry Andricoperator>=(const optional<_Tp>& __x, const optional<_Up>& __y) 10930b57cec5SDimitry Andric{ 10940b57cec5SDimitry Andric if (!static_cast<bool>(__y)) 10950b57cec5SDimitry Andric return true; 10960b57cec5SDimitry Andric if (!static_cast<bool>(__x)) 10970b57cec5SDimitry Andric return false; 10980b57cec5SDimitry Andric return *__x >= *__y; 10990b57cec5SDimitry Andric} 11000b57cec5SDimitry Andric 11010b57cec5SDimitry Andric// Comparisons with nullopt 11020b57cec5SDimitry Andrictemplate <class _Tp> 11030b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11040b57cec5SDimitry Andricbool 11050b57cec5SDimitry Andricoperator==(const optional<_Tp>& __x, nullopt_t) noexcept 11060b57cec5SDimitry Andric{ 11070b57cec5SDimitry Andric return !static_cast<bool>(__x); 11080b57cec5SDimitry Andric} 11090b57cec5SDimitry Andric 11100b57cec5SDimitry Andrictemplate <class _Tp> 11110b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11120b57cec5SDimitry Andricbool 11130b57cec5SDimitry Andricoperator==(nullopt_t, const optional<_Tp>& __x) noexcept 11140b57cec5SDimitry Andric{ 11150b57cec5SDimitry Andric return !static_cast<bool>(__x); 11160b57cec5SDimitry Andric} 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andrictemplate <class _Tp> 11190b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11200b57cec5SDimitry Andricbool 11210b57cec5SDimitry Andricoperator!=(const optional<_Tp>& __x, nullopt_t) noexcept 11220b57cec5SDimitry Andric{ 11230b57cec5SDimitry Andric return static_cast<bool>(__x); 11240b57cec5SDimitry Andric} 11250b57cec5SDimitry Andric 11260b57cec5SDimitry Andrictemplate <class _Tp> 11270b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11280b57cec5SDimitry Andricbool 11290b57cec5SDimitry Andricoperator!=(nullopt_t, const optional<_Tp>& __x) noexcept 11300b57cec5SDimitry Andric{ 11310b57cec5SDimitry Andric return static_cast<bool>(__x); 11320b57cec5SDimitry Andric} 11330b57cec5SDimitry Andric 11340b57cec5SDimitry Andrictemplate <class _Tp> 11350b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11360b57cec5SDimitry Andricbool 11370b57cec5SDimitry Andricoperator<(const optional<_Tp>&, nullopt_t) noexcept 11380b57cec5SDimitry Andric{ 11390b57cec5SDimitry Andric return false; 11400b57cec5SDimitry Andric} 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andrictemplate <class _Tp> 11430b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11440b57cec5SDimitry Andricbool 11450b57cec5SDimitry Andricoperator<(nullopt_t, const optional<_Tp>& __x) noexcept 11460b57cec5SDimitry Andric{ 11470b57cec5SDimitry Andric return static_cast<bool>(__x); 11480b57cec5SDimitry Andric} 11490b57cec5SDimitry Andric 11500b57cec5SDimitry Andrictemplate <class _Tp> 11510b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11520b57cec5SDimitry Andricbool 11530b57cec5SDimitry Andricoperator<=(const optional<_Tp>& __x, nullopt_t) noexcept 11540b57cec5SDimitry Andric{ 11550b57cec5SDimitry Andric return !static_cast<bool>(__x); 11560b57cec5SDimitry Andric} 11570b57cec5SDimitry Andric 11580b57cec5SDimitry Andrictemplate <class _Tp> 11590b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11600b57cec5SDimitry Andricbool 11610b57cec5SDimitry Andricoperator<=(nullopt_t, const optional<_Tp>&) noexcept 11620b57cec5SDimitry Andric{ 11630b57cec5SDimitry Andric return true; 11640b57cec5SDimitry Andric} 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andrictemplate <class _Tp> 11670b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11680b57cec5SDimitry Andricbool 11690b57cec5SDimitry Andricoperator>(const optional<_Tp>& __x, nullopt_t) noexcept 11700b57cec5SDimitry Andric{ 11710b57cec5SDimitry Andric return static_cast<bool>(__x); 11720b57cec5SDimitry Andric} 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andrictemplate <class _Tp> 11750b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11760b57cec5SDimitry Andricbool 11770b57cec5SDimitry Andricoperator>(nullopt_t, const optional<_Tp>&) noexcept 11780b57cec5SDimitry Andric{ 11790b57cec5SDimitry Andric return false; 11800b57cec5SDimitry Andric} 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andrictemplate <class _Tp> 11830b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11840b57cec5SDimitry Andricbool 11850b57cec5SDimitry Andricoperator>=(const optional<_Tp>&, nullopt_t) noexcept 11860b57cec5SDimitry Andric{ 11870b57cec5SDimitry Andric return true; 11880b57cec5SDimitry Andric} 11890b57cec5SDimitry Andric 11900b57cec5SDimitry Andrictemplate <class _Tp> 11910b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 11920b57cec5SDimitry Andricbool 11930b57cec5SDimitry Andricoperator>=(nullopt_t, const optional<_Tp>& __x) noexcept 11940b57cec5SDimitry Andric{ 11950b57cec5SDimitry Andric return !static_cast<bool>(__x); 11960b57cec5SDimitry Andric} 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric// Comparisons with T 11990b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12000b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1201*349cc55cSDimitry Andricenable_if_t< 1202fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() == 1203fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 12040b57cec5SDimitry Andric bool 12050b57cec5SDimitry Andric> 12060b57cec5SDimitry Andricoperator==(const optional<_Tp>& __x, const _Up& __v) 12070b57cec5SDimitry Andric{ 12080b57cec5SDimitry Andric return static_cast<bool>(__x) ? *__x == __v : false; 12090b57cec5SDimitry Andric} 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12120b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1213*349cc55cSDimitry Andricenable_if_t< 1214fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() == 1215fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 12160b57cec5SDimitry Andric bool 12170b57cec5SDimitry Andric> 12180b57cec5SDimitry Andricoperator==(const _Tp& __v, const optional<_Up>& __x) 12190b57cec5SDimitry Andric{ 12200b57cec5SDimitry Andric return static_cast<bool>(__x) ? __v == *__x : false; 12210b57cec5SDimitry Andric} 12220b57cec5SDimitry Andric 12230b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12240b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1225*349cc55cSDimitry Andricenable_if_t< 1226fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() != 1227fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 12280b57cec5SDimitry Andric bool 12290b57cec5SDimitry Andric> 12300b57cec5SDimitry Andricoperator!=(const optional<_Tp>& __x, const _Up& __v) 12310b57cec5SDimitry Andric{ 12320b57cec5SDimitry Andric return static_cast<bool>(__x) ? *__x != __v : true; 12330b57cec5SDimitry Andric} 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12360b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1237*349cc55cSDimitry Andricenable_if_t< 1238fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() != 1239fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 12400b57cec5SDimitry Andric bool 12410b57cec5SDimitry Andric> 12420b57cec5SDimitry Andricoperator!=(const _Tp& __v, const optional<_Up>& __x) 12430b57cec5SDimitry Andric{ 12440b57cec5SDimitry Andric return static_cast<bool>(__x) ? __v != *__x : true; 12450b57cec5SDimitry Andric} 12460b57cec5SDimitry Andric 12470b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12480b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1249*349cc55cSDimitry Andricenable_if_t< 1250fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() < 1251fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 12520b57cec5SDimitry Andric bool 12530b57cec5SDimitry Andric> 12540b57cec5SDimitry Andricoperator<(const optional<_Tp>& __x, const _Up& __v) 12550b57cec5SDimitry Andric{ 12560b57cec5SDimitry Andric return static_cast<bool>(__x) ? *__x < __v : true; 12570b57cec5SDimitry Andric} 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12600b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1261*349cc55cSDimitry Andricenable_if_t< 1262fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() < 1263fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 12640b57cec5SDimitry Andric bool 12650b57cec5SDimitry Andric> 12660b57cec5SDimitry Andricoperator<(const _Tp& __v, const optional<_Up>& __x) 12670b57cec5SDimitry Andric{ 12680b57cec5SDimitry Andric return static_cast<bool>(__x) ? __v < *__x : false; 12690b57cec5SDimitry Andric} 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12720b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1273*349cc55cSDimitry Andricenable_if_t< 1274fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() <= 1275fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 12760b57cec5SDimitry Andric bool 12770b57cec5SDimitry Andric> 12780b57cec5SDimitry Andricoperator<=(const optional<_Tp>& __x, const _Up& __v) 12790b57cec5SDimitry Andric{ 12800b57cec5SDimitry Andric return static_cast<bool>(__x) ? *__x <= __v : true; 12810b57cec5SDimitry Andric} 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12840b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1285*349cc55cSDimitry Andricenable_if_t< 1286fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() <= 1287fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 12880b57cec5SDimitry Andric bool 12890b57cec5SDimitry Andric> 12900b57cec5SDimitry Andricoperator<=(const _Tp& __v, const optional<_Up>& __x) 12910b57cec5SDimitry Andric{ 12920b57cec5SDimitry Andric return static_cast<bool>(__x) ? __v <= *__x : false; 12930b57cec5SDimitry Andric} 12940b57cec5SDimitry Andric 12950b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 12960b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1297*349cc55cSDimitry Andricenable_if_t< 1298fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() > 1299fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 13000b57cec5SDimitry Andric bool 13010b57cec5SDimitry Andric> 13020b57cec5SDimitry Andricoperator>(const optional<_Tp>& __x, const _Up& __v) 13030b57cec5SDimitry Andric{ 13040b57cec5SDimitry Andric return static_cast<bool>(__x) ? *__x > __v : false; 13050b57cec5SDimitry Andric} 13060b57cec5SDimitry Andric 13070b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 13080b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1309*349cc55cSDimitry Andricenable_if_t< 1310fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() > 1311fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 13120b57cec5SDimitry Andric bool 13130b57cec5SDimitry Andric> 13140b57cec5SDimitry Andricoperator>(const _Tp& __v, const optional<_Up>& __x) 13150b57cec5SDimitry Andric{ 13160b57cec5SDimitry Andric return static_cast<bool>(__x) ? __v > *__x : true; 13170b57cec5SDimitry Andric} 13180b57cec5SDimitry Andric 13190b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 13200b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1321*349cc55cSDimitry Andricenable_if_t< 1322fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() >= 1323fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 13240b57cec5SDimitry Andric bool 13250b57cec5SDimitry Andric> 13260b57cec5SDimitry Andricoperator>=(const optional<_Tp>& __x, const _Up& __v) 13270b57cec5SDimitry Andric{ 13280b57cec5SDimitry Andric return static_cast<bool>(__x) ? *__x >= __v : false; 13290b57cec5SDimitry Andric} 13300b57cec5SDimitry Andric 13310b57cec5SDimitry Andrictemplate <class _Tp, class _Up> 13320b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 1333*349cc55cSDimitry Andricenable_if_t< 1334fe6060f1SDimitry Andric is_convertible_v<decltype(declval<const _Tp&>() >= 1335fe6060f1SDimitry Andric declval<const _Up&>()), bool>, 13360b57cec5SDimitry Andric bool 13370b57cec5SDimitry Andric> 13380b57cec5SDimitry Andricoperator>=(const _Tp& __v, const optional<_Up>& __x) 13390b57cec5SDimitry Andric{ 13400b57cec5SDimitry Andric return static_cast<bool>(__x) ? __v >= *__x : true; 13410b57cec5SDimitry Andric} 13420b57cec5SDimitry Andric 13430b57cec5SDimitry Andric 13440b57cec5SDimitry Andrictemplate <class _Tp> 1345fe6060f1SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 1346*349cc55cSDimitry Andricenable_if_t< 13470b57cec5SDimitry Andric is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, 13480b57cec5SDimitry Andric void 13490b57cec5SDimitry Andric> 13500b57cec5SDimitry Andricswap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) 13510b57cec5SDimitry Andric{ 13520b57cec5SDimitry Andric __x.swap(__y); 13530b57cec5SDimitry Andric} 13540b57cec5SDimitry Andric 13550b57cec5SDimitry Andrictemplate <class _Tp> 13560b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 13570b57cec5SDimitry Andricoptional<decay_t<_Tp>> make_optional(_Tp&& __v) 13580b57cec5SDimitry Andric{ 13590b57cec5SDimitry Andric return optional<decay_t<_Tp>>(_VSTD::forward<_Tp>(__v)); 13600b57cec5SDimitry Andric} 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andrictemplate <class _Tp, class... _Args> 13630b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 13640b57cec5SDimitry Andricoptional<_Tp> make_optional(_Args&&... __args) 13650b57cec5SDimitry Andric{ 13660b57cec5SDimitry Andric return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...); 13670b57cec5SDimitry Andric} 13680b57cec5SDimitry Andric 13690b57cec5SDimitry Andrictemplate <class _Tp, class _Up, class... _Args> 13700b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr 13710b57cec5SDimitry Andricoptional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) 13720b57cec5SDimitry Andric{ 13730b57cec5SDimitry Andric return optional<_Tp>(in_place, __il, _VSTD::forward<_Args>(__args)...); 13740b57cec5SDimitry Andric} 13750b57cec5SDimitry Andric 13760b57cec5SDimitry Andrictemplate <class _Tp> 13770b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash< 13780b57cec5SDimitry Andric __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> 13790b57cec5SDimitry Andric> 13800b57cec5SDimitry Andric{ 1381fe6060f1SDimitry Andric#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1382fe6060f1SDimitry Andric _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type; 1383fe6060f1SDimitry Andric _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type; 1384fe6060f1SDimitry Andric#endif 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1387fe6060f1SDimitry Andric size_t operator()(const optional<_Tp>& __opt) const 13880b57cec5SDimitry Andric { 13890b57cec5SDimitry Andric return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0; 13900b57cec5SDimitry Andric } 13910b57cec5SDimitry Andric}; 13920b57cec5SDimitry Andric 13930b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 13940b57cec5SDimitry Andric 13950b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 14 13960b57cec5SDimitry Andric 13970b57cec5SDimitry Andric#endif // _LIBCPP_OPTIONAL 1398