| 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 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 |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_OPTIONAL |
| 11 | #define _LIBCPP_OPTIONAL |
| 12 | |
| 13 | /* |
| 14 | optional synopsis |
| 15 | |
| 16 | // C++1z |
| 17 | |
| 18 | namespace std { |
| 19 | // [optional.optional], class template optional |
| 20 | template <class T> |
| 21 | class optional; |
| 22 | |
| 23 | template<class T> |
| 24 | constexpr bool ranges::enable_view<optional<T>> = true; |
| 25 | template<class T> |
| 26 | constexpr auto format_kind<optional<T>> = range_format::disabled; |
| 27 | |
| 28 | template<class T> |
| 29 | concept is-derived-from-optional = requires(const T& t) { // exposition only |
| 30 | []<class U>(const optional<U>&){ }(t); |
| 31 | }; |
| 32 | |
| 33 | // [optional.nullopt], no-value state indicator |
| 34 | struct nullopt_t{see below }; |
| 35 | inline constexpr nullopt_t nullopt(unspecified ); |
| 36 | |
| 37 | // [optional.bad.access], class bad_optional_access |
| 38 | class bad_optional_access; |
| 39 | |
| 40 | // [optional.relops], relational operators |
| 41 | template <class T, class U> |
| 42 | constexpr bool operator==(const optional<T>&, const optional<U>&); |
| 43 | template <class T, class U> |
| 44 | constexpr bool operator!=(const optional<T>&, const optional<U>&); |
| 45 | template <class T, class U> |
| 46 | constexpr bool operator<(const optional<T>&, const optional<U>&); |
| 47 | template <class T, class U> |
| 48 | constexpr bool operator>(const optional<T>&, const optional<U>&); |
| 49 | template <class T, class U> |
| 50 | constexpr bool operator<=(const optional<T>&, const optional<U>&); |
| 51 | template <class T, class U> |
| 52 | constexpr bool operator>=(const optional<T>&, const optional<U>&); |
| 53 | template<class T, three_way_comparable_with<T> U> |
| 54 | constexpr compare_three_way_result_t<T, U> |
| 55 | operator<=>(const optional<T>&, const optional<U>&); // since C++20 |
| 56 | |
| 57 | // [optional.nullops], comparison with nullopt |
| 58 | template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept; |
| 59 | template<class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept; // until C++17 |
| 60 | template<class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; // until C++17 |
| 61 | template<class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; // until C++17 |
| 62 | template<class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept; // until C++17 |
| 63 | template<class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept; // until C++17 |
| 64 | template<class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; // until C++17 |
| 65 | template<class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; // until C++17 |
| 66 | template<class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept; // until C++17 |
| 67 | template<class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept; // until C++17 |
| 68 | template<class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; // until C++17 |
| 69 | template<class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; // until C++17 |
| 70 | template<class T> |
| 71 | constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // since C++20 |
| 72 | |
| 73 | // [optional.comp.with.t], comparison with T |
| 74 | template<class T, class U> constexpr bool operator==(const optional<T>&, const U&); |
| 75 | template<class T, class U> constexpr bool operator==(const T&, const optional<U>&); |
| 76 | template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&); |
| 77 | template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&); |
| 78 | template<class T, class U> constexpr bool operator<(const optional<T>&, const U&); |
| 79 | template<class T, class U> constexpr bool operator<(const T&, const optional<U>&); |
| 80 | template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&); |
| 81 | template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&); |
| 82 | template<class T, class U> constexpr bool operator>(const optional<T>&, const U&); |
| 83 | template<class T, class U> constexpr bool operator>(const T&, const optional<U>&); |
| 84 | template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&); |
| 85 | template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&); |
| 86 | template<class T, class U> |
| 87 | requires (!is-derived-from-optional<U>) && three_way_comparable_with<T, U> |
| 88 | constexpr compare_three_way_result_t<T, U> |
| 89 | operator<=>(const optional<T>&, const U&); // since C++20 |
| 90 | |
| 91 | // [optional.specalg], specialized algorithms |
| 92 | template<class T> |
| 93 | void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20 |
| 94 | |
| 95 | template<class T> |
| 96 | constexpr optional<see below > make_optional(T&&); |
| 97 | template<class T, class... Args> |
| 98 | constexpr optional<T> make_optional(Args&&... args); |
| 99 | template<class T, class U, class... Args> |
| 100 | constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args); |
| 101 | |
| 102 | // [optional.hash], hash support |
| 103 | template<class T> struct hash; |
| 104 | template<class T> struct hash<optional<T>>; |
| 105 | |
| 106 | template<class T> |
| 107 | class optional { |
| 108 | public: |
| 109 | using value_type = T; |
| 110 | using iterator = implementation-defined; // see [optional.iterators] |
| 111 | using const_iterator = implementation-defined; // see [optional.iterators] |
| 112 | |
| 113 | // [optional.ctor], constructors |
| 114 | constexpr optional() noexcept; |
| 115 | constexpr optional(nullopt_t) noexcept; |
| 116 | constexpr optional(const optional &); |
| 117 | constexpr optional(optional &&) noexcept(see below); |
| 118 | template<class... Args> |
| 119 | constexpr explicit optional(in_place_t, Args &&...); |
| 120 | template<class U, class... Args> |
| 121 | constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...); |
| 122 | template<class U = remove_cv_t<T>> |
| 123 | constexpr explicit(see-below) optional(U &&); |
| 124 | template<class U> |
| 125 | explicit(see-below) optional(const optional<U> &); // constexpr in C++20 |
| 126 | template<class U> |
| 127 | explicit(see-below) optional(optional<U> &&); // constexpr in C++20 |
| 128 | |
| 129 | // [optional.dtor], destructor |
| 130 | ~optional(); // constexpr in C++20 |
| 131 | |
| 132 | // [optional.assign], assignment |
| 133 | optional &operator=(nullopt_t) noexcept; // constexpr in C++20 |
| 134 | constexpr optional &operator=(const optional &); |
| 135 | constexpr optional &operator=(optional &&) noexcept(see below); |
| 136 | template<class U = remove_cv_t<T>> optional &operator=(U &&); // constexpr in C++20 |
| 137 | template<class U> optional &operator=(const optional<U> &); // constexpr in C++20 |
| 138 | template<class U> optional &operator=(optional<U> &&); // constexpr in C++20 |
| 139 | template<class... Args> T& emplace(Args &&...); // constexpr in C++20 |
| 140 | template<class U, class... Args> T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20 |
| 141 | |
| 142 | // [optional.swap], swap |
| 143 | void swap(optional &) noexcept(see below ); // constexpr in C++20 |
| 144 | |
| 145 | // [optional.iterators], iterator support |
| 146 | constexpr iterator begin() noexcept; |
| 147 | constexpr const_iterator begin() const noexcept; |
| 148 | constexpr iterator end() noexcept; |
| 149 | constexpr const_iterator end() const noexcept; |
| 150 | |
| 151 | // [optional.observe], observers |
| 152 | constexpr T const *operator->() const noexcept; |
| 153 | constexpr T *operator->() noexcept; |
| 154 | constexpr T const &operator*() const & noexcept; |
| 155 | constexpr T &operator*() & noexcept; |
| 156 | constexpr T &&operator*() && noexcept; |
| 157 | constexpr const T &&operator*() const && noexcept; |
| 158 | constexpr explicit operator bool() const noexcept; |
| 159 | constexpr bool has_value() const noexcept; |
| 160 | constexpr T const &value() const &; |
| 161 | constexpr T &value() &; |
| 162 | constexpr T &&value() &&; |
| 163 | constexpr const T &&value() const &&; |
| 164 | template<class U = remove_cv_t<T>> constexpr T value_or(U &&) const &; |
| 165 | template<class U = remove_cv_t<T>> constexpr T value_or(U &&) &&; |
| 166 | |
| 167 | // [optional.monadic], monadic operations |
| 168 | template<class F> constexpr auto and_then(F&& f) &; // since C++23 |
| 169 | template<class F> constexpr auto and_then(F&& f) &&; // since C++23 |
| 170 | template<class F> constexpr auto and_then(F&& f) const&; // since C++23 |
| 171 | template<class F> constexpr auto and_then(F&& f) const&&; // since C++23 |
| 172 | template<class F> constexpr auto transform(F&& f) &; // since C++23 |
| 173 | template<class F> constexpr auto transform(F&& f) &&; // since C++23 |
| 174 | template<class F> constexpr auto transform(F&& f) const&; // since C++23 |
| 175 | template<class F> constexpr auto transform(F&& f) const&&; // since C++23 |
| 176 | template<class F> constexpr optional or_else(F&& f) &&; // since C++23 |
| 177 | template<class F> constexpr optional or_else(F&& f) const&; // since C++23 |
| 178 | |
| 179 | // [optional.mod], modifiers |
| 180 | void reset() noexcept; // constexpr in C++20 |
| 181 | |
| 182 | private: |
| 183 | T *val; // exposition only |
| 184 | }; |
| 185 | |
| 186 | template<class T> |
| 187 | optional(T) -> optional<T>; |
| 188 | |
| 189 | template<class T> |
| 190 | class optional<T&> { // since C++26 |
| 191 | public: |
| 192 | using value_type = T; |
| 193 | using iterator = implementation-defined; // see [optional.ref.iterators] |
| 194 | |
| 195 | public: |
| 196 | // [optional.ref.ctor], constructors |
| 197 | constexpr optional() noexcept = default; |
| 198 | constexpr optional(nullopt_t) noexcept : optional() {} |
| 199 | constexpr optional(const optional& rhs) noexcept = default; |
| 200 | |
| 201 | template<class Arg> |
| 202 | constexpr explicit optional(in_place_t, Arg&& arg); |
| 203 | template<class U> |
| 204 | constexpr explicit(see below) optional(U&& u) noexcept(see below); |
| 205 | template<class U> |
| 206 | constexpr explicit(see below) optional(optional<U>& rhs) noexcept(see below); |
| 207 | template<class U> |
| 208 | constexpr explicit(see below) optional(const optional<U>& rhs) noexcept(see below); |
| 209 | template<class U> |
| 210 | constexpr explicit(see below) optional(optional<U>&& rhs) noexcept(see below); |
| 211 | template<class U> |
| 212 | constexpr explicit(see below) optional(const optional<U>&& rhs) noexcept(see below); |
| 213 | |
| 214 | constexpr ~optional() = default; |
| 215 | |
| 216 | // [optional.ref.assign], assignment |
| 217 | constexpr optional& operator=(nullopt_t) noexcept; |
| 218 | constexpr optional& operator=(const optional& rhs) noexcept = default; |
| 219 | |
| 220 | template<class U> constexpr T& emplace(U&& u) noexcept(see below); |
| 221 | |
| 222 | // [optional.ref.swap], swap |
| 223 | constexpr void swap(optional& rhs) noexcept; |
| 224 | |
| 225 | // [optional.ref.iterators], iterator support |
| 226 | constexpr iterator begin() const noexcept; |
| 227 | constexpr iterator end() const noexcept; |
| 228 | |
| 229 | // [optional.ref.observe], observers |
| 230 | constexpr T* operator->() const noexcept; |
| 231 | constexpr T& operator*() const noexcept; |
| 232 | constexpr explicit operator bool() const noexcept; |
| 233 | constexpr bool has_value() const noexcept; |
| 234 | constexpr T& value() const; // freestanding-deleted |
| 235 | template<class U = remove_cv_t<T>> |
| 236 | constexpr remove_cv_t<T> value_or(U&& u) const; |
| 237 | |
| 238 | // [optional.ref.monadic], monadic operations |
| 239 | template<class F> constexpr auto and_then(F&& f) const; |
| 240 | template<class F> constexpr optional<invoke_result_t<F, T&>> transform(F&& f) const; |
| 241 | template<class F> constexpr optional or_else(F&& f) const; |
| 242 | |
| 243 | // [optional.ref.mod], modifiers |
| 244 | constexpr void reset() noexcept; |
| 245 | |
| 246 | private: |
| 247 | T* val = nullptr; // exposition only |
| 248 | |
| 249 | // [optional.ref.expos], exposition only helper functions |
| 250 | template<class U> |
| 251 | constexpr void convert-ref-init-val(U&& u); // exposition only |
| 252 | }; |
| 253 | |
| 254 | } // namespace std |
| 255 | |
| 256 | */ |
| 257 | |
| 258 | #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) |
| 259 | # include <__cxx03/__config> |
| 260 | #else |
| 261 | # include <__assert> |
| 262 | # include <__compare/compare_three_way_result.h> |
| 263 | # include <__compare/ordering.h> |
| 264 | # include <__compare/three_way_comparable.h> |
| 265 | # include <__concepts/invocable.h> |
| 266 | # include <__config> |
| 267 | # include <__exception/exception.h> |
| 268 | # include <__functional/hash.h> |
| 269 | # include <__functional/invoke.h> |
| 270 | # include <__functional/unary_function.h> |
| 271 | # include <__fwd/format.h> |
| 272 | # include <__fwd/functional.h> |
| 273 | # include <__iterator/bounded_iter.h> |
| 274 | # include <__iterator/capacity_aware_iterator.h> |
| 275 | # include <__memory/addressof.h> |
| 276 | # include <__memory/construct_at.h> |
| 277 | # include <__ranges/enable_borrowed_range.h> |
| 278 | # include <__ranges/enable_view.h> |
| 279 | # include <__tuple/sfinae_helpers.h> |
| 280 | # include <__type_traits/add_pointer.h> |
| 281 | # include <__type_traits/conditional.h> |
| 282 | # include <__type_traits/conjunction.h> |
| 283 | # include <__type_traits/decay.h> |
| 284 | # include <__type_traits/disjunction.h> |
| 285 | # include <__type_traits/enable_if.h> |
| 286 | # include <__type_traits/integral_constant.h> |
| 287 | # include <__type_traits/invoke.h> |
| 288 | # include <__type_traits/is_array.h> |
| 289 | # include <__type_traits/is_assignable.h> |
| 290 | # include <__type_traits/is_constructible.h> |
| 291 | # include <__type_traits/is_convertible.h> |
| 292 | # include <__type_traits/is_core_convertible.h> |
| 293 | # include <__type_traits/is_destructible.h> |
| 294 | # include <__type_traits/is_function.h> |
| 295 | # include <__type_traits/is_nothrow_assignable.h> |
| 296 | # include <__type_traits/is_nothrow_constructible.h> |
| 297 | # include <__type_traits/is_object.h> |
| 298 | # include <__type_traits/is_reference.h> |
| 299 | # include <__type_traits/is_same.h> |
| 300 | # include <__type_traits/is_scalar.h> |
| 301 | # include <__type_traits/is_swappable.h> |
| 302 | # include <__type_traits/is_trivially_assignable.h> |
| 303 | # include <__type_traits/is_trivially_constructible.h> |
| 304 | # include <__type_traits/is_trivially_destructible.h> |
| 305 | # include <__type_traits/is_trivially_relocatable.h> |
| 306 | # include <__type_traits/negation.h> |
| 307 | # include <__type_traits/reference_constructs_from_temporary.h> |
| 308 | # include <__type_traits/remove_const.h> |
| 309 | # include <__type_traits/remove_cv.h> |
| 310 | # include <__type_traits/remove_cvref.h> |
| 311 | # include <__type_traits/remove_reference.h> |
| 312 | # include <__utility/declval.h> |
| 313 | # include <__utility/forward.h> |
| 314 | # include <__utility/in_place.h> |
| 315 | # include <__utility/move.h> |
| 316 | # include <__utility/swap.h> |
| 317 | # include <__verbose_abort> |
| 318 | # include <initializer_list> |
| 319 | # include <version> |
| 320 | |
| 321 | // standard-mandated includes |
| 322 | |
| 323 | // [optional.syn] |
| 324 | # include <compare> |
| 325 | |
| 326 | # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 327 | # pragma GCC system_header |
| 328 | # endif |
| 329 | |
| 330 | _LIBCPP_PUSH_MACROS |
| 331 | # include <__undef_macros> |
| 332 | |
| 333 | namespace std // purposefully not using versioning namespace |
| 334 | { |
| 335 | |
| 336 | class _LIBCPP_EXPORTED_FROM_ABI bad_optional_access : public exception { |
| 337 | public: |
| 338 | _LIBCPP_HIDE_FROM_ABI bad_optional_access() _NOEXCEPT = default; |
| 339 | _LIBCPP_HIDE_FROM_ABI bad_optional_access(const bad_optional_access&) _NOEXCEPT = default; |
| 340 | _LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default; |
| 341 | // Get the key function ~bad_optional_access() into the dylib |
| 342 | ~bad_optional_access() _NOEXCEPT override; |
| 343 | [[__nodiscard__]] const char* what() const _NOEXCEPT override; |
| 344 | }; |
| 345 | |
| 346 | } // namespace std |
| 347 | |
| 348 | # if _LIBCPP_STD_VER >= 17 |
| 349 | |
| 350 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 351 | |
| 352 | [[noreturn]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_optional_access() { |
| 353 | # if _LIBCPP_HAS_EXCEPTIONS |
| 354 | throw bad_optional_access(); |
| 355 | # else |
| 356 | _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode" ); |
| 357 | # endif |
| 358 | } |
| 359 | |
| 360 | struct nullopt_t { |
| 361 | struct __secret_tag { |
| 362 | explicit __secret_tag() = default; |
| 363 | }; |
| 364 | _LIBCPP_HIDE_FROM_ABI constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {} |
| 365 | }; |
| 366 | |
| 367 | inline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}}; |
| 368 | |
| 369 | struct __optional_construct_from_invoke_tag {}; |
| 370 | |
| 371 | template <class _Tp, bool = is_trivially_destructible<_Tp>::value> |
| 372 | struct __optional_destruct_base; |
| 373 | |
| 374 | template <class _Tp> |
| 375 | struct __optional_destruct_base<_Tp, false> { |
| 376 | typedef _Tp value_type; |
| 377 | static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior" ); |
| 378 | union { |
| 379 | char __null_state_; |
| 380 | remove_cv_t<value_type> __val_; |
| 381 | }; |
| 382 | bool __engaged_; |
| 383 | |
| 384 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__optional_destruct_base() { |
| 385 | if (__engaged_) |
| 386 | __val_.~value_type(); |
| 387 | } |
| 388 | |
| 389 | _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {} |
| 390 | |
| 391 | template <class... _Args> |
| 392 | _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args) |
| 393 | : __val_(std::forward<_Args>(__args)...), __engaged_(true) {} |
| 394 | |
| 395 | # if _LIBCPP_STD_VER >= 23 |
| 396 | template <class _Fp, class... _Args> |
| 397 | _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base( |
| 398 | __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) |
| 399 | : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {} |
| 400 | # endif |
| 401 | |
| 402 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { |
| 403 | if (__engaged_) { |
| 404 | __val_.~value_type(); |
| 405 | __engaged_ = false; |
| 406 | } |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | template <class _Tp> |
| 411 | struct __optional_destruct_base<_Tp, true> { |
| 412 | typedef _Tp value_type; |
| 413 | static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior" ); |
| 414 | union { |
| 415 | char __null_state_; |
| 416 | remove_cv_t<value_type> __val_; |
| 417 | }; |
| 418 | bool __engaged_; |
| 419 | |
| 420 | _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {} |
| 421 | |
| 422 | template <class... _Args> |
| 423 | _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args) |
| 424 | : __val_(std::forward<_Args>(__args)...), __engaged_(true) {} |
| 425 | |
| 426 | # if _LIBCPP_STD_VER >= 23 |
| 427 | template <class _Fp, class... _Args> |
| 428 | _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base( |
| 429 | __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args) |
| 430 | : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {} |
| 431 | # endif |
| 432 | |
| 433 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { |
| 434 | if (__engaged_) { |
| 435 | __engaged_ = false; |
| 436 | } |
| 437 | } |
| 438 | }; |
| 439 | |
| 440 | template <class _Tp, bool = is_reference<_Tp>::value> |
| 441 | struct __optional_storage_base : __optional_destruct_base<_Tp> { |
| 442 | using __base _LIBCPP_NODEBUG = __optional_destruct_base<_Tp>; |
| 443 | using value_type = _Tp; |
| 444 | using __base::__base; |
| 445 | |
| 446 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; } |
| 447 | |
| 448 | _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; } |
| 449 | _LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; } |
| 450 | _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() && noexcept { return std::move(this->__val_); } |
| 451 | _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& __get() const&& noexcept { return std::move(this->__val_); } |
| 452 | |
| 453 | template <class... _Args> |
| 454 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_Args&&... __args) { |
| 455 | _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage" ); |
| 456 | std::__construct_at(std::addressof(this->__val_), std::forward<_Args>(__args)...); |
| 457 | this->__engaged_ = true; |
| 458 | } |
| 459 | |
| 460 | template <class _That> |
| 461 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) { |
| 462 | if (__opt.has_value()) |
| 463 | __construct(std::forward<_That>(__opt).__get()); |
| 464 | } |
| 465 | |
| 466 | template <class _That> |
| 467 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) { |
| 468 | if (this->__engaged_ == __opt.has_value()) { |
| 469 | if (this->__engaged_) |
| 470 | static_cast<_Tp&>(this->__val_) = std::forward<_That>(__opt).__get(); |
| 471 | } else { |
| 472 | if (this->__engaged_) |
| 473 | this->reset(); |
| 474 | else |
| 475 | __construct(std::forward<_That>(__opt).__get()); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 480 | __swap(__optional_storage_base& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>) { |
| 481 | using std::swap; |
| 482 | if (this->has_value() == __rhs.has_value()) { |
| 483 | if (this->has_value()) |
| 484 | swap(this->__get(), __rhs.__get()); |
| 485 | } else { |
| 486 | if (this->has_value()) { |
| 487 | __rhs.__construct(std::move(this->__get())); |
| 488 | this->reset(); |
| 489 | } else { |
| 490 | this->__construct(std::move(__rhs.__get())); |
| 491 | __rhs.reset(); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // [optional.observe] |
| 497 | _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp const> operator->() const noexcept { |
| 498 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value" ); |
| 499 | return std::addressof(this->__get()); |
| 500 | } |
| 501 | |
| 502 | _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() noexcept { |
| 503 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value" ); |
| 504 | return std::addressof(this->__get()); |
| 505 | } |
| 506 | |
| 507 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept { |
| 508 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value" ); |
| 509 | return this->__get(); |
| 510 | } |
| 511 | |
| 512 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept { |
| 513 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value" ); |
| 514 | return this->__get(); |
| 515 | } |
| 516 | |
| 517 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept { |
| 518 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value" ); |
| 519 | return std::move(this->__get()); |
| 520 | } |
| 521 | |
| 522 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept { |
| 523 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value" ); |
| 524 | return std::move(this->__get()); |
| 525 | } |
| 526 | |
| 527 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& { |
| 528 | if (!this->has_value()) |
| 529 | std::__throw_bad_optional_access(); |
| 530 | return this->__get(); |
| 531 | } |
| 532 | |
| 533 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & { |
| 534 | if (!this->has_value()) |
| 535 | std::__throw_bad_optional_access(); |
| 536 | return this->__get(); |
| 537 | } |
| 538 | |
| 539 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && { |
| 540 | if (!this->has_value()) |
| 541 | std::__throw_bad_optional_access(); |
| 542 | return std::move(this->__get()); |
| 543 | } |
| 544 | |
| 545 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& { |
| 546 | if (!this->has_value()) |
| 547 | std::__throw_bad_optional_access(); |
| 548 | return std::move(this->__get()); |
| 549 | } |
| 550 | }; |
| 551 | |
| 552 | template <class _Tp> |
| 553 | struct __optional_storage_base<_Tp, true> { |
| 554 | using value_type = _Tp; |
| 555 | using __raw_type _LIBCPP_NODEBUG = remove_reference_t<_Tp>; |
| 556 | __raw_type* __value_; |
| 557 | |
| 558 | _LIBCPP_HIDE_FROM_ABI constexpr __optional_storage_base() noexcept : __value_(nullptr) {} |
| 559 | |
| 560 | template <class _Up> |
| 561 | _LIBCPP_HIDE_FROM_ABI constexpr void __convert_init_ref_val(_Up&& __val) noexcept { |
| 562 | _Tp& __r(std::forward<_Up>(__val)); |
| 563 | __value_ = std::addressof(__r); |
| 564 | } |
| 565 | |
| 566 | template <class _UArg> |
| 567 | _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg) { |
| 568 | static_assert(!__reference_constructs_from_temporary_v<_Tp, _UArg>, |
| 569 | "Attempted to construct a reference element in optional from a " |
| 570 | "possible temporary" ); |
| 571 | __convert_init_ref_val(std::forward<_UArg>(__uarg)); |
| 572 | } |
| 573 | |
| 574 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; } |
| 575 | |
| 576 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; } |
| 577 | |
| 578 | _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const noexcept { return *__value_; } |
| 579 | |
| 580 | template <class _UArg> |
| 581 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_UArg&& __val) { |
| 582 | _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage" ); |
| 583 | static_assert(!__reference_constructs_from_temporary_v<_Tp, _UArg>, |
| 584 | "Attempted to construct a reference element in tuple from a " |
| 585 | "possible temporary" ); |
| 586 | __convert_init_ref_val(std::forward<_UArg>(__val)); |
| 587 | } |
| 588 | |
| 589 | template <class _That> |
| 590 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) { |
| 591 | if (__opt.has_value()) |
| 592 | __construct(std::forward<_That>(__opt).__get()); |
| 593 | } |
| 594 | |
| 595 | template <class _That> |
| 596 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) { |
| 597 | if (has_value() == __opt.has_value()) { |
| 598 | if (has_value()) |
| 599 | *__value_ = std::forward<_That>(__opt).__get(); |
| 600 | } else { |
| 601 | if (has_value()) |
| 602 | reset(); |
| 603 | else |
| 604 | __construct(std::forward<_That>(__opt).__get()); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__optional_storage_base& __rhs) noexcept { |
| 609 | std::swap(__value_, __rhs.__value_); |
| 610 | } |
| 611 | |
| 612 | // [optional.ref.observe] |
| 613 | _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() const noexcept { |
| 614 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value" ); |
| 615 | return std::addressof(this->__get()); |
| 616 | } |
| 617 | |
| 618 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() const noexcept { |
| 619 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value" ); |
| 620 | return this->__get(); |
| 621 | } |
| 622 | |
| 623 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() const { |
| 624 | if (!this->has_value()) |
| 625 | std::__throw_bad_optional_access(); |
| 626 | return this->__get(); |
| 627 | } |
| 628 | }; |
| 629 | |
| 630 | template <class _Tp, bool = is_trivially_copy_constructible_v<_Tp> || is_lvalue_reference_v<_Tp>> |
| 631 | struct __optional_copy_base : __optional_storage_base<_Tp> { |
| 632 | using __optional_storage_base<_Tp>::__optional_storage_base; |
| 633 | }; |
| 634 | |
| 635 | template <class _Tp> |
| 636 | struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> { |
| 637 | using __optional_storage_base<_Tp>::__optional_storage_base; |
| 638 | |
| 639 | _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default; |
| 640 | |
| 641 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) { |
| 642 | this->__construct_from(__opt); |
| 643 | } |
| 644 | |
| 645 | _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&) = default; |
| 646 | _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default; |
| 647 | _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&) = default; |
| 648 | }; |
| 649 | |
| 650 | template <class _Tp, bool = is_trivially_move_constructible_v<_Tp> || is_lvalue_reference_v<_Tp>> |
| 651 | struct __optional_move_base : __optional_copy_base<_Tp> { |
| 652 | using __optional_copy_base<_Tp>::__optional_copy_base; |
| 653 | }; |
| 654 | |
| 655 | template <class _Tp> |
| 656 | struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> { |
| 657 | using value_type = _Tp; |
| 658 | using __optional_copy_base<_Tp>::__optional_copy_base; |
| 659 | |
| 660 | _LIBCPP_HIDE_FROM_ABI __optional_move_base() = default; |
| 661 | _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default; |
| 662 | |
| 663 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 |
| 664 | __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) { |
| 665 | this->__construct_from(std::move(__opt)); |
| 666 | } |
| 667 | |
| 668 | _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default; |
| 669 | _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&) = default; |
| 670 | }; |
| 671 | |
| 672 | template <class _Tp, |
| 673 | bool = (is_trivially_destructible_v<_Tp> && is_trivially_copy_constructible_v<_Tp> && |
| 674 | is_trivially_copy_assignable_v<_Tp>) || |
| 675 | is_lvalue_reference_v<_Tp>> |
| 676 | struct __optional_copy_assign_base : __optional_move_base<_Tp> { |
| 677 | using __optional_move_base<_Tp>::__optional_move_base; |
| 678 | }; |
| 679 | |
| 680 | template <class _Tp> |
| 681 | struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> { |
| 682 | using __optional_move_base<_Tp>::__optional_move_base; |
| 683 | |
| 684 | _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base() = default; |
| 685 | _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default; |
| 686 | _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&) = default; |
| 687 | |
| 688 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base& |
| 689 | operator=(const __optional_copy_assign_base& __opt) { |
| 690 | this->__assign_from(__opt); |
| 691 | return *this; |
| 692 | } |
| 693 | |
| 694 | _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default; |
| 695 | }; |
| 696 | |
| 697 | template <class _Tp, |
| 698 | bool = (is_trivially_destructible_v<_Tp> && is_trivially_move_constructible_v<_Tp> && |
| 699 | is_trivially_move_assignable_v<_Tp>) || |
| 700 | is_lvalue_reference_v<_Tp>> |
| 701 | struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> { |
| 702 | using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; |
| 703 | }; |
| 704 | |
| 705 | template <class _Tp> |
| 706 | struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> { |
| 707 | using value_type = _Tp; |
| 708 | using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; |
| 709 | |
| 710 | _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base() = default; |
| 711 | _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt) = default; |
| 712 | _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&) = default; |
| 713 | _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default; |
| 714 | |
| 715 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base& |
| 716 | operator=(__optional_move_assign_base&& __opt) noexcept( |
| 717 | is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) { |
| 718 | this->__assign_from(std::move(__opt)); |
| 719 | return *this; |
| 720 | } |
| 721 | }; |
| 722 | |
| 723 | template <class _Tp> |
| 724 | using __optional_sfinae_ctor_base_t _LIBCPP_NODEBUG = |
| 725 | __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >; |
| 726 | |
| 727 | template <class _Tp> |
| 728 | using __optional_sfinae_assign_base_t _LIBCPP_NODEBUG = |
| 729 | __sfinae_assign_base< (is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Tp>) || is_lvalue_reference_v<_Tp>, |
| 730 | (is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp>) || is_lvalue_reference_v<_Tp>>; |
| 731 | |
| 732 | template <class _Tp> |
| 733 | class optional; |
| 734 | |
| 735 | # if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR |
| 736 | template <class _Tp> |
| 737 | constexpr bool ranges::enable_view<optional<_Tp>> = true; |
| 738 | |
| 739 | template <class _Tp> |
| 740 | constexpr range_format format_kind<optional<_Tp>> = range_format::disabled; |
| 741 | |
| 742 | template <class _Tp> |
| 743 | constexpr bool ranges::enable_borrowed_range<optional<_Tp&>> = true; |
| 744 | |
| 745 | # endif |
| 746 | |
| 747 | # if _LIBCPP_STD_VER >= 20 |
| 748 | |
| 749 | template <class _Tp> |
| 750 | concept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(const optional<_Up>&) {}(__t); }; |
| 751 | |
| 752 | # endif // _LIBCPP_STD_VER >= 20 |
| 753 | |
| 754 | template <class _Tp> |
| 755 | struct __is_std_optional : false_type {}; |
| 756 | template <class _Tp> |
| 757 | struct __is_std_optional<optional<_Tp>> : true_type {}; |
| 758 | |
| 759 | template <class _Tp, class... _Args> |
| 760 | inline constexpr bool __is_constructible_for_optional_v = is_constructible_v<_Tp, _Args...>; |
| 761 | |
| 762 | template <class _Tp, class... _Args> |
| 763 | struct __is_constructible_for_optional : bool_constant<__is_constructible_for_optional_v<_Tp, _Args...>> {}; |
| 764 | |
| 765 | template <class _Tp, class _Up, class... _Args> |
| 766 | inline constexpr bool __is_constructible_for_optional_initializer_list_v = |
| 767 | is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>; |
| 768 | |
| 769 | # if _LIBCPP_STD_VER >= 26 |
| 770 | template <class _Tp, class... _Args> |
| 771 | inline constexpr bool __is_constructible_for_optional_v<_Tp&, _Args...> = false; |
| 772 | template <class _Tp, class _Arg> |
| 773 | inline constexpr bool __is_constructible_for_optional_v<_Tp&, _Arg> = |
| 774 | is_constructible_v<_Tp&, _Arg> && !reference_constructs_from_temporary_v<_Tp&, _Arg>; |
| 775 | |
| 776 | template <class _Tp, class _Up, class... _Args> |
| 777 | inline constexpr bool __is_constructible_for_optional_initializer_list_v<_Tp&, _Up, _Args...> = false; |
| 778 | # endif |
| 779 | |
| 780 | template <class _Tp, class = void> |
| 781 | struct __optional_iterator_base : __optional_move_assign_base<_Tp> { |
| 782 | using __optional_move_assign_base<_Tp>::__optional_move_assign_base; |
| 783 | }; |
| 784 | |
| 785 | # if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR |
| 786 | |
| 787 | template <class _Tp> |
| 788 | struct __optional_iterator_base<_Tp, enable_if_t<is_object_v<_Tp>>> : __optional_move_assign_base<_Tp> { |
| 789 | private: |
| 790 | using __pointer _LIBCPP_NODEBUG = add_pointer_t<_Tp>; |
| 791 | using __const_pointer _LIBCPP_NODEBUG = add_pointer_t<const _Tp>; |
| 792 | |
| 793 | public: |
| 794 | using __optional_move_assign_base<_Tp>::__optional_move_assign_base; |
| 795 | |
| 796 | # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL |
| 797 | using iterator = __bounded_iter<__pointer>; |
| 798 | using const_iterator = __bounded_iter<__const_pointer>; |
| 799 | # else |
| 800 | using iterator = __capacity_aware_iterator<__pointer, optional<_Tp>, 1>; |
| 801 | using const_iterator = __capacity_aware_iterator<__const_pointer, optional<_Tp>, 1>; |
| 802 | # endif |
| 803 | |
| 804 | // [optional.iterators], iterator support |
| 805 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept { |
| 806 | auto* __ptr = std::addressof(this->__get()); |
| 807 | |
| 808 | # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL |
| 809 | return std::__make_bounded_iter(__ptr, __ptr, __ptr + (this->has_value() ? 1 : 0)); |
| 810 | # else |
| 811 | return std::__make_capacity_aware_iterator<__pointer, optional<_Tp>, 1>(__ptr); |
| 812 | # endif |
| 813 | } |
| 814 | |
| 815 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept { |
| 816 | auto* __ptr = std::addressof(this->__get()); |
| 817 | |
| 818 | # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL |
| 819 | return std::__make_bounded_iter(__ptr, __ptr, __ptr + (this->has_value() ? 1 : 0)); |
| 820 | # else |
| 821 | return std::__make_capacity_aware_iterator<__const_pointer, optional<_Tp>, 1>(__ptr); |
| 822 | # endif |
| 823 | } |
| 824 | |
| 825 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept { |
| 826 | return begin() + (this->has_value() ? 1 : 0); |
| 827 | } |
| 828 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept { |
| 829 | return begin() + (this->has_value() ? 1 : 0); |
| 830 | } |
| 831 | }; |
| 832 | |
| 833 | template <class _Tp> |
| 834 | struct __optional_iterator_base<_Tp&, enable_if_t<is_object_v<_Tp> && !__is_unbounded_array_v<_Tp> >> |
| 835 | : __optional_move_assign_base<_Tp&> { |
| 836 | private: |
| 837 | using __pointer _LIBCPP_NODEBUG = add_pointer_t<_Tp>; |
| 838 | |
| 839 | public: |
| 840 | using __optional_move_assign_base<_Tp&>::__optional_move_assign_base; |
| 841 | |
| 842 | # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL |
| 843 | using iterator = __bounded_iter<__pointer>; |
| 844 | # else |
| 845 | using iterator = __capacity_aware_iterator<__pointer, optional<_Tp&>, 1>; |
| 846 | # endif |
| 847 | |
| 848 | // [optional.ref.iterators], iterator support |
| 849 | |
| 850 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const noexcept { |
| 851 | auto* __ptr = this->has_value() ? std::addressof(this->__get()) : nullptr; |
| 852 | |
| 853 | # ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL |
| 854 | return std::__make_bounded_iter(__ptr, __ptr, __ptr + (this->has_value() ? 1 : 0)); |
| 855 | # else |
| 856 | return std::__make_capacity_aware_iterator<__pointer, optional<_Tp&>, 1>(__ptr); |
| 857 | # endif |
| 858 | } |
| 859 | |
| 860 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const noexcept { |
| 861 | return begin() + (this->has_value() ? 1 : 0); |
| 862 | } |
| 863 | }; |
| 864 | |
| 865 | # endif // _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR |
| 866 | |
| 867 | template <class _Tp> |
| 868 | class _LIBCPP_DECLSPEC_EMPTY_BASES optional |
| 869 | : public __optional_iterator_base<_Tp>, |
| 870 | private __optional_sfinae_ctor_base_t<_Tp>, |
| 871 | private __optional_sfinae_assign_base_t<_Tp> { |
| 872 | using __base _LIBCPP_NODEBUG = __optional_iterator_base<_Tp>; |
| 873 | |
| 874 | public: |
| 875 | using value_type = __libcpp_remove_reference_t<_Tp>; |
| 876 | |
| 877 | using __trivially_relocatable _LIBCPP_NODEBUG = |
| 878 | conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, optional, void>; |
| 879 | |
| 880 | private: |
| 881 | static_assert(!is_same_v<__remove_cvref_t<_Tp>, in_place_t>, |
| 882 | "instantiation of optional with in_place_t is ill-formed" ); |
| 883 | static_assert(!is_same_v<__remove_cvref_t<_Tp>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed" ); |
| 884 | # if _LIBCPP_STD_VER >= 26 |
| 885 | static_assert(!is_rvalue_reference_v<_Tp>, "instantiation of optional with an rvalue reference type is ill-formed" ); |
| 886 | # else |
| 887 | static_assert(!is_reference_v<_Tp>, "instantiation of optional with a reference type is ill-formed" ); |
| 888 | # endif |
| 889 | static_assert(is_destructible_v<_Tp>, "instantiation of optional with a non-destructible type is ill-formed" ); |
| 890 | static_assert(!is_array_v<_Tp>, "instantiation of optional with an array type is ill-formed" ); |
| 891 | |
| 892 | # if _LIBCPP_STD_VER >= 26 |
| 893 | template <class _Up> |
| 894 | constexpr static bool __libcpp_opt_ref_ctor_deleted = |
| 895 | is_lvalue_reference_v<_Tp> && reference_constructs_from_temporary_v<_Tp, _Up>; |
| 896 | # endif |
| 897 | |
| 898 | // LWG2756: conditionally explicit conversion from _Up |
| 899 | struct _CheckOptionalArgsConstructor { |
| 900 | template <class _Up> |
| 901 | _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() { |
| 902 | return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>; |
| 903 | } |
| 904 | |
| 905 | template <class _Up> |
| 906 | _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() { |
| 907 | return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>; |
| 908 | } |
| 909 | }; |
| 910 | template <class _Up> |
| 911 | using _CheckOptionalArgsCtor _LIBCPP_NODEBUG = |
| 912 | _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value && |
| 913 | (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value), |
| 914 | _CheckOptionalArgsConstructor, |
| 915 | __check_tuple_constructor_fail >; |
| 916 | template <class _QualUp> |
| 917 | struct _CheckOptionalLikeConstructor { |
| 918 | template <class _Up, class _Opt = optional<_Up>> |
| 919 | using __check_constructible_from_opt _LIBCPP_NODEBUG = |
| 920 | _Or< is_constructible<_Tp, _Opt&>, |
| 921 | is_constructible<_Tp, _Opt const&>, |
| 922 | is_constructible<_Tp, _Opt&&>, |
| 923 | is_constructible<_Tp, _Opt const&&>, |
| 924 | is_convertible<_Opt&, _Tp>, |
| 925 | is_convertible<_Opt const&, _Tp>, |
| 926 | is_convertible<_Opt&&, _Tp>, |
| 927 | is_convertible<_Opt const&&, _Tp> >; |
| 928 | template <class _Up, class _Opt = optional<_Up>> |
| 929 | using __check_assignable_from_opt _LIBCPP_NODEBUG = |
| 930 | _Or< is_assignable<_Tp&, _Opt&>, |
| 931 | is_assignable<_Tp&, _Opt const&>, |
| 932 | is_assignable<_Tp&, _Opt&&>, |
| 933 | is_assignable<_Tp&, _Opt const&&> >; |
| 934 | template <class _Up, class _QUp = _QualUp> |
| 935 | _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() { |
| 936 | return is_convertible<_QUp, _Tp>::value && |
| 937 | (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value); |
| 938 | } |
| 939 | template <class _Up, class _QUp = _QualUp> |
| 940 | _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() { |
| 941 | return !is_convertible<_QUp, _Tp>::value && |
| 942 | (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value); |
| 943 | } |
| 944 | template <class _Up, class _QUp = _QualUp> |
| 945 | _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() { |
| 946 | // Construction and assignability of _QUp to _Tp has already been |
| 947 | // checked. |
| 948 | return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value; |
| 949 | } |
| 950 | }; |
| 951 | |
| 952 | template <class _Up, class _QualUp> |
| 953 | using _CheckOptionalLikeCtor _LIBCPP_NODEBUG = |
| 954 | _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value, |
| 955 | _CheckOptionalLikeConstructor<_QualUp>, |
| 956 | __check_tuple_constructor_fail >; |
| 957 | template <class _Up, class _QualUp> |
| 958 | using _CheckOptionalLikeAssign _LIBCPP_NODEBUG = |
| 959 | _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value, |
| 960 | _CheckOptionalLikeConstructor<_QualUp>, |
| 961 | __check_tuple_constructor_fail >; |
| 962 | |
| 963 | public: |
| 964 | _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {} |
| 965 | _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default; |
| 966 | _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&) = default; |
| 967 | _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {} |
| 968 | |
| 969 | template < |
| 970 | class _InPlaceT, |
| 971 | class... _Args, |
| 972 | enable_if_t<_And<_IsSame<_InPlaceT, in_place_t>, __is_constructible_for_optional<_Tp, _Args...>>::value, int> = 0> |
| 973 | _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args) |
| 974 | : __base(in_place, std::forward<_Args>(__args)...) {} |
| 975 | |
| 976 | template <class _Up, |
| 977 | class... _Args, |
| 978 | enable_if_t<__is_constructible_for_optional_initializer_list_v<_Tp, _Up, _Args...>, int> = 0> |
| 979 | _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args) |
| 980 | : __base(in_place, __il, std::forward<_Args>(__args)...) {} |
| 981 | |
| 982 | template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0> |
| 983 | _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) |
| 984 | # if _LIBCPP_STD_VER >= 26 |
| 985 | noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up>) |
| 986 | # endif |
| 987 | : __base(in_place, std::forward<_Up>(__v)) { |
| 988 | } |
| 989 | |
| 990 | template <class _Up = remove_cv_t<_Tp>, |
| 991 | enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0> |
| 992 | _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v) |
| 993 | # if _LIBCPP_STD_VER >= 26 |
| 994 | noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up>) |
| 995 | # endif |
| 996 | : __base(in_place, std::forward<_Up>(__v)) { |
| 997 | } |
| 998 | |
| 999 | // LWG2756: conditionally explicit conversion from const optional<_Up>& |
| 1000 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0> |
| 1001 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v) |
| 1002 | # if _LIBCPP_STD_VER >= 26 |
| 1003 | noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up&>) |
| 1004 | # endif |
| 1005 | { |
| 1006 | this->__construct_from(__v); |
| 1007 | } |
| 1008 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0> |
| 1009 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v) |
| 1010 | # if _LIBCPP_STD_VER >= 26 |
| 1011 | noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up&>) |
| 1012 | # endif |
| 1013 | { |
| 1014 | this->__construct_from(__v); |
| 1015 | } |
| 1016 | |
| 1017 | // LWG2756: conditionally explicit conversion from optional<_Up>&& |
| 1018 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0> |
| 1019 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v) |
| 1020 | # if _LIBCPP_STD_VER >= 26 |
| 1021 | noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up>) |
| 1022 | # endif |
| 1023 | { |
| 1024 | this->__construct_from(std::move(__v)); |
| 1025 | } |
| 1026 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0> |
| 1027 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v) |
| 1028 | # if _LIBCPP_STD_VER >= 26 |
| 1029 | noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp&, _Up>) |
| 1030 | # endif |
| 1031 | { |
| 1032 | this->__construct_from(std::move(__v)); |
| 1033 | } |
| 1034 | |
| 1035 | // deleted optional<T&> constructors and additional optional<T&> constructors |
| 1036 | # if _LIBCPP_STD_VER >= 26 |
| 1037 | // optional(U&&) |
| 1038 | template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0> |
| 1039 | requires __libcpp_opt_ref_ctor_deleted<_Up> |
| 1040 | optional(_Up&&) = delete; |
| 1041 | |
| 1042 | template <class _Up = remove_cv_t<_Tp>, |
| 1043 | enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0> |
| 1044 | requires __libcpp_opt_ref_ctor_deleted<_Up> |
| 1045 | explicit optional(_Up&&) = delete; |
| 1046 | |
| 1047 | // optional(optional<U>& rhs) |
| 1048 | template <class _Up> |
| 1049 | requires(!__libcpp_opt_ref_ctor_deleted<_Up>) && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) && |
| 1050 | (!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up&> |
| 1051 | _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up&, _Tp&>) |
| 1052 | optional(optional<_Up>& __rhs) noexcept(is_nothrow_constructible_v<_Tp&, _Up&>) { |
| 1053 | this->__construct_from(__rhs); |
| 1054 | } |
| 1055 | |
| 1056 | template <class _Up> |
| 1057 | requires __libcpp_opt_ref_ctor_deleted<_Up> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) && |
| 1058 | (!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up&> |
| 1059 | constexpr explicit optional(optional<_Up>& __rhs) noexcept = delete; |
| 1060 | |
| 1061 | // optional(const optional<U>&) |
| 1062 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0> |
| 1063 | requires __libcpp_opt_ref_ctor_deleted<_Up> |
| 1064 | optional(const optional<_Up>&) = delete; |
| 1065 | |
| 1066 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0> |
| 1067 | requires __libcpp_opt_ref_ctor_deleted<_Up> |
| 1068 | explicit optional(const optional<_Up>&) = delete; |
| 1069 | |
| 1070 | // optional(optional<U>&&) |
| 1071 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0> |
| 1072 | requires __libcpp_opt_ref_ctor_deleted<_Up> |
| 1073 | optional(optional<_Up>&&) = delete; |
| 1074 | |
| 1075 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0> |
| 1076 | requires __libcpp_opt_ref_ctor_deleted<_Up> |
| 1077 | explicit optional(optional<_Up>&&) = delete; |
| 1078 | |
| 1079 | // optional(const optional<U>&&) |
| 1080 | template <class _Up> |
| 1081 | requires(!__libcpp_opt_ref_ctor_deleted<_Up>) && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) && |
| 1082 | (!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up> |
| 1083 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(!is_convertible_v<const _Up, _Tp&>) |
| 1084 | optional(const optional<_Up>&& __v) noexcept(is_nothrow_constructible_v<_Tp&, const _Up>) { |
| 1085 | this->__construct_from(std::move(__v)); |
| 1086 | } |
| 1087 | |
| 1088 | template <class _Up> |
| 1089 | requires __libcpp_opt_ref_ctor_deleted<_Up> && (!is_same_v<remove_cvref_t<_Tp>, optional<_Up>>) && |
| 1090 | (!is_same_v<_Tp&, _Up>) && is_constructible_v<_Tp&, _Up> |
| 1091 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>&& __v) noexcept = delete; |
| 1092 | # endif |
| 1093 | |
| 1094 | # if _LIBCPP_STD_VER >= 23 |
| 1095 | template <class _Tag, |
| 1096 | class _Fp, |
| 1097 | class... _Args, |
| 1098 | enable_if_t<_IsSame<_Tag, __optional_construct_from_invoke_tag>::value, int> = 0> |
| 1099 | _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Tag, _Fp&& __f, _Args&&... __args) |
| 1100 | : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {} |
| 1101 | # endif |
| 1102 | |
| 1103 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept { |
| 1104 | reset(); |
| 1105 | return *this; |
| 1106 | } |
| 1107 | |
| 1108 | _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default; |
| 1109 | _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default; |
| 1110 | |
| 1111 | // LWG2756 |
| 1112 | template <class _Up = remove_cv_t<_Tp>, |
| 1113 | enable_if_t<_And<_IsNotSame<__remove_cvref_t<_Up>, optional>, |
| 1114 | _Or<_IsNotSame<__remove_cvref_t<_Up>, _Tp>, _Not<is_scalar<_Tp>>>, |
| 1115 | is_constructible<_Tp, _Up>, |
| 1116 | is_assignable<_Tp&, _Up>, |
| 1117 | is_object<_Tp>>::value, |
| 1118 | int> = 0> |
| 1119 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) { |
| 1120 | if (this->has_value()) |
| 1121 | this->__get() = std::forward<_Up>(__v); |
| 1122 | else |
| 1123 | this->__construct(std::forward<_Up>(__v)); |
| 1124 | return *this; |
| 1125 | } |
| 1126 | |
| 1127 | // LWG2756 |
| 1128 | template <class _Up, enable_if_t<_CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0> |
| 1129 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) { |
| 1130 | this->__assign_from(__v); |
| 1131 | return *this; |
| 1132 | } |
| 1133 | |
| 1134 | // LWG2756 |
| 1135 | template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0> |
| 1136 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) { |
| 1137 | this->__assign_from(std::move(__v)); |
| 1138 | return *this; |
| 1139 | } |
| 1140 | |
| 1141 | template <class... _Args, enable_if_t<__is_constructible_for_optional_v<_Tp, _Args...>, int> = 0> |
| 1142 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) |
| 1143 | # if _LIBCPP_STD_VER >= 26 |
| 1144 | noexcept(is_lvalue_reference_v<_Tp> && is_nothrow_constructible_v<_Tp, _Args...>) |
| 1145 | # endif |
| 1146 | { |
| 1147 | reset(); |
| 1148 | this->__construct(std::forward<_Args>(__args)...); |
| 1149 | return this->__get(); |
| 1150 | } |
| 1151 | |
| 1152 | template <class _Up, |
| 1153 | class... _Args, |
| 1154 | enable_if_t<__is_constructible_for_optional_initializer_list_v<_Tp, _Up, _Args...>, int> = 0> |
| 1155 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { |
| 1156 | reset(); |
| 1157 | this->__construct(__il, std::forward<_Args>(__args)...); |
| 1158 | return this->__get(); |
| 1159 | } |
| 1160 | |
| 1161 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(optional& __opt) noexcept( |
| 1162 | (is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>) |
| 1163 | # if _LIBCPP_STD_VER >= 26 |
| 1164 | || is_lvalue_reference_v<_Tp> |
| 1165 | # endif |
| 1166 | ) { |
| 1167 | this->__swap(__opt); |
| 1168 | } |
| 1169 | |
| 1170 | using __base::operator*; |
| 1171 | using __base::operator->; |
| 1172 | |
| 1173 | _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); } |
| 1174 | |
| 1175 | using __base::__get; |
| 1176 | using __base::has_value; |
| 1177 | using __base::value; |
| 1178 | |
| 1179 | template <class _Up = remove_cv_t<_Tp>> |
| 1180 | # if _LIBCPP_STD_VER >= 26 |
| 1181 | requires(is_object_v<_Tp>) |
| 1182 | # endif |
| 1183 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& { |
| 1184 | static_assert(is_copy_constructible_v<_Tp>, "optional<T>::value_or: T must be copy constructible" ); |
| 1185 | static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T" ); |
| 1186 | return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v)); |
| 1187 | } |
| 1188 | |
| 1189 | template <class _Up = remove_cv_t<_Tp>> |
| 1190 | # if _LIBCPP_STD_VER >= 26 |
| 1191 | requires(is_object_v<_Tp>) |
| 1192 | # endif |
| 1193 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && { |
| 1194 | static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible" ); |
| 1195 | static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T" ); |
| 1196 | return this->has_value() ? std::move(this->__get()) : static_cast<_Tp>(std::forward<_Up>(__v)); |
| 1197 | } |
| 1198 | |
| 1199 | # if _LIBCPP_STD_VER >= 23 |
| 1200 | template <class _Func> |
| 1201 | # if _LIBCPP_STD_VER >= 26 |
| 1202 | requires(is_object_v<_Tp>) |
| 1203 | # endif |
| 1204 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & { |
| 1205 | using _Up = invoke_result_t<_Func, _Tp&>; |
| 1206 | static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, |
| 1207 | "Result of f(value()) must be a specialization of std::optional" ); |
| 1208 | if (*this) |
| 1209 | return std::invoke(std::forward<_Func>(__f), value()); |
| 1210 | return remove_cvref_t<_Up>(); |
| 1211 | } |
| 1212 | |
| 1213 | template <class _Func> |
| 1214 | # if _LIBCPP_STD_VER >= 26 |
| 1215 | requires(is_object_v<_Tp>) |
| 1216 | # endif |
| 1217 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& { |
| 1218 | using _Up = invoke_result_t<_Func, const _Tp&>; |
| 1219 | static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, |
| 1220 | "Result of f(value()) must be a specialization of std::optional" ); |
| 1221 | if (*this) |
| 1222 | return std::invoke(std::forward<_Func>(__f), value()); |
| 1223 | return remove_cvref_t<_Up>(); |
| 1224 | } |
| 1225 | |
| 1226 | template <class _Func> |
| 1227 | # if _LIBCPP_STD_VER >= 26 |
| 1228 | requires(is_object_v<_Tp>) |
| 1229 | # endif |
| 1230 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && { |
| 1231 | using _Up = invoke_result_t<_Func, _Tp&&>; |
| 1232 | static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, |
| 1233 | "Result of f(std::move(value())) must be a specialization of std::optional" ); |
| 1234 | if (*this) |
| 1235 | return std::invoke(std::forward<_Func>(__f), std::move(value())); |
| 1236 | return remove_cvref_t<_Up>(); |
| 1237 | } |
| 1238 | |
| 1239 | template <class _Func> |
| 1240 | # if _LIBCPP_STD_VER >= 26 |
| 1241 | requires(is_object_v<_Tp>) |
| 1242 | # endif |
| 1243 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& { |
| 1244 | using _Up = invoke_result_t<_Func, const _Tp&&>; |
| 1245 | static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, |
| 1246 | "Result of f(std::move(value())) must be a specialization of std::optional" ); |
| 1247 | if (*this) |
| 1248 | return std::invoke(std::forward<_Func>(__f), std::move(value())); |
| 1249 | return remove_cvref_t<_Up>(); |
| 1250 | } |
| 1251 | |
| 1252 | template <class _Func> |
| 1253 | # if _LIBCPP_STD_VER >= 26 |
| 1254 | requires(is_object_v<_Tp>) |
| 1255 | # endif |
| 1256 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & { |
| 1257 | using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>; |
| 1258 | static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array" ); |
| 1259 | static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t" ); |
| 1260 | static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t" ); |
| 1261 | static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type" ); |
| 1262 | if (*this) |
| 1263 | return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value()); |
| 1264 | return optional<_Up>(); |
| 1265 | } |
| 1266 | |
| 1267 | template <class _Func> |
| 1268 | # if _LIBCPP_STD_VER >= 26 |
| 1269 | requires(is_object_v<_Tp>) |
| 1270 | # endif |
| 1271 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& { |
| 1272 | using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>; |
| 1273 | static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array" ); |
| 1274 | static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t" ); |
| 1275 | static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t" ); |
| 1276 | static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type" ); |
| 1277 | if (*this) |
| 1278 | return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value()); |
| 1279 | return optional<_Up>(); |
| 1280 | } |
| 1281 | |
| 1282 | template <class _Func> |
| 1283 | # if _LIBCPP_STD_VER >= 26 |
| 1284 | requires(is_object_v<_Tp>) |
| 1285 | # endif |
| 1286 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && { |
| 1287 | using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>; |
| 1288 | static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array" ); |
| 1289 | static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t" ); |
| 1290 | static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t" ); |
| 1291 | static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type" ); |
| 1292 | if (*this) |
| 1293 | return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value())); |
| 1294 | return optional<_Up>(); |
| 1295 | } |
| 1296 | |
| 1297 | template <class _Func> |
| 1298 | # if _LIBCPP_STD_VER >= 26 |
| 1299 | requires(is_object_v<_Tp>) |
| 1300 | # endif |
| 1301 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& { |
| 1302 | using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&&>>; |
| 1303 | static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array" ); |
| 1304 | static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t" ); |
| 1305 | static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t" ); |
| 1306 | static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type" ); |
| 1307 | if (*this) |
| 1308 | return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value())); |
| 1309 | return optional<_Up>(); |
| 1310 | } |
| 1311 | |
| 1312 | template <invocable _Func> |
| 1313 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const& |
| 1314 | requires is_copy_constructible_v<_Tp> |
| 1315 | # if _LIBCPP_STD_VER >= 26 |
| 1316 | && (is_object_v<_Tp>) |
| 1317 | # endif |
| 1318 | { |
| 1319 | static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>, |
| 1320 | "Result of f() should be the same type as this optional" ); |
| 1321 | if (*this) |
| 1322 | return *this; |
| 1323 | return std::forward<_Func>(__f)(); |
| 1324 | } |
| 1325 | |
| 1326 | template <invocable _Func> |
| 1327 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) && |
| 1328 | requires is_move_constructible_v<_Tp> |
| 1329 | # if _LIBCPP_STD_VER >= 26 |
| 1330 | && (is_object_v<_Tp>) |
| 1331 | # endif |
| 1332 | { |
| 1333 | static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>, |
| 1334 | "Result of f() should be the same type as this optional" ); |
| 1335 | if (*this) |
| 1336 | return std::move(*this); |
| 1337 | return std::forward<_Func>(__f)(); |
| 1338 | } |
| 1339 | # endif // _LIBCPP_STD_VER >= 23 |
| 1340 | |
| 1341 | using __base::reset; |
| 1342 | |
| 1343 | // optional<T&> overloads |
| 1344 | # if _LIBCPP_STD_VER >= 26 |
| 1345 | |
| 1346 | template <class _Up = remove_cvref_t<_Tp>> |
| 1347 | requires(is_lvalue_reference_v<_Tp> && is_object_v<__libcpp_remove_reference_t<_Tp>> && |
| 1348 | !is_array_v<__libcpp_remove_reference_t<_Tp>>) |
| 1349 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decay_t<_Tp> value_or(_Up&& __v) const { |
| 1350 | static_assert( |
| 1351 | is_constructible_v<remove_cvref_t<_Tp>, _Tp&>, "optional<T&>::value_or: remove_cv_t<T> must be constructible" ); |
| 1352 | static_assert( |
| 1353 | is_convertible_v<_Up, remove_cvref_t<_Tp>>, "optional<T&>::value_or: U must be convertible to remove_cv_t<T>" ); |
| 1354 | return this->has_value() ? this->__get() : static_cast<remove_cvref_t<_Tp>>(std::forward<_Up>(__v)); |
| 1355 | } |
| 1356 | |
| 1357 | template <class _Func> |
| 1358 | requires(is_lvalue_reference_v<_Tp>) |
| 1359 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const { |
| 1360 | using _Up = invoke_result_t<_Func, _Tp&>; |
| 1361 | static_assert(__is_std_optional<remove_cvref_t<_Up>>::value, |
| 1362 | "Result of f(value()) must be a specialization of std::optional" ); |
| 1363 | if (*this) |
| 1364 | return std::invoke(std::forward<_Func>(__f), value()); |
| 1365 | return remove_cvref_t<_Up>(); |
| 1366 | } |
| 1367 | |
| 1368 | template <class _Func> |
| 1369 | requires(is_lvalue_reference_v<_Tp>) |
| 1370 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<remove_cv_t<invoke_result_t<_Func, _Tp&>>> |
| 1371 | transform(_Func&& __f) const { |
| 1372 | using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>; |
| 1373 | static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array" ); |
| 1374 | static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t" ); |
| 1375 | static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t" ); |
| 1376 | static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type" ); |
| 1377 | if (*this) |
| 1378 | return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value()); |
| 1379 | return optional<_Up>(); |
| 1380 | } |
| 1381 | |
| 1382 | template <invocable _Func> |
| 1383 | requires(is_lvalue_reference_v<_Tp>) |
| 1384 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const { |
| 1385 | static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>, |
| 1386 | "Result of f() should be the same type as this optional" ); |
| 1387 | if (*this) |
| 1388 | return *this; |
| 1389 | return std::forward<_Func>(__f)(); |
| 1390 | } |
| 1391 | # endif |
| 1392 | }; |
| 1393 | |
| 1394 | template <class _Tp> |
| 1395 | optional(_Tp) -> optional<_Tp>; |
| 1396 | |
| 1397 | // [optional.relops] Relational operators |
| 1398 | |
| 1399 | template < |
| 1400 | class _Tp, |
| 1401 | class _Up, |
| 1402 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>, |
| 1403 | int> = 0> |
| 1404 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, const optional<_Up>& __y) { |
| 1405 | if (static_cast<bool>(__x) != static_cast<bool>(__y)) |
| 1406 | return false; |
| 1407 | if (!static_cast<bool>(__x)) |
| 1408 | return true; |
| 1409 | return *__x == *__y; |
| 1410 | } |
| 1411 | |
| 1412 | template < |
| 1413 | class _Tp, |
| 1414 | class _Up, |
| 1415 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>, |
| 1416 | int> = 0> |
| 1417 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) { |
| 1418 | if (static_cast<bool>(__x) != static_cast<bool>(__y)) |
| 1419 | return true; |
| 1420 | if (!static_cast<bool>(__x)) |
| 1421 | return false; |
| 1422 | return *__x != *__y; |
| 1423 | } |
| 1424 | |
| 1425 | template < class _Tp, |
| 1426 | class _Up, |
| 1427 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>, |
| 1428 | int> = 0> |
| 1429 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>& __x, const optional<_Up>& __y) { |
| 1430 | if (!static_cast<bool>(__y)) |
| 1431 | return false; |
| 1432 | if (!static_cast<bool>(__x)) |
| 1433 | return true; |
| 1434 | return *__x < *__y; |
| 1435 | } |
| 1436 | |
| 1437 | template < class _Tp, |
| 1438 | class _Up, |
| 1439 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>, |
| 1440 | int> = 0> |
| 1441 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, const optional<_Up>& __y) { |
| 1442 | if (!static_cast<bool>(__x)) |
| 1443 | return false; |
| 1444 | if (!static_cast<bool>(__y)) |
| 1445 | return true; |
| 1446 | return *__x > *__y; |
| 1447 | } |
| 1448 | |
| 1449 | template < |
| 1450 | class _Tp, |
| 1451 | class _Up, |
| 1452 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>, |
| 1453 | int> = 0> |
| 1454 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, const optional<_Up>& __y) { |
| 1455 | if (!static_cast<bool>(__x)) |
| 1456 | return true; |
| 1457 | if (!static_cast<bool>(__y)) |
| 1458 | return false; |
| 1459 | return *__x <= *__y; |
| 1460 | } |
| 1461 | |
| 1462 | template < |
| 1463 | class _Tp, |
| 1464 | class _Up, |
| 1465 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>, |
| 1466 | int> = 0> |
| 1467 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) { |
| 1468 | if (!static_cast<bool>(__y)) |
| 1469 | return true; |
| 1470 | if (!static_cast<bool>(__x)) |
| 1471 | return false; |
| 1472 | return *__x >= *__y; |
| 1473 | } |
| 1474 | |
| 1475 | # if _LIBCPP_STD_VER >= 20 |
| 1476 | |
| 1477 | template <class _Tp, three_way_comparable_with<_Tp> _Up> |
| 1478 | _LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up> |
| 1479 | operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) { |
| 1480 | if (__x && __y) |
| 1481 | return *__x <=> *__y; |
| 1482 | return __x.has_value() <=> __y.has_value(); |
| 1483 | } |
| 1484 | |
| 1485 | # endif // _LIBCPP_STD_VER >= 20 |
| 1486 | |
| 1487 | // [optional.nullops] Comparison with nullopt |
| 1488 | |
| 1489 | template <class _Tp> |
| 1490 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept { |
| 1491 | return !static_cast<bool>(__x); |
| 1492 | } |
| 1493 | |
| 1494 | # if _LIBCPP_STD_VER <= 17 |
| 1495 | |
| 1496 | template <class _Tp> |
| 1497 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(nullopt_t, const optional<_Tp>& __x) noexcept { |
| 1498 | return !static_cast<bool>(__x); |
| 1499 | } |
| 1500 | |
| 1501 | template <class _Tp> |
| 1502 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, nullopt_t) noexcept { |
| 1503 | return static_cast<bool>(__x); |
| 1504 | } |
| 1505 | |
| 1506 | template <class _Tp> |
| 1507 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(nullopt_t, const optional<_Tp>& __x) noexcept { |
| 1508 | return static_cast<bool>(__x); |
| 1509 | } |
| 1510 | |
| 1511 | template <class _Tp> |
| 1512 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>&, nullopt_t) noexcept { |
| 1513 | return false; |
| 1514 | } |
| 1515 | |
| 1516 | template <class _Tp> |
| 1517 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(nullopt_t, const optional<_Tp>& __x) noexcept { |
| 1518 | return static_cast<bool>(__x); |
| 1519 | } |
| 1520 | |
| 1521 | template <class _Tp> |
| 1522 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, nullopt_t) noexcept { |
| 1523 | return !static_cast<bool>(__x); |
| 1524 | } |
| 1525 | |
| 1526 | template <class _Tp> |
| 1527 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(nullopt_t, const optional<_Tp>&) noexcept { |
| 1528 | return true; |
| 1529 | } |
| 1530 | |
| 1531 | template <class _Tp> |
| 1532 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, nullopt_t) noexcept { |
| 1533 | return static_cast<bool>(__x); |
| 1534 | } |
| 1535 | |
| 1536 | template <class _Tp> |
| 1537 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(nullopt_t, const optional<_Tp>&) noexcept { |
| 1538 | return false; |
| 1539 | } |
| 1540 | |
| 1541 | template <class _Tp> |
| 1542 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>&, nullopt_t) noexcept { |
| 1543 | return true; |
| 1544 | } |
| 1545 | |
| 1546 | template <class _Tp> |
| 1547 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(nullopt_t, const optional<_Tp>& __x) noexcept { |
| 1548 | return !static_cast<bool>(__x); |
| 1549 | } |
| 1550 | |
| 1551 | # else // _LIBCPP_STD_VER <= 17 |
| 1552 | |
| 1553 | template <class _Tp> |
| 1554 | _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept { |
| 1555 | return __x.has_value() <=> false; |
| 1556 | } |
| 1557 | |
| 1558 | # endif // _LIBCPP_STD_VER <= 17 |
| 1559 | |
| 1560 | // [optional.comp.with.t] Comparison with T |
| 1561 | |
| 1562 | template < |
| 1563 | class _Tp, |
| 1564 | class _Up, |
| 1565 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>, |
| 1566 | int> = 0> |
| 1567 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, const _Up& __v) { |
| 1568 | if (__x.has_value()) |
| 1569 | return *__x == __v; |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
| 1573 | template < |
| 1574 | class _Tp, |
| 1575 | class _Up, |
| 1576 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>, |
| 1577 | int> = 0> |
| 1578 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const _Tp& __v, const optional<_Up>& __x) { |
| 1579 | if (__x.has_value()) |
| 1580 | return __v == *__x; |
| 1581 | return false; |
| 1582 | } |
| 1583 | |
| 1584 | template < |
| 1585 | class _Tp, |
| 1586 | class _Up, |
| 1587 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>, |
| 1588 | int> = 0> |
| 1589 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, const _Up& __v) { |
| 1590 | if (__x.has_value()) |
| 1591 | return *__x != __v; |
| 1592 | return true; |
| 1593 | } |
| 1594 | |
| 1595 | template < |
| 1596 | class _Tp, |
| 1597 | class _Up, |
| 1598 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>, |
| 1599 | int> = 0> |
| 1600 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const _Tp& __v, const optional<_Up>& __x) { |
| 1601 | if (__x.has_value()) |
| 1602 | return __v != *__x; |
| 1603 | return true; |
| 1604 | } |
| 1605 | |
| 1606 | template < class _Tp, |
| 1607 | class _Up, |
| 1608 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>, |
| 1609 | int> = 0> |
| 1610 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>& __x, const _Up& __v) { |
| 1611 | if (__x.has_value()) |
| 1612 | return *__x < __v; |
| 1613 | return true; |
| 1614 | } |
| 1615 | |
| 1616 | template < class _Tp, |
| 1617 | class _Up, |
| 1618 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>, |
| 1619 | int> = 0> |
| 1620 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const _Tp& __v, const optional<_Up>& __x) { |
| 1621 | if (__x.has_value()) |
| 1622 | return __v < *__x; |
| 1623 | return false; |
| 1624 | } |
| 1625 | |
| 1626 | template < |
| 1627 | class _Tp, |
| 1628 | class _Up, |
| 1629 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>, |
| 1630 | int> = 0> |
| 1631 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, const _Up& __v) { |
| 1632 | if (__x.has_value()) |
| 1633 | return *__x <= __v; |
| 1634 | return true; |
| 1635 | } |
| 1636 | |
| 1637 | template < |
| 1638 | class _Tp, |
| 1639 | class _Up, |
| 1640 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>, |
| 1641 | int> = 0> |
| 1642 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const _Tp& __v, const optional<_Up>& __x) { |
| 1643 | if (__x.has_value()) |
| 1644 | return __v <= *__x; |
| 1645 | return false; |
| 1646 | } |
| 1647 | |
| 1648 | template < class _Tp, |
| 1649 | class _Up, |
| 1650 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>, |
| 1651 | int> = 0> |
| 1652 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, const _Up& __v) { |
| 1653 | if (__x.has_value()) |
| 1654 | return *__x > __v; |
| 1655 | return false; |
| 1656 | } |
| 1657 | |
| 1658 | template < class _Tp, |
| 1659 | class _Up, |
| 1660 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>, |
| 1661 | int> = 0> |
| 1662 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const _Tp& __v, const optional<_Up>& __x) { |
| 1663 | if (__x.has_value()) |
| 1664 | return __v > *__x; |
| 1665 | return true; |
| 1666 | } |
| 1667 | |
| 1668 | template < |
| 1669 | class _Tp, |
| 1670 | class _Up, |
| 1671 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>, |
| 1672 | int> = 0> |
| 1673 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>& __x, const _Up& __v) { |
| 1674 | if (__x.has_value()) |
| 1675 | return *__x >= __v; |
| 1676 | return false; |
| 1677 | } |
| 1678 | |
| 1679 | template < |
| 1680 | class _Tp, |
| 1681 | class _Up, |
| 1682 | enable_if_t<__is_core_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>, |
| 1683 | int> = 0> |
| 1684 | _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const _Tp& __v, const optional<_Up>& __x) { |
| 1685 | if (__x.has_value()) |
| 1686 | return __v >= *__x; |
| 1687 | return true; |
| 1688 | } |
| 1689 | |
| 1690 | # if _LIBCPP_STD_VER >= 20 |
| 1691 | |
| 1692 | template <class _Tp, class _Up> |
| 1693 | requires(!__is_derived_from_optional<_Up>) && three_way_comparable_with<_Tp, _Up> |
| 1694 | _LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up> |
| 1695 | operator<=>(const optional<_Tp>& __x, const _Up& __v) { |
| 1696 | return __x.has_value() ? *__x <=> __v : strong_ordering::less; |
| 1697 | } |
| 1698 | |
| 1699 | # endif // _LIBCPP_STD_VER >= 20 |
| 1700 | |
| 1701 | template <class _Tp, enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, int> = 0> |
| 1702 | inline _LIBCPP_HIDE_FROM_ABI |
| 1703 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) { |
| 1704 | __x.swap(__y); |
| 1705 | } |
| 1706 | |
| 1707 | struct __make_optional_barrier_tag { |
| 1708 | explicit __make_optional_barrier_tag() = default; |
| 1709 | }; |
| 1710 | |
| 1711 | template < |
| 1712 | # if _LIBCPP_STD_VER >= 26 |
| 1713 | __make_optional_barrier_tag = __make_optional_barrier_tag{}, |
| 1714 | # endif |
| 1715 | class _Tp, |
| 1716 | enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>, int> = 0> |
| 1717 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) { |
| 1718 | return optional<decay_t<_Tp>>(std::forward<_Tp>(__v)); |
| 1719 | } |
| 1720 | |
| 1721 | template <class _Tp, class... _Args, enable_if_t<__is_constructible_for_optional_v<_Tp, _Args...>, int> = 0> |
| 1722 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) { |
| 1723 | return optional<_Tp>(in_place, std::forward<_Args>(__args)...); |
| 1724 | } |
| 1725 | |
| 1726 | template <class _Tp, |
| 1727 | class _Up, |
| 1728 | class... _Args, |
| 1729 | enable_if_t<__is_constructible_for_optional_initializer_list_v<_Tp, _Up, _Args...>, int> = 0> |
| 1730 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> |
| 1731 | make_optional(initializer_list<_Up> __il, _Args&&... __args) { |
| 1732 | return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...); |
| 1733 | } |
| 1734 | |
| 1735 | template <class _Tp> |
| 1736 | struct hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > { |
| 1737 | # if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) |
| 1738 | _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type; |
| 1739 | _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type; |
| 1740 | # endif |
| 1741 | |
| 1742 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const { |
| 1743 | return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0; |
| 1744 | } |
| 1745 | }; |
| 1746 | |
| 1747 | _LIBCPP_END_NAMESPACE_STD |
| 1748 | |
| 1749 | # endif // _LIBCPP_STD_VER >= 17 |
| 1750 | |
| 1751 | _LIBCPP_POP_MACROS |
| 1752 | |
| 1753 | # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 |
| 1754 | # include <atomic> |
| 1755 | # include <climits> |
| 1756 | # include <concepts> |
| 1757 | # include <ctime> |
| 1758 | # include <iterator> |
| 1759 | # include <limits> |
| 1760 | # include <memory> |
| 1761 | # include <ratio> |
| 1762 | # include <stdexcept> |
| 1763 | # include <tuple> |
| 1764 | # include <type_traits> |
| 1765 | # include <typeinfo> |
| 1766 | # include <utility> |
| 1767 | # include <variant> |
| 1768 | # endif |
| 1769 | #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) |
| 1770 | |
| 1771 | #endif // _LIBCPP_OPTIONAL |
| 1772 | |