optional 51 KB

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