optional 51 KB

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