optional 55 KB

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