optional 50 KB

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