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