optional 50 KB

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