Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===-------------------------- optional ----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_OPTIONAL |
| 11 | #define _LIBCPP_OPTIONAL |
| 12 | |
| 13 | /* |
| 14 | optional synopsis |
| 15 | |
| 16 | // C++1z |
| 17 | |
| 18 | namespace std { |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 19 | // 23.6.3, optional for object types |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 20 | template <class T> class optional; |
| 21 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 22 | // 23.6.4, no-value state indicator |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 23 | struct nullopt_t{see below }; |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 24 | inline constexpr nullopt_t nullopt(unspecified ); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 25 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 26 | // 23.6.5, class bad_optional_access |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 27 | class bad_optional_access; |
| 28 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 29 | // 23.6.6, relational operators |
| 30 | template <class T, class U> |
| 31 | constexpr bool operator==(const optional<T>&, const optional<U>&); |
| 32 | template <class T, class U> |
| 33 | constexpr bool operator!=(const optional<T>&, const optional<U>&); |
| 34 | template <class T, class U> |
| 35 | constexpr bool operator<(const optional<T>&, const optional<U>&); |
| 36 | template <class T, class U> |
| 37 | constexpr bool operator>(const optional<T>&, const optional<U>&); |
| 38 | template <class T, class U> |
| 39 | constexpr bool operator<=(const optional<T>&, const optional<U>&); |
| 40 | template <class T, class U> |
| 41 | constexpr bool operator>=(const optional<T>&, const optional<U>&); |
| 42 | |
| 43 | // 23.6.7 comparison with nullopt |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 44 | template <class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept; |
| 45 | template <class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept; |
| 46 | template <class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; |
| 47 | template <class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; |
| 48 | template <class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept; |
| 49 | template <class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept; |
| 50 | template <class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; |
| 51 | template <class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; |
| 52 | template <class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept; |
| 53 | template <class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept; |
| 54 | template <class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; |
| 55 | template <class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; |
| 56 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 57 | // 23.6.8, comparison with T |
| 58 | template <class T, class U> constexpr bool operator==(const optional<T>&, const U&); |
Marshall Clow | 29dd7e4 | 2017-11-01 01:27:25 +0000 | [diff] [blame] | 59 | template <class T, class U> constexpr bool operator==(const T&, const optional<U>&); |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 60 | template <class T, class U> constexpr bool operator!=(const optional<T>&, const U&); |
Marshall Clow | 29dd7e4 | 2017-11-01 01:27:25 +0000 | [diff] [blame] | 61 | template <class T, class U> constexpr bool operator!=(const T&, const optional<U>&); |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 62 | template <class T, class U> constexpr bool operator<(const optional<T>&, const U&); |
Marshall Clow | 29dd7e4 | 2017-11-01 01:27:25 +0000 | [diff] [blame] | 63 | template <class T, class U> constexpr bool operator<(const T&, const optional<U>&); |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 64 | template <class T, class U> constexpr bool operator<=(const optional<T>&, const U&); |
Marshall Clow | 29dd7e4 | 2017-11-01 01:27:25 +0000 | [diff] [blame] | 65 | template <class T, class U> constexpr bool operator<=(const T&, const optional<U>&); |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 66 | template <class T, class U> constexpr bool operator>(const optional<T>&, const U&); |
Marshall Clow | 29dd7e4 | 2017-11-01 01:27:25 +0000 | [diff] [blame] | 67 | template <class T, class U> constexpr bool operator>(const T&, const optional<U>&); |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 68 | template <class T, class U> constexpr bool operator>=(const optional<T>&, const U&); |
Marshall Clow | 29dd7e4 | 2017-11-01 01:27:25 +0000 | [diff] [blame] | 69 | template <class T, class U> constexpr bool operator>=(const T&, const optional<U>&); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 70 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 71 | // 23.6.9, specialized algorithms |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 72 | template <class T> void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 73 | template <class T> constexpr optional<see below > make_optional(T&&); |
| 74 | template <class T, class... Args> |
| 75 | constexpr optional<T> make_optional(Args&&... args); |
| 76 | template <class T, class U, class... Args> |
| 77 | constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args); |
| 78 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 79 | // 23.6.10, hash support |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 80 | template <class T> struct hash; |
| 81 | template <class T> struct hash<optional<T>>; |
| 82 | |
| 83 | template <class T> class optional { |
| 84 | public: |
| 85 | using value_type = T; |
| 86 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 87 | // 23.6.3.1, constructors |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 88 | constexpr optional() noexcept; |
| 89 | constexpr optional(nullopt_t) noexcept; |
| 90 | optional(const optional &); |
Marshall Clow | 45ff982 | 2017-04-12 22:51:27 +0000 | [diff] [blame] | 91 | optional(optional &&) noexcept(see below); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 92 | template <class... Args> constexpr explicit optional(in_place_t, Args &&...); |
| 93 | template <class U, class... Args> |
| 94 | constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...); |
| 95 | template <class U = T> |
| 96 | constexpr EXPLICIT optional(U &&); |
| 97 | template <class U> |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 98 | EXPLICIT optional(const optional<U> &); // constexpr in C++20 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 99 | template <class U> |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 100 | EXPLICIT optional(optional<U> &&); // constexpr in C++20 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 101 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 102 | // 23.6.3.2, destructor |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 103 | ~optional(); // constexpr in C++20 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 104 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 105 | // 23.6.3.3, assignment |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 106 | optional &operator=(nullopt_t) noexcept; // constexpr in C++20 |
| 107 | optional &operator=(const optional &); // constexpr in C++20 |
| 108 | optional &operator=(optional &&) noexcept(see below); // constexpr in C++20 |
| 109 | template <class U = T> optional &operator=(U &&); // constexpr in C++20 |
| 110 | template <class U> optional &operator=(const optional<U> &); // constexpr in C++20 |
| 111 | template <class U> optional &operator=(optional<U> &&); // constexpr in C++20 |
| 112 | template <class... Args> T& emplace(Args &&...); // constexpr in C++20 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 113 | template <class U, class... Args> |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 114 | T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 115 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 116 | // 23.6.3.4, swap |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 117 | void swap(optional &) noexcept(see below ); // constexpr in C++20 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 118 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 119 | // 23.6.3.5, observers |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 120 | constexpr T const *operator->() const; |
| 121 | constexpr T *operator->(); |
| 122 | constexpr T const &operator*() const &; |
| 123 | constexpr T &operator*() &; |
| 124 | constexpr T &&operator*() &&; |
| 125 | constexpr const T &&operator*() const &&; |
| 126 | constexpr explicit operator bool() const noexcept; |
| 127 | constexpr bool has_value() const noexcept; |
| 128 | constexpr T const &value() const &; |
| 129 | constexpr T &value() &; |
| 130 | constexpr T &&value() &&; |
| 131 | constexpr const T &&value() const &&; |
| 132 | template <class U> constexpr T value_or(U &&) const &; |
| 133 | template <class U> constexpr T value_or(U &&) &&; |
| 134 | |
Casey Carter | 84b8a40 | 2017-04-17 20:15:16 +0000 | [diff] [blame] | 135 | // 23.6.3.6, modifiers |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 136 | void reset() noexcept; // constexpr in C++20 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 137 | |
| 138 | private: |
| 139 | T *val; // exposition only |
| 140 | }; |
Marshall Clow | e3c71ae | 2018-05-25 02:08:49 +0000 | [diff] [blame] | 141 | |
| 142 | template<class T> |
| 143 | optional(T) -> optional<T>; |
| 144 | |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 145 | } // namespace std |
| 146 | |
| 147 | */ |
| 148 | |
Louis Dionne | 73912b2 | 2020-11-04 15:01:25 -0500 | [diff] [blame] | 149 | #include <__availability> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 150 | #include <__config> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 151 | #include <__debug> |
| 152 | #include <__functional_base> |
Arthur O'Dwyer | 7deec12 | 2021-03-24 18:19:12 -0400 | [diff] [blame] | 153 | #include <compare> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 154 | #include <functional> |
| 155 | #include <initializer_list> |
| 156 | #include <new> |
| 157 | #include <stdexcept> |
| 158 | #include <type_traits> |
| 159 | #include <utility> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 160 | #include <version> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 161 | |
| 162 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 163 | #pragma GCC system_header |
| 164 | #endif |
| 165 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 166 | _LIBCPP_PUSH_MACROS |
| 167 | #include <__undef_macros> |
| 168 | |
| 169 | |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 170 | namespace std // purposefully not using versioning namespace |
| 171 | { |
| 172 | |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 173 | class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access |
Marshall Clow | a627d4a | 2017-02-05 20:06:38 +0000 | [diff] [blame] | 174 | : public exception |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 175 | { |
| 176 | public: |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 177 | // Get the key function ~bad_optional_access() into the dylib |
Marshall Clow | 53fc037 | 2017-02-05 20:52:32 +0000 | [diff] [blame] | 178 | virtual ~bad_optional_access() _NOEXCEPT; |
Marshall Clow | a627d4a | 2017-02-05 20:06:38 +0000 | [diff] [blame] | 179 | virtual const char* what() const _NOEXCEPT; |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | } // std |
| 183 | |
| 184 | #if _LIBCPP_STD_VER > 14 |
| 185 | |
| 186 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 187 | |
| 188 | _LIBCPP_NORETURN |
| 189 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 190 | _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 191 | void __throw_bad_optional_access() { |
| 192 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 193 | throw bad_optional_access(); |
| 194 | #else |
| 195 | _VSTD::abort(); |
| 196 | #endif |
| 197 | } |
| 198 | |
| 199 | struct nullopt_t |
| 200 | { |
| 201 | struct __secret_tag { _LIBCPP_INLINE_VISIBILITY explicit __secret_tag() = default; }; |
| 202 | _LIBCPP_INLINE_VISIBILITY constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {} |
| 203 | }; |
| 204 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 205 | _LIBCPP_INLINE_VAR constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}}; |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 206 | |
| 207 | template <class _Tp, bool = is_trivially_destructible<_Tp>::value> |
| 208 | struct __optional_destruct_base; |
| 209 | |
| 210 | template <class _Tp> |
| 211 | struct __optional_destruct_base<_Tp, false> |
| 212 | { |
| 213 | typedef _Tp value_type; |
| 214 | static_assert(is_object_v<value_type>, |
| 215 | "instantiation of optional with a non-object type is undefined behavior"); |
| 216 | union |
| 217 | { |
| 218 | char __null_state_; |
| 219 | value_type __val_; |
| 220 | }; |
| 221 | bool __engaged_; |
| 222 | |
| 223 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 224 | _LIBCPP_CONSTEXPR_AFTER_CXX17 ~__optional_destruct_base() |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 225 | { |
| 226 | if (__engaged_) |
| 227 | __val_.~value_type(); |
| 228 | } |
| 229 | |
| 230 | _LIBCPP_INLINE_VISIBILITY |
| 231 | constexpr __optional_destruct_base() noexcept |
| 232 | : __null_state_(), |
| 233 | __engaged_(false) {} |
| 234 | |
| 235 | template <class... _Args> |
| 236 | _LIBCPP_INLINE_VISIBILITY |
| 237 | constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args) |
| 238 | : __val_(_VSTD::forward<_Args>(__args)...), |
| 239 | __engaged_(true) {} |
| 240 | |
| 241 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 242 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 243 | { |
| 244 | if (__engaged_) |
| 245 | { |
| 246 | __val_.~value_type(); |
| 247 | __engaged_ = false; |
| 248 | } |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | template <class _Tp> |
| 253 | struct __optional_destruct_base<_Tp, true> |
| 254 | { |
| 255 | typedef _Tp value_type; |
| 256 | static_assert(is_object_v<value_type>, |
| 257 | "instantiation of optional with a non-object type is undefined behavior"); |
| 258 | union |
| 259 | { |
| 260 | char __null_state_; |
| 261 | value_type __val_; |
| 262 | }; |
| 263 | bool __engaged_; |
| 264 | |
| 265 | _LIBCPP_INLINE_VISIBILITY |
| 266 | constexpr __optional_destruct_base() noexcept |
| 267 | : __null_state_(), |
| 268 | __engaged_(false) {} |
| 269 | |
| 270 | template <class... _Args> |
| 271 | _LIBCPP_INLINE_VISIBILITY |
| 272 | constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args) |
| 273 | : __val_(_VSTD::forward<_Args>(__args)...), |
| 274 | __engaged_(true) {} |
| 275 | |
| 276 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 277 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 278 | { |
| 279 | if (__engaged_) |
| 280 | { |
| 281 | __engaged_ = false; |
| 282 | } |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | template <class _Tp, bool = is_reference<_Tp>::value> |
| 287 | struct __optional_storage_base : __optional_destruct_base<_Tp> |
| 288 | { |
| 289 | using __base = __optional_destruct_base<_Tp>; |
| 290 | using value_type = _Tp; |
| 291 | using __base::__base; |
| 292 | |
| 293 | _LIBCPP_INLINE_VISIBILITY |
| 294 | constexpr bool has_value() const noexcept |
| 295 | { |
| 296 | return this->__engaged_; |
| 297 | } |
| 298 | |
| 299 | _LIBCPP_INLINE_VISIBILITY |
| 300 | constexpr value_type& __get() & noexcept |
| 301 | { |
| 302 | return this->__val_; |
| 303 | } |
| 304 | _LIBCPP_INLINE_VISIBILITY |
| 305 | constexpr const value_type& __get() const& noexcept |
| 306 | { |
| 307 | return this->__val_; |
| 308 | } |
| 309 | _LIBCPP_INLINE_VISIBILITY |
| 310 | constexpr value_type&& __get() && noexcept |
| 311 | { |
| 312 | return _VSTD::move(this->__val_); |
| 313 | } |
| 314 | _LIBCPP_INLINE_VISIBILITY |
| 315 | constexpr const value_type&& __get() const&& noexcept |
| 316 | { |
| 317 | return _VSTD::move(this->__val_); |
| 318 | } |
| 319 | |
| 320 | template <class... _Args> |
| 321 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 322 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct(_Args&&... __args) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 323 | { |
| 324 | _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage"); |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 325 | #if _LIBCPP_STD_VER > 17 |
| 326 | _VSTD::construct_at(_VSTD::addressof(this->__val_), _VSTD::forward<_Args>(__args)...); |
| 327 | #else |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 328 | ::new ((void*)_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Args>(__args)...); |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 329 | #endif |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 330 | this->__engaged_ = true; |
| 331 | } |
| 332 | |
| 333 | template <class _That> |
| 334 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 335 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct_from(_That&& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 336 | { |
| 337 | if (__opt.has_value()) |
| 338 | __construct(_VSTD::forward<_That>(__opt).__get()); |
| 339 | } |
| 340 | |
| 341 | template <class _That> |
| 342 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 343 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void __assign_from(_That&& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 344 | { |
| 345 | if (this->__engaged_ == __opt.has_value()) |
| 346 | { |
| 347 | if (this->__engaged_) |
| 348 | this->__val_ = _VSTD::forward<_That>(__opt).__get(); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | if (this->__engaged_) |
| 353 | this->reset(); |
| 354 | else |
| 355 | __construct(_VSTD::forward<_That>(__opt).__get()); |
| 356 | } |
| 357 | } |
| 358 | }; |
| 359 | |
| 360 | // optional<T&> is currently required ill-formed, however it may to be in the |
| 361 | // future. For this reason it has already been implemented to ensure we can |
| 362 | // make the change in an ABI compatible manner. |
| 363 | template <class _Tp> |
| 364 | struct __optional_storage_base<_Tp, true> |
| 365 | { |
| 366 | using value_type = _Tp; |
| 367 | using __raw_type = remove_reference_t<_Tp>; |
| 368 | __raw_type* __value_; |
| 369 | |
| 370 | template <class _Up> |
| 371 | static constexpr bool __can_bind_reference() { |
| 372 | using _RawUp = typename remove_reference<_Up>::type; |
| 373 | using _UpPtr = _RawUp*; |
| 374 | using _RawTp = typename remove_reference<_Tp>::type; |
| 375 | using _TpPtr = _RawTp*; |
| 376 | using _CheckLValueArg = integral_constant<bool, |
| 377 | (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value) |
| 378 | || is_same<_RawUp, reference_wrapper<_RawTp>>::value |
| 379 | || is_same<_RawUp, reference_wrapper<typename remove_const<_RawTp>::type>>::value |
| 380 | >; |
| 381 | return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value) |
| 382 | || (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value && |
| 383 | is_convertible<_UpPtr, _TpPtr>::value); |
| 384 | } |
| 385 | |
| 386 | _LIBCPP_INLINE_VISIBILITY |
| 387 | constexpr __optional_storage_base() noexcept |
| 388 | : __value_(nullptr) {} |
| 389 | |
| 390 | template <class _UArg> |
| 391 | _LIBCPP_INLINE_VISIBILITY |
| 392 | constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg) |
| 393 | : __value_(_VSTD::addressof(__uarg)) |
| 394 | { |
| 395 | static_assert(__can_bind_reference<_UArg>(), |
| 396 | "Attempted to construct a reference element in tuple from a " |
| 397 | "possible temporary"); |
| 398 | } |
| 399 | |
| 400 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 401 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept { __value_ = nullptr; } |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 402 | |
| 403 | _LIBCPP_INLINE_VISIBILITY |
| 404 | constexpr bool has_value() const noexcept |
| 405 | { return __value_ != nullptr; } |
| 406 | |
| 407 | _LIBCPP_INLINE_VISIBILITY |
| 408 | constexpr value_type& __get() const& noexcept |
| 409 | { return *__value_; } |
| 410 | |
| 411 | _LIBCPP_INLINE_VISIBILITY |
| 412 | constexpr value_type&& __get() const&& noexcept |
| 413 | { return _VSTD::forward<value_type>(*__value_); } |
| 414 | |
| 415 | template <class _UArg> |
| 416 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 417 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct(_UArg&& __val) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 418 | { |
| 419 | _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage"); |
| 420 | static_assert(__can_bind_reference<_UArg>(), |
| 421 | "Attempted to construct a reference element in tuple from a " |
| 422 | "possible temporary"); |
| 423 | __value_ = _VSTD::addressof(__val); |
| 424 | } |
| 425 | |
| 426 | template <class _That> |
| 427 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 428 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void __construct_from(_That&& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 429 | { |
| 430 | if (__opt.has_value()) |
| 431 | __construct(_VSTD::forward<_That>(__opt).__get()); |
| 432 | } |
| 433 | |
| 434 | template <class _That> |
| 435 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 436 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void __assign_from(_That&& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 437 | { |
| 438 | if (has_value() == __opt.has_value()) |
| 439 | { |
| 440 | if (has_value()) |
| 441 | *__value_ = _VSTD::forward<_That>(__opt).__get(); |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | if (has_value()) |
| 446 | reset(); |
| 447 | else |
| 448 | __construct(_VSTD::forward<_That>(__opt).__get()); |
| 449 | } |
| 450 | } |
| 451 | }; |
| 452 | |
Casey Carter | c189f7f | 2017-07-09 17:15:49 +0000 | [diff] [blame] | 453 | template <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value> |
| 454 | struct __optional_copy_base : __optional_storage_base<_Tp> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 455 | { |
| 456 | using __optional_storage_base<_Tp>::__optional_storage_base; |
| 457 | }; |
| 458 | |
| 459 | template <class _Tp> |
Casey Carter | c189f7f | 2017-07-09 17:15:49 +0000 | [diff] [blame] | 460 | struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 461 | { |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 462 | using __optional_storage_base<_Tp>::__optional_storage_base; |
| 463 | |
| 464 | _LIBCPP_INLINE_VISIBILITY |
Casey Carter | c189f7f | 2017-07-09 17:15:49 +0000 | [diff] [blame] | 465 | __optional_copy_base() = default; |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 466 | |
| 467 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 468 | _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_copy_base(const __optional_copy_base& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 469 | { |
| 470 | this->__construct_from(__opt); |
| 471 | } |
| 472 | |
| 473 | _LIBCPP_INLINE_VISIBILITY |
Casey Carter | c189f7f | 2017-07-09 17:15:49 +0000 | [diff] [blame] | 474 | __optional_copy_base(__optional_copy_base&&) = default; |
| 475 | _LIBCPP_INLINE_VISIBILITY |
| 476 | __optional_copy_base& operator=(const __optional_copy_base&) = default; |
| 477 | _LIBCPP_INLINE_VISIBILITY |
| 478 | __optional_copy_base& operator=(__optional_copy_base&&) = default; |
| 479 | }; |
| 480 | |
| 481 | template <class _Tp, bool = is_trivially_move_constructible<_Tp>::value> |
| 482 | struct __optional_move_base : __optional_copy_base<_Tp> |
| 483 | { |
| 484 | using __optional_copy_base<_Tp>::__optional_copy_base; |
| 485 | }; |
| 486 | |
| 487 | template <class _Tp> |
| 488 | struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> |
| 489 | { |
| 490 | using value_type = _Tp; |
| 491 | using __optional_copy_base<_Tp>::__optional_copy_base; |
| 492 | |
| 493 | _LIBCPP_INLINE_VISIBILITY |
| 494 | __optional_move_base() = default; |
| 495 | _LIBCPP_INLINE_VISIBILITY |
| 496 | __optional_move_base(const __optional_move_base&) = default; |
| 497 | |
| 498 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 499 | _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_move_base(__optional_move_base&& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 500 | noexcept(is_nothrow_move_constructible_v<value_type>) |
| 501 | { |
| 502 | this->__construct_from(_VSTD::move(__opt)); |
| 503 | } |
| 504 | |
| 505 | _LIBCPP_INLINE_VISIBILITY |
Casey Carter | c189f7f | 2017-07-09 17:15:49 +0000 | [diff] [blame] | 506 | __optional_move_base& operator=(const __optional_move_base&) = default; |
| 507 | _LIBCPP_INLINE_VISIBILITY |
| 508 | __optional_move_base& operator=(__optional_move_base&&) = default; |
| 509 | }; |
| 510 | |
| 511 | template <class _Tp, bool = |
| 512 | is_trivially_destructible<_Tp>::value && |
| 513 | is_trivially_copy_constructible<_Tp>::value && |
| 514 | is_trivially_copy_assignable<_Tp>::value> |
| 515 | struct __optional_copy_assign_base : __optional_move_base<_Tp> |
| 516 | { |
| 517 | using __optional_move_base<_Tp>::__optional_move_base; |
| 518 | }; |
| 519 | |
| 520 | template <class _Tp> |
| 521 | struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> |
| 522 | { |
| 523 | using __optional_move_base<_Tp>::__optional_move_base; |
| 524 | |
| 525 | _LIBCPP_INLINE_VISIBILITY |
| 526 | __optional_copy_assign_base() = default; |
| 527 | _LIBCPP_INLINE_VISIBILITY |
| 528 | __optional_copy_assign_base(const __optional_copy_assign_base&) = default; |
| 529 | _LIBCPP_INLINE_VISIBILITY |
| 530 | __optional_copy_assign_base(__optional_copy_assign_base&&) = default; |
| 531 | |
| 532 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 533 | _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_copy_assign_base& operator=(const __optional_copy_assign_base& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 534 | { |
| 535 | this->__assign_from(__opt); |
| 536 | return *this; |
| 537 | } |
| 538 | |
| 539 | _LIBCPP_INLINE_VISIBILITY |
Casey Carter | c189f7f | 2017-07-09 17:15:49 +0000 | [diff] [blame] | 540 | __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default; |
| 541 | }; |
| 542 | |
| 543 | template <class _Tp, bool = |
| 544 | is_trivially_destructible<_Tp>::value && |
| 545 | is_trivially_move_constructible<_Tp>::value && |
| 546 | is_trivially_move_assignable<_Tp>::value> |
| 547 | struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> |
| 548 | { |
| 549 | using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; |
| 550 | }; |
| 551 | |
| 552 | template <class _Tp> |
| 553 | struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> |
| 554 | { |
| 555 | using value_type = _Tp; |
| 556 | using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; |
| 557 | |
| 558 | _LIBCPP_INLINE_VISIBILITY |
| 559 | __optional_move_assign_base() = default; |
| 560 | _LIBCPP_INLINE_VISIBILITY |
| 561 | __optional_move_assign_base(const __optional_move_assign_base& __opt) = default; |
| 562 | _LIBCPP_INLINE_VISIBILITY |
| 563 | __optional_move_assign_base(__optional_move_assign_base&&) = default; |
| 564 | _LIBCPP_INLINE_VISIBILITY |
| 565 | __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default; |
| 566 | |
| 567 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 568 | _LIBCPP_CONSTEXPR_AFTER_CXX17 __optional_move_assign_base& operator=(__optional_move_assign_base&& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 569 | noexcept(is_nothrow_move_assignable_v<value_type> && |
| 570 | is_nothrow_move_constructible_v<value_type>) |
| 571 | { |
| 572 | this->__assign_from(_VSTD::move(__opt)); |
| 573 | return *this; |
| 574 | } |
| 575 | }; |
| 576 | |
| 577 | template <class _Tp> |
| 578 | using __optional_sfinae_ctor_base_t = __sfinae_ctor_base< |
| 579 | is_copy_constructible<_Tp>::value, |
| 580 | is_move_constructible<_Tp>::value |
| 581 | >; |
| 582 | |
| 583 | template <class _Tp> |
| 584 | using __optional_sfinae_assign_base_t = __sfinae_assign_base< |
| 585 | (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value), |
| 586 | (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value) |
| 587 | >; |
| 588 | |
| 589 | template <class _Tp> |
| 590 | class optional |
Casey Carter | c189f7f | 2017-07-09 17:15:49 +0000 | [diff] [blame] | 591 | : private __optional_move_assign_base<_Tp> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 592 | , private __optional_sfinae_ctor_base_t<_Tp> |
| 593 | , private __optional_sfinae_assign_base_t<_Tp> |
| 594 | { |
Casey Carter | c189f7f | 2017-07-09 17:15:49 +0000 | [diff] [blame] | 595 | using __base = __optional_move_assign_base<_Tp>; |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 596 | public: |
| 597 | using value_type = _Tp; |
| 598 | |
| 599 | private: |
| 600 | // Disable the reference extension using this static assert. |
Marshall Clow | fa6a334 | 2019-03-25 16:35:59 +0000 | [diff] [blame] | 601 | static_assert(!is_same_v<__uncvref_t<value_type>, in_place_t>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 602 | "instantiation of optional with in_place_t is ill-formed"); |
| 603 | static_assert(!is_same_v<__uncvref_t<value_type>, nullopt_t>, |
| 604 | "instantiation of optional with nullopt_t is ill-formed"); |
| 605 | static_assert(!is_reference_v<value_type>, |
| 606 | "instantiation of optional with a reference type is ill-formed"); |
| 607 | static_assert(is_destructible_v<value_type>, |
| 608 | "instantiation of optional with a non-destructible type is ill-formed"); |
Marshall Clow | fa6a334 | 2019-03-25 16:35:59 +0000 | [diff] [blame] | 609 | static_assert(!is_array_v<value_type>, |
| 610 | "instantiation of optional with an array type is ill-formed"); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 611 | |
| 612 | // LWG2756: conditionally explicit conversion from _Up |
| 613 | struct _CheckOptionalArgsConstructor { |
| 614 | template <class _Up> |
| 615 | static constexpr bool __enable_implicit() { |
| 616 | return is_constructible_v<_Tp, _Up&&> && |
| 617 | is_convertible_v<_Up&&, _Tp>; |
| 618 | } |
| 619 | |
| 620 | template <class _Up> |
| 621 | static constexpr bool __enable_explicit() { |
| 622 | return is_constructible_v<_Tp, _Up&&> && |
| 623 | !is_convertible_v<_Up&&, _Tp>; |
| 624 | } |
| 625 | }; |
| 626 | template <class _Up> |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 627 | using _CheckOptionalArgsCtor = _If< |
| 628 | _IsNotSame<__uncvref_t<_Up>, in_place_t>::value && |
| 629 | _IsNotSame<__uncvref_t<_Up>, optional>::value, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 630 | _CheckOptionalArgsConstructor, |
| 631 | __check_tuple_constructor_fail |
| 632 | >; |
| 633 | template <class _QualUp> |
| 634 | struct _CheckOptionalLikeConstructor { |
| 635 | template <class _Up, class _Opt = optional<_Up>> |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 636 | using __check_constructible_from_opt = _Or< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 637 | is_constructible<_Tp, _Opt&>, |
| 638 | is_constructible<_Tp, _Opt const&>, |
| 639 | is_constructible<_Tp, _Opt&&>, |
| 640 | is_constructible<_Tp, _Opt const&&>, |
| 641 | is_convertible<_Opt&, _Tp>, |
| 642 | is_convertible<_Opt const&, _Tp>, |
| 643 | is_convertible<_Opt&&, _Tp>, |
| 644 | is_convertible<_Opt const&&, _Tp> |
| 645 | >; |
| 646 | template <class _Up, class _Opt = optional<_Up>> |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 647 | using __check_assignable_from_opt = _Or< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 648 | is_assignable<_Tp&, _Opt&>, |
| 649 | is_assignable<_Tp&, _Opt const&>, |
| 650 | is_assignable<_Tp&, _Opt&&>, |
| 651 | is_assignable<_Tp&, _Opt const&&> |
| 652 | >; |
| 653 | template <class _Up, class _QUp = _QualUp> |
| 654 | static constexpr bool __enable_implicit() { |
| 655 | return is_convertible<_QUp, _Tp>::value && |
| 656 | !__check_constructible_from_opt<_Up>::value; |
| 657 | } |
| 658 | template <class _Up, class _QUp = _QualUp> |
| 659 | static constexpr bool __enable_explicit() { |
| 660 | return !is_convertible<_QUp, _Tp>::value && |
| 661 | !__check_constructible_from_opt<_Up>::value; |
| 662 | } |
| 663 | template <class _Up, class _QUp = _QualUp> |
| 664 | static constexpr bool __enable_assign() { |
Arthur O'Dwyer | df5df56 | 2020-12-12 11:57:32 -0500 | [diff] [blame] | 665 | // Construction and assignability of _QUp to _Tp has already been |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 666 | // checked. |
| 667 | return !__check_constructible_from_opt<_Up>::value && |
| 668 | !__check_assignable_from_opt<_Up>::value; |
| 669 | } |
| 670 | }; |
| 671 | |
| 672 | template <class _Up, class _QualUp> |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 673 | using _CheckOptionalLikeCtor = _If< |
| 674 | _And< |
| 675 | _IsNotSame<_Up, _Tp>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 676 | is_constructible<_Tp, _QualUp> |
| 677 | >::value, |
| 678 | _CheckOptionalLikeConstructor<_QualUp>, |
| 679 | __check_tuple_constructor_fail |
| 680 | >; |
| 681 | template <class _Up, class _QualUp> |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 682 | using _CheckOptionalLikeAssign = _If< |
| 683 | _And< |
| 684 | _IsNotSame<_Up, _Tp>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 685 | is_constructible<_Tp, _QualUp>, |
| 686 | is_assignable<_Tp&, _QualUp> |
| 687 | >::value, |
| 688 | _CheckOptionalLikeConstructor<_QualUp>, |
| 689 | __check_tuple_constructor_fail |
| 690 | >; |
| 691 | public: |
| 692 | |
| 693 | _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {} |
Marshall Clow | d44e2e3 | 2017-05-17 15:30:01 +0000 | [diff] [blame] | 694 | _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default; |
| 695 | _LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default; |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 696 | _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {} |
| 697 | |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 698 | template <class _InPlaceT, class... _Args, class = _EnableIf< |
| 699 | _And< |
| 700 | _IsSame<_InPlaceT, in_place_t>, |
| 701 | is_constructible<value_type, _Args...> |
Eric Fiselier | 8210393 | 2019-03-11 22:55:21 +0000 | [diff] [blame] | 702 | >::value |
| 703 | > |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 704 | > |
| 705 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 8210393 | 2019-03-11 22:55:21 +0000 | [diff] [blame] | 706 | constexpr explicit optional(_InPlaceT, _Args&&... __args) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 707 | : __base(in_place, _VSTD::forward<_Args>(__args)...) {} |
| 708 | |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 709 | template <class _Up, class... _Args, class = _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 710 | is_constructible_v<value_type, initializer_list<_Up>&, _Args...>> |
| 711 | > |
| 712 | _LIBCPP_INLINE_VISIBILITY |
| 713 | constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args) |
| 714 | : __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {} |
| 715 | |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 716 | template <class _Up = value_type, _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 717 | _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>() |
| 718 | , int> = 0> |
| 719 | _LIBCPP_INLINE_VISIBILITY |
| 720 | constexpr optional(_Up&& __v) |
| 721 | : __base(in_place, _VSTD::forward<_Up>(__v)) {} |
| 722 | |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 723 | template <class _Up, _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 724 | _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>() |
| 725 | , int> = 0> |
| 726 | _LIBCPP_INLINE_VISIBILITY |
| 727 | constexpr explicit optional(_Up&& __v) |
| 728 | : __base(in_place, _VSTD::forward<_Up>(__v)) {} |
| 729 | |
| 730 | // LWG2756: conditionally explicit conversion from const optional<_Up>& |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 731 | template <class _Up, _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 732 | _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>() |
| 733 | , int> = 0> |
| 734 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 735 | _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(const optional<_Up>& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 736 | { |
| 737 | this->__construct_from(__v); |
| 738 | } |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 739 | template <class _Up, _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 740 | _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>() |
| 741 | , int> = 0> |
| 742 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 743 | _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(const optional<_Up>& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 744 | { |
| 745 | this->__construct_from(__v); |
| 746 | } |
| 747 | |
| 748 | // LWG2756: conditionally explicit conversion from optional<_Up>&& |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 749 | template <class _Up, _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 750 | _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_implicit<_Up>() |
| 751 | , int> = 0> |
| 752 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 753 | _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(optional<_Up>&& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 754 | { |
| 755 | this->__construct_from(_VSTD::move(__v)); |
| 756 | } |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 757 | template <class _Up, _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 758 | _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_explicit<_Up>() |
| 759 | , int> = 0> |
| 760 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 761 | _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(optional<_Up>&& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 762 | { |
| 763 | this->__construct_from(_VSTD::move(__v)); |
| 764 | } |
| 765 | |
| 766 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 767 | _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& operator=(nullopt_t) noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 768 | { |
| 769 | reset(); |
| 770 | return *this; |
| 771 | } |
| 772 | |
| 773 | _LIBCPP_INLINE_VISIBILITY optional& operator=(const optional&) = default; |
| 774 | _LIBCPP_INLINE_VISIBILITY optional& operator=(optional&&) = default; |
| 775 | |
| 776 | // LWG2756 |
| 777 | template <class _Up = value_type, |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 778 | class = _EnableIf< |
| 779 | _And< |
| 780 | _IsNotSame<__uncvref_t<_Up>, optional>, |
| 781 | _Or< |
Marshall Clow | 9931511 | 2019-06-27 18:40:55 +0000 | [diff] [blame] | 782 | _IsNotSame<__uncvref_t<_Up>, value_type>, |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 783 | _Not<is_scalar<value_type>> |
Eric Fiselier | b901426 | 2016-10-16 03:21:35 +0000 | [diff] [blame] | 784 | >, |
| 785 | is_constructible<value_type, _Up>, |
| 786 | is_assignable<value_type&, _Up> |
| 787 | >::value> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 788 | > |
| 789 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 790 | _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 791 | operator=(_Up&& __v) |
| 792 | { |
| 793 | if (this->has_value()) |
| 794 | this->__get() = _VSTD::forward<_Up>(__v); |
| 795 | else |
| 796 | this->__construct(_VSTD::forward<_Up>(__v)); |
| 797 | return *this; |
| 798 | } |
| 799 | |
| 800 | // LWG2756 |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 801 | template <class _Up, _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 802 | _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>() |
| 803 | , int> = 0> |
| 804 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 805 | _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 806 | operator=(const optional<_Up>& __v) |
| 807 | { |
| 808 | this->__assign_from(__v); |
| 809 | return *this; |
| 810 | } |
| 811 | |
| 812 | // LWG2756 |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 813 | template <class _Up, _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 814 | _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_assign<_Up>() |
| 815 | , int> = 0> |
| 816 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 817 | _LIBCPP_CONSTEXPR_AFTER_CXX17 optional& |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 818 | operator=(optional<_Up>&& __v) |
| 819 | { |
| 820 | this->__assign_from(_VSTD::move(__v)); |
| 821 | return *this; |
| 822 | } |
| 823 | |
| 824 | template <class... _Args, |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 825 | class = _EnableIf |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 826 | < |
| 827 | is_constructible_v<value_type, _Args...> |
| 828 | > |
| 829 | > |
| 830 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 831 | _LIBCPP_CONSTEXPR_AFTER_CXX17 _Tp & |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 832 | emplace(_Args&&... __args) |
| 833 | { |
| 834 | reset(); |
| 835 | this->__construct(_VSTD::forward<_Args>(__args)...); |
Marshall Clow | 45ff982 | 2017-04-12 22:51:27 +0000 | [diff] [blame] | 836 | return this->__get(); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | template <class _Up, class... _Args, |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 840 | class = _EnableIf |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 841 | < |
| 842 | is_constructible_v<value_type, initializer_list<_Up>&, _Args...> |
| 843 | > |
| 844 | > |
| 845 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 846 | _LIBCPP_CONSTEXPR_AFTER_CXX17 _Tp & |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 847 | emplace(initializer_list<_Up> __il, _Args&&... __args) |
| 848 | { |
| 849 | reset(); |
| 850 | this->__construct(__il, _VSTD::forward<_Args>(__args)...); |
Marshall Clow | 45ff982 | 2017-04-12 22:51:27 +0000 | [diff] [blame] | 851 | return this->__get(); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | _LIBCPP_INLINE_VISIBILITY |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 855 | _LIBCPP_CONSTEXPR_AFTER_CXX17 void swap(optional& __opt) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 856 | noexcept(is_nothrow_move_constructible_v<value_type> && |
| 857 | is_nothrow_swappable_v<value_type>) |
| 858 | { |
| 859 | if (this->has_value() == __opt.has_value()) |
| 860 | { |
| 861 | using _VSTD::swap; |
| 862 | if (this->has_value()) |
| 863 | swap(this->__get(), __opt.__get()); |
| 864 | } |
| 865 | else |
| 866 | { |
| 867 | if (this->has_value()) |
| 868 | { |
| 869 | __opt.__construct(_VSTD::move(this->__get())); |
| 870 | reset(); |
| 871 | } |
| 872 | else |
| 873 | { |
| 874 | this->__construct(_VSTD::move(__opt.__get())); |
| 875 | __opt.reset(); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | _LIBCPP_INLINE_VISIBILITY |
| 881 | constexpr |
| 882 | add_pointer_t<value_type const> |
| 883 | operator->() const |
| 884 | { |
Kristina Bessonova | aeeaa7e | 2021-05-09 19:29:56 +0200 | [diff] [blame] | 885 | _LIBCPP_ASSERT(this->has_value(), "optional operator-> called on a disengaged value"); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 886 | return _VSTD::addressof(this->__get()); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | _LIBCPP_INLINE_VISIBILITY |
| 890 | constexpr |
| 891 | add_pointer_t<value_type> |
| 892 | operator->() |
| 893 | { |
Kristina Bessonova | aeeaa7e | 2021-05-09 19:29:56 +0200 | [diff] [blame] | 894 | _LIBCPP_ASSERT(this->has_value(), "optional operator-> called on a disengaged value"); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 895 | return _VSTD::addressof(this->__get()); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | _LIBCPP_INLINE_VISIBILITY |
| 899 | constexpr |
| 900 | const value_type& |
zoecarver | 73efa75 | 2021-07-01 10:18:27 -0700 | [diff] [blame] | 901 | operator*() const& noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 902 | { |
Kristina Bessonova | aeeaa7e | 2021-05-09 19:29:56 +0200 | [diff] [blame] | 903 | _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value"); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 904 | return this->__get(); |
| 905 | } |
| 906 | |
| 907 | _LIBCPP_INLINE_VISIBILITY |
| 908 | constexpr |
| 909 | value_type& |
zoecarver | 73efa75 | 2021-07-01 10:18:27 -0700 | [diff] [blame] | 910 | operator*() & noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 911 | { |
Kristina Bessonova | aeeaa7e | 2021-05-09 19:29:56 +0200 | [diff] [blame] | 912 | _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value"); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 913 | return this->__get(); |
| 914 | } |
| 915 | |
| 916 | _LIBCPP_INLINE_VISIBILITY |
| 917 | constexpr |
| 918 | value_type&& |
zoecarver | 73efa75 | 2021-07-01 10:18:27 -0700 | [diff] [blame] | 919 | operator*() && noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 920 | { |
Kristina Bessonova | aeeaa7e | 2021-05-09 19:29:56 +0200 | [diff] [blame] | 921 | _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value"); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 922 | return _VSTD::move(this->__get()); |
| 923 | } |
| 924 | |
| 925 | _LIBCPP_INLINE_VISIBILITY |
| 926 | constexpr |
| 927 | const value_type&& |
zoecarver | 73efa75 | 2021-07-01 10:18:27 -0700 | [diff] [blame] | 928 | operator*() const&& noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 929 | { |
Kristina Bessonova | aeeaa7e | 2021-05-09 19:29:56 +0200 | [diff] [blame] | 930 | _LIBCPP_ASSERT(this->has_value(), "optional operator* called on a disengaged value"); |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 931 | return _VSTD::move(this->__get()); |
| 932 | } |
| 933 | |
| 934 | _LIBCPP_INLINE_VISIBILITY |
| 935 | constexpr explicit operator bool() const noexcept { return has_value(); } |
| 936 | |
| 937 | using __base::has_value; |
| 938 | using __base::__get; |
| 939 | |
| 940 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 941 | _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 942 | constexpr value_type const& value() const& |
| 943 | { |
| 944 | if (!this->has_value()) |
| 945 | __throw_bad_optional_access(); |
| 946 | return this->__get(); |
| 947 | } |
| 948 | |
| 949 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 950 | _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 951 | constexpr value_type& value() & |
| 952 | { |
| 953 | if (!this->has_value()) |
| 954 | __throw_bad_optional_access(); |
| 955 | return this->__get(); |
| 956 | } |
| 957 | |
| 958 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 959 | _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 960 | constexpr value_type&& value() && |
| 961 | { |
| 962 | if (!this->has_value()) |
| 963 | __throw_bad_optional_access(); |
| 964 | return _VSTD::move(this->__get()); |
| 965 | } |
| 966 | |
| 967 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 968 | _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 969 | constexpr value_type const&& value() const&& |
| 970 | { |
| 971 | if (!this->has_value()) |
| 972 | __throw_bad_optional_access(); |
| 973 | return _VSTD::move(this->__get()); |
| 974 | } |
| 975 | |
| 976 | template <class _Up> |
| 977 | _LIBCPP_INLINE_VISIBILITY |
| 978 | constexpr value_type value_or(_Up&& __v) const& |
| 979 | { |
| 980 | static_assert(is_copy_constructible_v<value_type>, |
| 981 | "optional<T>::value_or: T must be copy constructible"); |
| 982 | static_assert(is_convertible_v<_Up, value_type>, |
| 983 | "optional<T>::value_or: U must be convertible to T"); |
| 984 | return this->has_value() ? this->__get() : |
| 985 | static_cast<value_type>(_VSTD::forward<_Up>(__v)); |
| 986 | } |
| 987 | |
| 988 | template <class _Up> |
| 989 | _LIBCPP_INLINE_VISIBILITY |
Casey Carter | cb05fae | 2017-06-06 18:47:26 +0000 | [diff] [blame] | 990 | constexpr value_type value_or(_Up&& __v) && |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 991 | { |
| 992 | static_assert(is_move_constructible_v<value_type>, |
| 993 | "optional<T>::value_or: T must be move constructible"); |
| 994 | static_assert(is_convertible_v<_Up, value_type>, |
| 995 | "optional<T>::value_or: U must be convertible to T"); |
| 996 | return this->has_value() ? _VSTD::move(this->__get()) : |
| 997 | static_cast<value_type>(_VSTD::forward<_Up>(__v)); |
| 998 | } |
| 999 | |
| 1000 | using __base::reset; |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1001 | }; |
| 1002 | |
Louis Dionne | d59f8a5 | 2021-08-17 11:59:07 -0400 | [diff] [blame^] | 1003 | #if _LIBCPP_STD_VER >= 17 |
Marshall Clow | e3c71ae | 2018-05-25 02:08:49 +0000 | [diff] [blame] | 1004 | template<class T> |
| 1005 | optional(T) -> optional<T>; |
| 1006 | #endif |
| 1007 | |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1008 | // Comparisons between optionals |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1009 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1010 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1011 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1012 | is_convertible_v<decltype(declval<const _Tp&>() == |
| 1013 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1014 | bool |
| 1015 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1016 | operator==(const optional<_Tp>& __x, const optional<_Up>& __y) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1017 | { |
| 1018 | if (static_cast<bool>(__x) != static_cast<bool>(__y)) |
| 1019 | return false; |
| 1020 | if (!static_cast<bool>(__x)) |
| 1021 | return true; |
| 1022 | return *__x == *__y; |
| 1023 | } |
| 1024 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1025 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1026 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1027 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1028 | is_convertible_v<decltype(declval<const _Tp&>() != |
| 1029 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1030 | bool |
| 1031 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1032 | operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1033 | { |
| 1034 | if (static_cast<bool>(__x) != static_cast<bool>(__y)) |
| 1035 | return true; |
| 1036 | if (!static_cast<bool>(__x)) |
| 1037 | return false; |
| 1038 | return *__x != *__y; |
| 1039 | } |
| 1040 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1041 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1042 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1043 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1044 | is_convertible_v<decltype(declval<const _Tp&>() < |
| 1045 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1046 | bool |
| 1047 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1048 | operator<(const optional<_Tp>& __x, const optional<_Up>& __y) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1049 | { |
| 1050 | if (!static_cast<bool>(__y)) |
| 1051 | return false; |
| 1052 | if (!static_cast<bool>(__x)) |
| 1053 | return true; |
| 1054 | return *__x < *__y; |
| 1055 | } |
| 1056 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1057 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1058 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1059 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1060 | is_convertible_v<decltype(declval<const _Tp&>() > |
| 1061 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1062 | bool |
| 1063 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1064 | operator>(const optional<_Tp>& __x, const optional<_Up>& __y) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1065 | { |
| 1066 | if (!static_cast<bool>(__x)) |
| 1067 | return false; |
| 1068 | if (!static_cast<bool>(__y)) |
| 1069 | return true; |
| 1070 | return *__x > *__y; |
| 1071 | } |
| 1072 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1073 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1074 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1075 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1076 | is_convertible_v<decltype(declval<const _Tp&>() <= |
| 1077 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1078 | bool |
| 1079 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1080 | operator<=(const optional<_Tp>& __x, const optional<_Up>& __y) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1081 | { |
| 1082 | if (!static_cast<bool>(__x)) |
| 1083 | return true; |
| 1084 | if (!static_cast<bool>(__y)) |
| 1085 | return false; |
| 1086 | return *__x <= *__y; |
| 1087 | } |
| 1088 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1089 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1090 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1091 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1092 | is_convertible_v<decltype(declval<const _Tp&>() >= |
| 1093 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1094 | bool |
| 1095 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1096 | operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1097 | { |
| 1098 | if (!static_cast<bool>(__y)) |
| 1099 | return true; |
| 1100 | if (!static_cast<bool>(__x)) |
| 1101 | return false; |
| 1102 | return *__x >= *__y; |
| 1103 | } |
| 1104 | |
| 1105 | // Comparisons with nullopt |
| 1106 | template <class _Tp> |
| 1107 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1108 | bool |
| 1109 | operator==(const optional<_Tp>& __x, nullopt_t) noexcept |
| 1110 | { |
| 1111 | return !static_cast<bool>(__x); |
| 1112 | } |
| 1113 | |
| 1114 | template <class _Tp> |
| 1115 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1116 | bool |
| 1117 | operator==(nullopt_t, const optional<_Tp>& __x) noexcept |
| 1118 | { |
| 1119 | return !static_cast<bool>(__x); |
| 1120 | } |
| 1121 | |
| 1122 | template <class _Tp> |
| 1123 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1124 | bool |
| 1125 | operator!=(const optional<_Tp>& __x, nullopt_t) noexcept |
| 1126 | { |
| 1127 | return static_cast<bool>(__x); |
| 1128 | } |
| 1129 | |
| 1130 | template <class _Tp> |
| 1131 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1132 | bool |
| 1133 | operator!=(nullopt_t, const optional<_Tp>& __x) noexcept |
| 1134 | { |
| 1135 | return static_cast<bool>(__x); |
| 1136 | } |
| 1137 | |
| 1138 | template <class _Tp> |
| 1139 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1140 | bool |
| 1141 | operator<(const optional<_Tp>&, nullopt_t) noexcept |
| 1142 | { |
| 1143 | return false; |
| 1144 | } |
| 1145 | |
| 1146 | template <class _Tp> |
| 1147 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1148 | bool |
| 1149 | operator<(nullopt_t, const optional<_Tp>& __x) noexcept |
| 1150 | { |
| 1151 | return static_cast<bool>(__x); |
| 1152 | } |
| 1153 | |
| 1154 | template <class _Tp> |
| 1155 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1156 | bool |
| 1157 | operator<=(const optional<_Tp>& __x, nullopt_t) noexcept |
| 1158 | { |
| 1159 | return !static_cast<bool>(__x); |
| 1160 | } |
| 1161 | |
| 1162 | template <class _Tp> |
| 1163 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1164 | bool |
Eric Fiselier | 6003c77 | 2016-12-23 23:37:52 +0000 | [diff] [blame] | 1165 | operator<=(nullopt_t, const optional<_Tp>&) noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1166 | { |
| 1167 | return true; |
| 1168 | } |
| 1169 | |
| 1170 | template <class _Tp> |
| 1171 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1172 | bool |
| 1173 | operator>(const optional<_Tp>& __x, nullopt_t) noexcept |
| 1174 | { |
| 1175 | return static_cast<bool>(__x); |
| 1176 | } |
| 1177 | |
| 1178 | template <class _Tp> |
| 1179 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1180 | bool |
Eric Fiselier | 6003c77 | 2016-12-23 23:37:52 +0000 | [diff] [blame] | 1181 | operator>(nullopt_t, const optional<_Tp>&) noexcept |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1182 | { |
| 1183 | return false; |
| 1184 | } |
| 1185 | |
| 1186 | template <class _Tp> |
| 1187 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1188 | bool |
| 1189 | operator>=(const optional<_Tp>&, nullopt_t) noexcept |
| 1190 | { |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
| 1194 | template <class _Tp> |
| 1195 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1196 | bool |
| 1197 | operator>=(nullopt_t, const optional<_Tp>& __x) noexcept |
| 1198 | { |
| 1199 | return !static_cast<bool>(__x); |
| 1200 | } |
| 1201 | |
| 1202 | // Comparisons with T |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1203 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1204 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1205 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1206 | is_convertible_v<decltype(declval<const _Tp&>() == |
| 1207 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1208 | bool |
| 1209 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1210 | operator==(const optional<_Tp>& __x, const _Up& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1211 | { |
| 1212 | return static_cast<bool>(__x) ? *__x == __v : false; |
| 1213 | } |
| 1214 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1215 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1216 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1217 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1218 | is_convertible_v<decltype(declval<const _Tp&>() == |
| 1219 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1220 | bool |
| 1221 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1222 | operator==(const _Tp& __v, const optional<_Up>& __x) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1223 | { |
| 1224 | return static_cast<bool>(__x) ? __v == *__x : false; |
| 1225 | } |
| 1226 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1227 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1228 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1229 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1230 | is_convertible_v<decltype(declval<const _Tp&>() != |
| 1231 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1232 | bool |
| 1233 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1234 | operator!=(const optional<_Tp>& __x, const _Up& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1235 | { |
| 1236 | return static_cast<bool>(__x) ? *__x != __v : true; |
| 1237 | } |
| 1238 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1239 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1240 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1241 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1242 | is_convertible_v<decltype(declval<const _Tp&>() != |
| 1243 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1244 | bool |
| 1245 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1246 | operator!=(const _Tp& __v, const optional<_Up>& __x) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1247 | { |
| 1248 | return static_cast<bool>(__x) ? __v != *__x : true; |
| 1249 | } |
| 1250 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1251 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1252 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1253 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1254 | is_convertible_v<decltype(declval<const _Tp&>() < |
| 1255 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1256 | bool |
| 1257 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1258 | operator<(const optional<_Tp>& __x, const _Up& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1259 | { |
| 1260 | return static_cast<bool>(__x) ? *__x < __v : true; |
| 1261 | } |
| 1262 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1263 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1264 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1265 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1266 | is_convertible_v<decltype(declval<const _Tp&>() < |
| 1267 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1268 | bool |
| 1269 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1270 | operator<(const _Tp& __v, const optional<_Up>& __x) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1271 | { |
| 1272 | return static_cast<bool>(__x) ? __v < *__x : false; |
| 1273 | } |
| 1274 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1275 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1276 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1277 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1278 | is_convertible_v<decltype(declval<const _Tp&>() <= |
| 1279 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1280 | bool |
| 1281 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1282 | operator<=(const optional<_Tp>& __x, const _Up& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1283 | { |
| 1284 | return static_cast<bool>(__x) ? *__x <= __v : true; |
| 1285 | } |
| 1286 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1287 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1288 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1289 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1290 | is_convertible_v<decltype(declval<const _Tp&>() <= |
| 1291 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1292 | bool |
| 1293 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1294 | operator<=(const _Tp& __v, const optional<_Up>& __x) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1295 | { |
| 1296 | return static_cast<bool>(__x) ? __v <= *__x : false; |
| 1297 | } |
| 1298 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1299 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1300 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1301 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1302 | is_convertible_v<decltype(declval<const _Tp&>() > |
| 1303 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1304 | bool |
| 1305 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1306 | operator>(const optional<_Tp>& __x, const _Up& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1307 | { |
| 1308 | return static_cast<bool>(__x) ? *__x > __v : false; |
| 1309 | } |
| 1310 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1311 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1312 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1313 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1314 | is_convertible_v<decltype(declval<const _Tp&>() > |
| 1315 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1316 | bool |
| 1317 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1318 | operator>(const _Tp& __v, const optional<_Up>& __x) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1319 | { |
| 1320 | return static_cast<bool>(__x) ? __v > *__x : true; |
| 1321 | } |
| 1322 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1323 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1324 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1325 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1326 | is_convertible_v<decltype(declval<const _Tp&>() >= |
| 1327 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1328 | bool |
| 1329 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1330 | operator>=(const optional<_Tp>& __x, const _Up& __v) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1331 | { |
| 1332 | return static_cast<bool>(__x) ? *__x >= __v : false; |
| 1333 | } |
| 1334 | |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1335 | template <class _Tp, class _Up> |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1336 | _LIBCPP_INLINE_VISIBILITY constexpr |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1337 | _EnableIf< |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1338 | is_convertible_v<decltype(declval<const _Tp&>() >= |
| 1339 | declval<const _Up&>()), bool>, |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1340 | bool |
| 1341 | > |
Eric Fiselier | 8af9014 | 2017-03-30 20:06:52 +0000 | [diff] [blame] | 1342 | operator>=(const _Tp& __v, const optional<_Up>& __x) |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1343 | { |
| 1344 | return static_cast<bool>(__x) ? __v >= *__x : true; |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | template <class _Tp> |
Christopher Di Bella | 80e8f64 | 2021-05-09 01:30:32 +0000 | [diff] [blame] | 1349 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Eric Fiselier | 3906a13 | 2019-06-23 20:28:29 +0000 | [diff] [blame] | 1350 | _EnableIf< |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1351 | is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, |
| 1352 | void |
| 1353 | > |
| 1354 | swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) |
| 1355 | { |
| 1356 | __x.swap(__y); |
| 1357 | } |
| 1358 | |
| 1359 | template <class _Tp> |
| 1360 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1361 | optional<decay_t<_Tp>> make_optional(_Tp&& __v) |
| 1362 | { |
| 1363 | return optional<decay_t<_Tp>>(_VSTD::forward<_Tp>(__v)); |
| 1364 | } |
| 1365 | |
| 1366 | template <class _Tp, class... _Args> |
| 1367 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1368 | optional<_Tp> make_optional(_Args&&... __args) |
| 1369 | { |
| 1370 | return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...); |
| 1371 | } |
| 1372 | |
| 1373 | template <class _Tp, class _Up, class... _Args> |
| 1374 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 1375 | optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) |
| 1376 | { |
| 1377 | return optional<_Tp>(in_place, __il, _VSTD::forward<_Args>(__args)...); |
| 1378 | } |
| 1379 | |
| 1380 | template <class _Tp> |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 1381 | struct _LIBCPP_TEMPLATE_VIS hash< |
| 1382 | __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> |
| 1383 | > |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1384 | { |
Arthur O'Dwyer | f5486c8 | 2021-05-25 14:34:18 -0400 | [diff] [blame] | 1385 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) |
| 1386 | _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type; |
| 1387 | _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type; |
| 1388 | #endif |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1389 | |
| 1390 | _LIBCPP_INLINE_VISIBILITY |
Arthur O'Dwyer | f5486c8 | 2021-05-25 14:34:18 -0400 | [diff] [blame] | 1391 | size_t operator()(const optional<_Tp>& __opt) const |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1392 | { |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 1393 | return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0; |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1394 | } |
| 1395 | }; |
| 1396 | |
| 1397 | _LIBCPP_END_NAMESPACE_STD |
| 1398 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1399 | #endif // _LIBCPP_STD_VER > 14 |
Eric Fiselier | d4ec635 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 1400 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 1401 | _LIBCPP_POP_MACROS |
| 1402 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1403 | #endif // _LIBCPP_OPTIONAL |