optional 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_OPTIONAL
  10. #define _LIBCPP_OPTIONAL
  11. /*
  12. optional synopsis
  13. // C++1z
  14. namespace std {
  15. // [optional.optional], class template optional
  16. template <class T>
  17. class optional;
  18. template<class T>
  19. concept is-derived-from-optional = requires(const T& t) { // exposition only
  20. []<class U>(const optional<U>&){ }(t);
  21. };
  22. // [optional.nullopt], no-value state indicator
  23. struct nullopt_t{see below };
  24. inline constexpr nullopt_t nullopt(unspecified );
  25. // [optional.bad.access], class bad_optional_access
  26. class bad_optional_access;
  27. // [optional.relops], relational operators
  28. template <class T, class U>
  29. constexpr bool operator==(const optional<T>&, const optional<U>&);
  30. template <class T, class U>
  31. constexpr bool operator!=(const optional<T>&, const optional<U>&);
  32. template <class T, class U>
  33. constexpr bool operator<(const optional<T>&, const optional<U>&);
  34. template <class T, class U>
  35. constexpr bool operator>(const optional<T>&, const optional<U>&);
  36. template <class T, class U>
  37. constexpr bool operator<=(const optional<T>&, const optional<U>&);
  38. template <class T, class U>
  39. constexpr bool operator>=(const optional<T>&, const optional<U>&);
  40. template<class T, three_way_comparable_with<T> U>
  41. constexpr compare_three_way_result_t<T, U>
  42. operator<=>(const optional<T>&, const optional<U>&); // since C++20
  43. // [optional.nullops], comparison with nullopt
  44. template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;
  45. template<class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept; // until C++17
  46. template<class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; // until C++17
  47. template<class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; // until C++17
  48. template<class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept; // until C++17
  49. template<class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept; // until C++17
  50. template<class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; // until C++17
  51. template<class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; // until C++17
  52. template<class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept; // until C++17
  53. template<class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept; // until C++17
  54. template<class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; // until C++17
  55. template<class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; // until C++17
  56. template<class T>
  57. constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // since C++20
  58. // [optional.comp.with.t], comparison with T
  59. template<class T, class U> constexpr bool operator==(const optional<T>&, const U&);
  60. template<class T, class U> constexpr bool operator==(const T&, const optional<U>&);
  61. template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&);
  62. template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&);
  63. template<class T, class U> constexpr bool operator<(const optional<T>&, const U&);
  64. template<class T, class U> constexpr bool operator<(const T&, const optional<U>&);
  65. template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&);
  66. template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&);
  67. template<class T, class U> constexpr bool operator>(const optional<T>&, const U&);
  68. template<class T, class U> constexpr bool operator>(const T&, const optional<U>&);
  69. template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&);
  70. template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&);
  71. template<class T, class U>
  72. requires (!is-derived-from-optional<U>) && three_way_comparable_with<T, U>
  73. constexpr compare_three_way_result_t<T, U>
  74. operator<=>(const optional<T>&, const U&); // since C++20
  75. // [optional.specalg], specialized algorithms
  76. template<class T>
  77. void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20
  78. template<class T>
  79. constexpr optional<see below > make_optional(T&&);
  80. template<class T, class... Args>
  81. constexpr optional<T> make_optional(Args&&... args);
  82. template<class T, class U, class... Args>
  83. constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
  84. // [optional.hash], hash support
  85. template<class T> struct hash;
  86. template<class T> struct hash<optional<T>>;
  87. template<class T>
  88. class optional {
  89. public:
  90. using value_type = T;
  91. // [optional.ctor], constructors
  92. constexpr optional() noexcept;
  93. constexpr optional(nullopt_t) noexcept;
  94. constexpr optional(const optional &);
  95. constexpr optional(optional &&) noexcept(see below);
  96. template<class... Args>
  97. constexpr explicit optional(in_place_t, Args &&...);
  98. template<class U, class... Args>
  99. constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...);
  100. template<class U = T>
  101. constexpr explicit(see-below) optional(U &&);
  102. template<class U>
  103. explicit(see-below) optional(const optional<U> &); // constexpr in C++20
  104. template<class U>
  105. explicit(see-below) optional(optional<U> &&); // constexpr in C++20
  106. // [optional.dtor], destructor
  107. ~optional(); // constexpr in C++20
  108. // [optional.assign], assignment
  109. optional &operator=(nullopt_t) noexcept; // constexpr in C++20
  110. constexpr optional &operator=(const optional &);
  111. constexpr optional &operator=(optional &&) noexcept(see below);
  112. template<class U = T> optional &operator=(U &&); // constexpr in C++20
  113. template<class U> optional &operator=(const optional<U> &); // constexpr in C++20
  114. template<class U> optional &operator=(optional<U> &&); // constexpr in C++20
  115. template<class... Args> T& emplace(Args &&...); // constexpr in C++20
  116. template<class U, class... Args> T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20
  117. // [optional.swap], swap
  118. void swap(optional &) noexcept(see below ); // constexpr in C++20
  119. // [optional.observe], observers
  120. constexpr T const *operator->() const;
  121. constexpr T *operator->();
  122. constexpr T const &operator*() const &;
  123. constexpr T &operator*() &;
  124. constexpr T &&operator*() &&;
  125. constexpr const T &&operator*() const &&;
  126. constexpr explicit operator bool() const noexcept;
  127. constexpr bool has_value() const noexcept;
  128. constexpr T const &value() const &;
  129. constexpr T &value() &;
  130. constexpr T &&value() &&;
  131. constexpr const T &&value() const &&;
  132. template<class U> constexpr T value_or(U &&) const &;
  133. template<class U> constexpr T value_or(U &&) &&;
  134. // [optional.monadic], monadic operations
  135. template<class F> constexpr auto and_then(F&& f) &; // since C++23
  136. template<class F> constexpr auto and_then(F&& f) &&; // since C++23
  137. template<class F> constexpr auto and_then(F&& f) const&; // since C++23
  138. template<class F> constexpr auto and_then(F&& f) const&&; // since C++23
  139. template<class F> constexpr auto transform(F&& f) &; // since C++23
  140. template<class F> constexpr auto transform(F&& f) &&; // since C++23
  141. template<class F> constexpr auto transform(F&& f) const&; // since C++23
  142. template<class F> constexpr auto transform(F&& f) const&&; // since C++23
  143. template<class F> constexpr optional or_else(F&& f) &&; // since C++23
  144. template<class F> constexpr optional or_else(F&& f) const&; // since C++23
  145. // [optional.mod], modifiers
  146. void reset() noexcept; // constexpr in C++20
  147. private:
  148. T *val; // exposition only
  149. };
  150. template<class T>
  151. optional(T) -> optional<T>;
  152. } // namespace std
  153. */
  154. #include <__assert>
  155. #include <__availability>
  156. #include <__compare/compare_three_way_result.h>
  157. #include <__compare/three_way_comparable.h>
  158. #include <__concepts/invocable.h>
  159. #include <__config>
  160. #include <__exception/exception.h>
  161. #include <__functional/hash.h>
  162. #include <__functional/invoke.h>
  163. #include <__functional/unary_function.h>
  164. #include <__fwd/functional.h>
  165. #include <__memory/addressof.h>
  166. #include <__memory/construct_at.h>
  167. #include <__tuple/sfinae_helpers.h>
  168. #include <__type_traits/add_pointer.h>
  169. #include <__type_traits/conditional.h>
  170. #include <__type_traits/conjunction.h>
  171. #include <__type_traits/decay.h>
  172. #include <__type_traits/disjunction.h>
  173. #include <__type_traits/is_array.h>
  174. #include <__type_traits/is_assignable.h>
  175. #include <__type_traits/is_constructible.h>
  176. #include <__type_traits/is_convertible.h>
  177. #include <__type_traits/is_destructible.h>
  178. #include <__type_traits/is_nothrow_assignable.h>
  179. #include <__type_traits/is_nothrow_constructible.h>
  180. #include <__type_traits/is_object.h>
  181. #include <__type_traits/is_reference.h>
  182. #include <__type_traits/is_scalar.h>
  183. #include <__type_traits/is_swappable.h>
  184. #include <__type_traits/is_trivially_assignable.h>
  185. #include <__type_traits/is_trivially_constructible.h>
  186. #include <__type_traits/is_trivially_destructible.h>
  187. #include <__type_traits/negation.h>
  188. #include <__type_traits/remove_const.h>
  189. #include <__type_traits/remove_cvref.h>
  190. #include <__type_traits/remove_reference.h>
  191. #include <__utility/declval.h>
  192. #include <__utility/forward.h>
  193. #include <__utility/in_place.h>
  194. #include <__utility/move.h>
  195. #include <__utility/swap.h>
  196. #include <__verbose_abort>
  197. #include <initializer_list>
  198. #include <new>
  199. #include <version>
  200. // standard-mandated includes
  201. // [optional.syn]
  202. #include <compare>
  203. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  204. # pragma GCC system_header
  205. #endif
  206. _LIBCPP_PUSH_MACROS
  207. #include <__undef_macros>
  208. namespace std // purposefully not using versioning namespace
  209. {
  210. class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access : public exception {
  211. public:
  212. _LIBCPP_HIDE_FROM_ABI bad_optional_access() _NOEXCEPT = default;
  213. _LIBCPP_HIDE_FROM_ABI bad_optional_access(const bad_optional_access&) _NOEXCEPT = default;
  214. _LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default;
  215. // Get the key function ~bad_optional_access() into the dylib
  216. ~bad_optional_access() _NOEXCEPT override;
  217. const char* what() const _NOEXCEPT override;
  218. };
  219. } // namespace std
  220. #if _LIBCPP_STD_VER >= 17
  221. _LIBCPP_BEGIN_NAMESPACE_STD
  222. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS void
  223. __throw_bad_optional_access() {
  224. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  225. throw bad_optional_access();
  226. # else
  227. _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode");
  228. # endif
  229. }
  230. struct nullopt_t {
  231. struct __secret_tag {
  232. explicit __secret_tag() = default;
  233. };
  234. _LIBCPP_HIDE_FROM_ABI constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
  235. };
  236. inline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
  237. struct __optional_construct_from_invoke_tag {};
  238. template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
  239. struct __optional_destruct_base;
  240. template <class _Tp>
  241. struct __optional_destruct_base<_Tp, false> {
  242. typedef _Tp value_type;
  243. static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
  244. union {
  245. char __null_state_;
  246. value_type __val_;
  247. };
  248. bool __engaged_;
  249. _LIBCPP_HIDE_FROM_ABI ~__optional_destruct_base() {
  250. if (__engaged_)
  251. __val_.~value_type();
  252. }
  253. _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
  254. template <class... _Args>
  255. _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
  256. : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
  257. # if _LIBCPP_STD_VER >= 23
  258. template <class _Fp, class... _Args>
  259. _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base(
  260. __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
  261. : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
  262. # endif
  263. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
  264. if (__engaged_) {
  265. __val_.~value_type();
  266. __engaged_ = false;
  267. }
  268. }
  269. };
  270. template <class _Tp>
  271. struct __optional_destruct_base<_Tp, true> {
  272. typedef _Tp value_type;
  273. static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
  274. union {
  275. char __null_state_;
  276. value_type __val_;
  277. };
  278. bool __engaged_;
  279. _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
  280. template <class... _Args>
  281. _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
  282. : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
  283. # if _LIBCPP_STD_VER >= 23
  284. template <class _Fp, class... _Args>
  285. _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base(
  286. __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
  287. : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
  288. # endif
  289. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
  290. if (__engaged_) {
  291. __engaged_ = false;
  292. }
  293. }
  294. };
  295. template <class _Tp, bool = is_reference<_Tp>::value>
  296. struct __optional_storage_base : __optional_destruct_base<_Tp> {
  297. using __base = __optional_destruct_base<_Tp>;
  298. using value_type = _Tp;
  299. using __base::__base;
  300. _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; }
  301. _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; }
  302. _LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; }
  303. _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() && noexcept { return std::move(this->__val_); }
  304. _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& __get() const&& noexcept { return std::move(this->__val_); }
  305. template <class... _Args>
  306. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_Args&&... __args) {
  307. _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
  308. std::__construct_at(std::addressof(this->__val_), std::forward<_Args>(__args)...);
  309. this->__engaged_ = true;
  310. }
  311. template <class _That>
  312. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
  313. if (__opt.has_value())
  314. __construct(std::forward<_That>(__opt).__get());
  315. }
  316. template <class _That>
  317. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
  318. if (this->__engaged_ == __opt.has_value()) {
  319. if (this->__engaged_)
  320. this->__val_ = std::forward<_That>(__opt).__get();
  321. } else {
  322. if (this->__engaged_)
  323. this->reset();
  324. else
  325. __construct(std::forward<_That>(__opt).__get());
  326. }
  327. }
  328. };
  329. // optional<T&> is currently required to be ill-formed. However, it may
  330. // be allowed in the future. For this reason, it has already been implemented
  331. // to ensure we can make the change in an ABI-compatible manner.
  332. template <class _Tp>
  333. struct __optional_storage_base<_Tp, true> {
  334. using value_type = _Tp;
  335. using __raw_type = remove_reference_t<_Tp>;
  336. __raw_type* __value_;
  337. template <class _Up>
  338. static _LIBCPP_HIDE_FROM_ABI constexpr bool __can_bind_reference() {
  339. using _RawUp = __libcpp_remove_reference_t<_Up>;
  340. using _UpPtr = _RawUp*;
  341. using _RawTp = __libcpp_remove_reference_t<_Tp>;
  342. using _TpPtr = _RawTp*;
  343. using _CheckLValueArg =
  344. integral_constant<bool,
  345. (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value) ||
  346. is_same<_RawUp, reference_wrapper<_RawTp>>::value ||
  347. is_same<_RawUp, reference_wrapper<__remove_const_t<_RawTp>>>::value >;
  348. return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value) ||
  349. (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value &&
  350. is_convertible<_UpPtr, _TpPtr>::value);
  351. }
  352. _LIBCPP_HIDE_FROM_ABI constexpr __optional_storage_base() noexcept : __value_(nullptr) {}
  353. template <class _UArg>
  354. _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg)
  355. : __value_(std::addressof(__uarg)) {
  356. static_assert(__can_bind_reference<_UArg>(),
  357. "Attempted to construct a reference element in tuple from a "
  358. "possible temporary");
  359. }
  360. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; }
  361. _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; }
  362. _LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const& noexcept { return *__value_; }
  363. _LIBCPP_HIDE_FROM_ABI constexpr value_type&& __get() const&& noexcept { return std::forward<value_type>(*__value_); }
  364. template <class _UArg>
  365. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_UArg&& __val) {
  366. _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
  367. static_assert(__can_bind_reference<_UArg>(),
  368. "Attempted to construct a reference element in tuple from a "
  369. "possible temporary");
  370. __value_ = std::addressof(__val);
  371. }
  372. template <class _That>
  373. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
  374. if (__opt.has_value())
  375. __construct(std::forward<_That>(__opt).__get());
  376. }
  377. template <class _That>
  378. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
  379. if (has_value() == __opt.has_value()) {
  380. if (has_value())
  381. *__value_ = std::forward<_That>(__opt).__get();
  382. } else {
  383. if (has_value())
  384. reset();
  385. else
  386. __construct(std::forward<_That>(__opt).__get());
  387. }
  388. }
  389. };
  390. template <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value>
  391. struct __optional_copy_base : __optional_storage_base<_Tp> {
  392. using __optional_storage_base<_Tp>::__optional_storage_base;
  393. };
  394. template <class _Tp>
  395. struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> {
  396. using __optional_storage_base<_Tp>::__optional_storage_base;
  397. _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default;
  398. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) {
  399. this->__construct_from(__opt);
  400. }
  401. _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&) = default;
  402. _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default;
  403. _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&) = default;
  404. };
  405. template <class _Tp, bool = is_trivially_move_constructible<_Tp>::value>
  406. struct __optional_move_base : __optional_copy_base<_Tp> {
  407. using __optional_copy_base<_Tp>::__optional_copy_base;
  408. };
  409. template <class _Tp>
  410. struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> {
  411. using value_type = _Tp;
  412. using __optional_copy_base<_Tp>::__optional_copy_base;
  413. _LIBCPP_HIDE_FROM_ABI __optional_move_base() = default;
  414. _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default;
  415. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  416. __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) {
  417. this->__construct_from(std::move(__opt));
  418. }
  419. _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default;
  420. _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&) = default;
  421. };
  422. template <class _Tp,
  423. bool = is_trivially_destructible<_Tp>::value && is_trivially_copy_constructible<_Tp>::value &&
  424. is_trivially_copy_assignable<_Tp>::value>
  425. struct __optional_copy_assign_base : __optional_move_base<_Tp> {
  426. using __optional_move_base<_Tp>::__optional_move_base;
  427. };
  428. template <class _Tp>
  429. struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> {
  430. using __optional_move_base<_Tp>::__optional_move_base;
  431. _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base() = default;
  432. _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
  433. _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&) = default;
  434. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base&
  435. operator=(const __optional_copy_assign_base& __opt) {
  436. this->__assign_from(__opt);
  437. return *this;
  438. }
  439. _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
  440. };
  441. template <class _Tp,
  442. bool = is_trivially_destructible<_Tp>::value && is_trivially_move_constructible<_Tp>::value &&
  443. is_trivially_move_assignable<_Tp>::value>
  444. struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> {
  445. using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
  446. };
  447. template <class _Tp>
  448. struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> {
  449. using value_type = _Tp;
  450. using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
  451. _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base() = default;
  452. _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;
  453. _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&) = default;
  454. _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
  455. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base&
  456. operator=(__optional_move_assign_base&& __opt) noexcept(
  457. is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) {
  458. this->__assign_from(std::move(__opt));
  459. return *this;
  460. }
  461. };
  462. template <class _Tp>
  463. using __optional_sfinae_ctor_base_t =
  464. __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >;
  465. template <class _Tp>
  466. using __optional_sfinae_assign_base_t =
  467. __sfinae_assign_base< (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),
  468. (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value) >;
  469. template <class _Tp>
  470. class optional;
  471. # if _LIBCPP_STD_VER >= 20
  472. template <class _Tp>
  473. concept __is_derived_from_optional = requires(const _Tp& __t) { []<class _Up>(const optional<_Up>&) {}(__t); };
  474. # endif // _LIBCPP_STD_VER >= 20
  475. template <class _Tp>
  476. struct __is_std_optional : false_type {};
  477. template <class _Tp>
  478. struct __is_std_optional<optional<_Tp>> : true_type {};
  479. template <class _Tp>
  480. class _LIBCPP_DECLSPEC_EMPTY_BASES optional
  481. : private __optional_move_assign_base<_Tp>,
  482. private __optional_sfinae_ctor_base_t<_Tp>,
  483. private __optional_sfinae_assign_base_t<_Tp> {
  484. using __base = __optional_move_assign_base<_Tp>;
  485. public:
  486. using value_type = _Tp;
  487. private:
  488. // Disable the reference extension using this static assert.
  489. static_assert(!is_same_v<__remove_cvref_t<value_type>, in_place_t>,
  490. "instantiation of optional with in_place_t is ill-formed");
  491. static_assert(!is_same_v<__remove_cvref_t<value_type>, nullopt_t>,
  492. "instantiation of optional with nullopt_t is ill-formed");
  493. static_assert(!is_reference_v<value_type>, "instantiation of optional with a reference type is ill-formed");
  494. static_assert(is_destructible_v<value_type>, "instantiation of optional with a non-destructible type is ill-formed");
  495. static_assert(!is_array_v<value_type>, "instantiation of optional with an array type is ill-formed");
  496. // LWG2756: conditionally explicit conversion from _Up
  497. struct _CheckOptionalArgsConstructor {
  498. template <class _Up>
  499. _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
  500. return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>;
  501. }
  502. template <class _Up>
  503. _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
  504. return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>;
  505. }
  506. };
  507. template <class _Up>
  508. using _CheckOptionalArgsCtor =
  509. _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value &&
  510. (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional<__remove_cvref_t<_Up>>::value),
  511. _CheckOptionalArgsConstructor,
  512. __check_tuple_constructor_fail >;
  513. template <class _QualUp>
  514. struct _CheckOptionalLikeConstructor {
  515. template <class _Up, class _Opt = optional<_Up>>
  516. using __check_constructible_from_opt =
  517. _Or< is_constructible<_Tp, _Opt&>,
  518. is_constructible<_Tp, _Opt const&>,
  519. is_constructible<_Tp, _Opt&&>,
  520. is_constructible<_Tp, _Opt const&&>,
  521. is_convertible<_Opt&, _Tp>,
  522. is_convertible<_Opt const&, _Tp>,
  523. is_convertible<_Opt&&, _Tp>,
  524. is_convertible<_Opt const&&, _Tp> >;
  525. template <class _Up, class _Opt = optional<_Up>>
  526. using __check_assignable_from_opt =
  527. _Or< is_assignable<_Tp&, _Opt&>,
  528. is_assignable<_Tp&, _Opt const&>,
  529. is_assignable<_Tp&, _Opt&&>,
  530. is_assignable<_Tp&, _Opt const&&> >;
  531. template <class _Up, class _QUp = _QualUp>
  532. _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
  533. return is_convertible<_QUp, _Tp>::value &&
  534. (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
  535. }
  536. template <class _Up, class _QUp = _QualUp>
  537. _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
  538. return !is_convertible<_QUp, _Tp>::value &&
  539. (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
  540. }
  541. template <class _Up, class _QUp = _QualUp>
  542. _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() {
  543. // Construction and assignability of _QUp to _Tp has already been
  544. // checked.
  545. return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value;
  546. }
  547. };
  548. template <class _Up, class _QualUp>
  549. using _CheckOptionalLikeCtor =
  550. _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value,
  551. _CheckOptionalLikeConstructor<_QualUp>,
  552. __check_tuple_constructor_fail >;
  553. template <class _Up, class _QualUp>
  554. using _CheckOptionalLikeAssign =
  555. _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value,
  556. _CheckOptionalLikeConstructor<_QualUp>,
  557. __check_tuple_constructor_fail >;
  558. public:
  559. _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {}
  560. _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default;
  561. _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&) = default;
  562. _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {}
  563. template <
  564. class _InPlaceT,
  565. class... _Args,
  566. class = enable_if_t< _And< _IsSame<_InPlaceT, in_place_t>, is_constructible<value_type, _Args...> >::value > >
  567. _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args)
  568. : __base(in_place, std::forward<_Args>(__args)...) {}
  569. template <class _Up,
  570. class... _Args,
  571. class = enable_if_t< is_constructible_v<value_type, initializer_list<_Up>&, _Args...>> >
  572. _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
  573. : __base(in_place, __il, std::forward<_Args>(__args)...) {}
  574. template <class _Up = value_type,
  575. enable_if_t< _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
  576. _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}
  577. template <class _Up, enable_if_t< _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>
  578. _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v) : __base(in_place, std::forward<_Up>(__v)) {}
  579. // LWG2756: conditionally explicit conversion from const optional<_Up>&
  580. template <class _Up,
  581. enable_if_t< _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
  582. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v) {
  583. this->__construct_from(__v);
  584. }
  585. template <class _Up,
  586. enable_if_t< _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
  587. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v) {
  588. this->__construct_from(__v);
  589. }
  590. // LWG2756: conditionally explicit conversion from optional<_Up>&&
  591. template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>
  592. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v) {
  593. this->__construct_from(std::move(__v));
  594. }
  595. template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>
  596. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v) {
  597. this->__construct_from(std::move(__v));
  598. }
  599. # if _LIBCPP_STD_VER >= 23
  600. template <class _Fp, class... _Args>
  601. _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
  602. : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}
  603. # endif
  604. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept {
  605. reset();
  606. return *this;
  607. }
  608. _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default;
  609. _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default;
  610. // LWG2756
  611. template <
  612. class _Up = value_type,
  613. class = enable_if_t< _And< _IsNotSame<__remove_cvref_t<_Up>, optional>,
  614. _Or< _IsNotSame<__remove_cvref_t<_Up>, value_type>, _Not<is_scalar<value_type>> >,
  615. is_constructible<value_type, _Up>,
  616. is_assignable<value_type&, _Up> >::value> >
  617. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) {
  618. if (this->has_value())
  619. this->__get() = std::forward<_Up>(__v);
  620. else
  621. this->__construct(std::forward<_Up>(__v));
  622. return *this;
  623. }
  624. // LWG2756
  625. template <class _Up,
  626. enable_if_t< _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0>
  627. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) {
  628. this->__assign_from(__v);
  629. return *this;
  630. }
  631. // LWG2756
  632. template <class _Up, enable_if_t< _CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0>
  633. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) {
  634. this->__assign_from(std::move(__v));
  635. return *this;
  636. }
  637. template <class... _Args, class = enable_if_t< is_constructible_v<value_type, _Args...> > >
  638. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
  639. reset();
  640. this->__construct(std::forward<_Args>(__args)...);
  641. return this->__get();
  642. }
  643. template <class _Up,
  644. class... _Args,
  645. class = enable_if_t< is_constructible_v<value_type, initializer_list<_Up>&, _Args...> > >
  646. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
  647. reset();
  648. this->__construct(__il, std::forward<_Args>(__args)...);
  649. return this->__get();
  650. }
  651. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  652. swap(optional& __opt) noexcept(is_nothrow_move_constructible_v<value_type> && is_nothrow_swappable_v<value_type>) {
  653. if (this->has_value() == __opt.has_value()) {
  654. using std::swap;
  655. if (this->has_value())
  656. swap(this->__get(), __opt.__get());
  657. } else {
  658. if (this->has_value()) {
  659. __opt.__construct(std::move(this->__get()));
  660. reset();
  661. } else {
  662. this->__construct(std::move(__opt.__get()));
  663. __opt.reset();
  664. }
  665. }
  666. }
  667. _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type const> operator->() const {
  668. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
  669. return std::addressof(this->__get());
  670. }
  671. _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<value_type> operator->() {
  672. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
  673. return std::addressof(this->__get());
  674. }
  675. _LIBCPP_HIDE_FROM_ABI constexpr const value_type& operator*() const& noexcept {
  676. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
  677. return this->__get();
  678. }
  679. _LIBCPP_HIDE_FROM_ABI constexpr value_type& operator*() & noexcept {
  680. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
  681. return this->__get();
  682. }
  683. _LIBCPP_HIDE_FROM_ABI constexpr value_type&& operator*() && noexcept {
  684. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
  685. return std::move(this->__get());
  686. }
  687. _LIBCPP_HIDE_FROM_ABI constexpr const value_type&& operator*() const&& noexcept {
  688. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
  689. return std::move(this->__get());
  690. }
  691. _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); }
  692. using __base::__get;
  693. using __base::has_value;
  694. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type const& value() const& {
  695. if (!this->has_value())
  696. __throw_bad_optional_access();
  697. return this->__get();
  698. }
  699. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type& value() & {
  700. if (!this->has_value())
  701. __throw_bad_optional_access();
  702. return this->__get();
  703. }
  704. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type&& value() && {
  705. if (!this->has_value())
  706. __throw_bad_optional_access();
  707. return std::move(this->__get());
  708. }
  709. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr value_type const&& value() const&& {
  710. if (!this->has_value())
  711. __throw_bad_optional_access();
  712. return std::move(this->__get());
  713. }
  714. template <class _Up>
  715. _LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) const& {
  716. static_assert(is_copy_constructible_v<value_type>, "optional<T>::value_or: T must be copy constructible");
  717. static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
  718. return this->has_value() ? this->__get() : static_cast<value_type>(std::forward<_Up>(__v));
  719. }
  720. template <class _Up>
  721. _LIBCPP_HIDE_FROM_ABI constexpr value_type value_or(_Up&& __v) && {
  722. static_assert(is_move_constructible_v<value_type>, "optional<T>::value_or: T must be move constructible");
  723. static_assert(is_convertible_v<_Up, value_type>, "optional<T>::value_or: U must be convertible to T");
  724. return this->has_value() ? std::move(this->__get()) : static_cast<value_type>(std::forward<_Up>(__v));
  725. }
  726. # if _LIBCPP_STD_VER >= 23
  727. template <class _Func>
  728. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) & {
  729. using _Up = invoke_result_t<_Func, value_type&>;
  730. static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
  731. "Result of f(value()) must be a specialization of std::optional");
  732. if (*this)
  733. return std::invoke(std::forward<_Func>(__f), value());
  734. return remove_cvref_t<_Up>();
  735. }
  736. template <class _Func>
  737. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) const& {
  738. using _Up = invoke_result_t<_Func, const value_type&>;
  739. static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
  740. "Result of f(value()) must be a specialization of std::optional");
  741. if (*this)
  742. return std::invoke(std::forward<_Func>(__f), value());
  743. return remove_cvref_t<_Up>();
  744. }
  745. template <class _Func>
  746. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto and_then(_Func&& __f) && {
  747. using _Up = invoke_result_t<_Func, value_type&&>;
  748. static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
  749. "Result of f(std::move(value())) must be a specialization of std::optional");
  750. if (*this)
  751. return std::invoke(std::forward<_Func>(__f), std::move(value()));
  752. return remove_cvref_t<_Up>();
  753. }
  754. template <class _Func>
  755. _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
  756. using _Up = invoke_result_t<_Func, const value_type&&>;
  757. static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
  758. "Result of f(std::move(value())) must be a specialization of std::optional");
  759. if (*this)
  760. return std::invoke(std::forward<_Func>(__f), std::move(value()));
  761. return remove_cvref_t<_Up>();
  762. }
  763. template <class _Func>
  764. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) & {
  765. using _Up = remove_cv_t<invoke_result_t<_Func, value_type&>>;
  766. static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
  767. static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
  768. static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
  769. static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
  770. if (*this)
  771. return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
  772. return optional<_Up>();
  773. }
  774. template <class _Func>
  775. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) const& {
  776. using _Up = remove_cv_t<invoke_result_t<_Func, const value_type&>>;
  777. static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
  778. static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
  779. static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
  780. static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
  781. if (*this)
  782. return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
  783. return optional<_Up>();
  784. }
  785. template <class _Func>
  786. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) && {
  787. using _Up = remove_cv_t<invoke_result_t<_Func, value_type&&>>;
  788. static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
  789. static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
  790. static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
  791. static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
  792. if (*this)
  793. return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
  794. return optional<_Up>();
  795. }
  796. template <class _Func>
  797. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS constexpr auto transform(_Func&& __f) const&& {
  798. using _Up = remove_cvref_t<invoke_result_t<_Func, const value_type&&>>;
  799. static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
  800. static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
  801. static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
  802. static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
  803. if (*this)
  804. return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
  805. return optional<_Up>();
  806. }
  807. template <invocable _Func>
  808. _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
  809. requires is_copy_constructible_v<value_type>
  810. {
  811. static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
  812. "Result of f() should be the same type as this optional");
  813. if (*this)
  814. return *this;
  815. return std::forward<_Func>(__f)();
  816. }
  817. template <invocable _Func>
  818. _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
  819. requires is_move_constructible_v<value_type>
  820. {
  821. static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
  822. "Result of f() should be the same type as this optional");
  823. if (*this)
  824. return std::move(*this);
  825. return std::forward<_Func>(__f)();
  826. }
  827. # endif // _LIBCPP_STD_VER >= 23
  828. using __base::reset;
  829. };
  830. # if _LIBCPP_STD_VER >= 17
  831. template <class _Tp>
  832. optional(_Tp) -> optional<_Tp>;
  833. # endif
  834. // Comparisons between optionals
  835. template <class _Tp, class _Up>
  836. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  837. is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
  838. bool >
  839. operator==(const optional<_Tp>& __x, const optional<_Up>& __y) {
  840. if (static_cast<bool>(__x) != static_cast<bool>(__y))
  841. return false;
  842. if (!static_cast<bool>(__x))
  843. return true;
  844. return *__x == *__y;
  845. }
  846. template <class _Tp, class _Up>
  847. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  848. is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
  849. bool >
  850. operator!=(const optional<_Tp>& __x, const optional<_Up>& __y) {
  851. if (static_cast<bool>(__x) != static_cast<bool>(__y))
  852. return true;
  853. if (!static_cast<bool>(__x))
  854. return false;
  855. return *__x != *__y;
  856. }
  857. template <class _Tp, class _Up>
  858. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  859. is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
  860. bool >
  861. operator<(const optional<_Tp>& __x, const optional<_Up>& __y) {
  862. if (!static_cast<bool>(__y))
  863. return false;
  864. if (!static_cast<bool>(__x))
  865. return true;
  866. return *__x < *__y;
  867. }
  868. template <class _Tp, class _Up>
  869. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  870. is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
  871. bool >
  872. operator>(const optional<_Tp>& __x, const optional<_Up>& __y) {
  873. if (!static_cast<bool>(__x))
  874. return false;
  875. if (!static_cast<bool>(__y))
  876. return true;
  877. return *__x > *__y;
  878. }
  879. template <class _Tp, class _Up>
  880. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  881. is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
  882. bool >
  883. operator<=(const optional<_Tp>& __x, const optional<_Up>& __y) {
  884. if (!static_cast<bool>(__x))
  885. return true;
  886. if (!static_cast<bool>(__y))
  887. return false;
  888. return *__x <= *__y;
  889. }
  890. template <class _Tp, class _Up>
  891. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  892. is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
  893. bool >
  894. operator>=(const optional<_Tp>& __x, const optional<_Up>& __y) {
  895. if (!static_cast<bool>(__y))
  896. return true;
  897. if (!static_cast<bool>(__x))
  898. return false;
  899. return *__x >= *__y;
  900. }
  901. # if _LIBCPP_STD_VER >= 20
  902. template <class _Tp, three_way_comparable_with<_Tp> _Up>
  903. _LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
  904. operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) {
  905. if (__x && __y)
  906. return *__x <=> *__y;
  907. return __x.has_value() <=> __y.has_value();
  908. }
  909. # endif // _LIBCPP_STD_VER >= 20
  910. // Comparisons with nullopt
  911. template <class _Tp>
  912. _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const optional<_Tp>& __x, nullopt_t) noexcept {
  913. return !static_cast<bool>(__x);
  914. }
  915. # if _LIBCPP_STD_VER <= 17
  916. template <class _Tp>
  917. _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(nullopt_t, const optional<_Tp>& __x) noexcept {
  918. return !static_cast<bool>(__x);
  919. }
  920. template <class _Tp>
  921. _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const optional<_Tp>& __x, nullopt_t) noexcept {
  922. return static_cast<bool>(__x);
  923. }
  924. template <class _Tp>
  925. _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(nullopt_t, const optional<_Tp>& __x) noexcept {
  926. return static_cast<bool>(__x);
  927. }
  928. template <class _Tp>
  929. _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const optional<_Tp>&, nullopt_t) noexcept {
  930. return false;
  931. }
  932. template <class _Tp>
  933. _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(nullopt_t, const optional<_Tp>& __x) noexcept {
  934. return static_cast<bool>(__x);
  935. }
  936. template <class _Tp>
  937. _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const optional<_Tp>& __x, nullopt_t) noexcept {
  938. return !static_cast<bool>(__x);
  939. }
  940. template <class _Tp>
  941. _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(nullopt_t, const optional<_Tp>&) noexcept {
  942. return true;
  943. }
  944. template <class _Tp>
  945. _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const optional<_Tp>& __x, nullopt_t) noexcept {
  946. return static_cast<bool>(__x);
  947. }
  948. template <class _Tp>
  949. _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(nullopt_t, const optional<_Tp>&) noexcept {
  950. return false;
  951. }
  952. template <class _Tp>
  953. _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const optional<_Tp>&, nullopt_t) noexcept {
  954. return true;
  955. }
  956. template <class _Tp>
  957. _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(nullopt_t, const optional<_Tp>& __x) noexcept {
  958. return !static_cast<bool>(__x);
  959. }
  960. # else // _LIBCPP_STD_VER <= 17
  961. template <class _Tp>
  962. _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept {
  963. return __x.has_value() <=> false;
  964. }
  965. # endif // _LIBCPP_STD_VER <= 17
  966. // Comparisons with T
  967. template <class _Tp, class _Up>
  968. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  969. is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
  970. bool >
  971. operator==(const optional<_Tp>& __x, const _Up& __v) {
  972. return static_cast<bool>(__x) ? *__x == __v : false;
  973. }
  974. template <class _Tp, class _Up>
  975. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  976. is_convertible_v<decltype(std::declval<const _Tp&>() == std::declval<const _Up&>()), bool>,
  977. bool >
  978. operator==(const _Tp& __v, const optional<_Up>& __x) {
  979. return static_cast<bool>(__x) ? __v == *__x : false;
  980. }
  981. template <class _Tp, class _Up>
  982. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  983. is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
  984. bool >
  985. operator!=(const optional<_Tp>& __x, const _Up& __v) {
  986. return static_cast<bool>(__x) ? *__x != __v : true;
  987. }
  988. template <class _Tp, class _Up>
  989. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  990. is_convertible_v<decltype(std::declval<const _Tp&>() != std::declval<const _Up&>()), bool>,
  991. bool >
  992. operator!=(const _Tp& __v, const optional<_Up>& __x) {
  993. return static_cast<bool>(__x) ? __v != *__x : true;
  994. }
  995. template <class _Tp, class _Up>
  996. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  997. is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
  998. bool >
  999. operator<(const optional<_Tp>& __x, const _Up& __v) {
  1000. return static_cast<bool>(__x) ? *__x < __v : true;
  1001. }
  1002. template <class _Tp, class _Up>
  1003. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  1004. is_convertible_v<decltype(std::declval<const _Tp&>() < std::declval<const _Up&>()), bool>,
  1005. bool >
  1006. operator<(const _Tp& __v, const optional<_Up>& __x) {
  1007. return static_cast<bool>(__x) ? __v < *__x : false;
  1008. }
  1009. template <class _Tp, class _Up>
  1010. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  1011. is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
  1012. bool >
  1013. operator<=(const optional<_Tp>& __x, const _Up& __v) {
  1014. return static_cast<bool>(__x) ? *__x <= __v : true;
  1015. }
  1016. template <class _Tp, class _Up>
  1017. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  1018. is_convertible_v<decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>()), bool>,
  1019. bool >
  1020. operator<=(const _Tp& __v, const optional<_Up>& __x) {
  1021. return static_cast<bool>(__x) ? __v <= *__x : false;
  1022. }
  1023. template <class _Tp, class _Up>
  1024. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  1025. is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
  1026. bool >
  1027. operator>(const optional<_Tp>& __x, const _Up& __v) {
  1028. return static_cast<bool>(__x) ? *__x > __v : false;
  1029. }
  1030. template <class _Tp, class _Up>
  1031. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  1032. is_convertible_v<decltype(std::declval<const _Tp&>() > std::declval<const _Up&>()), bool>,
  1033. bool >
  1034. operator>(const _Tp& __v, const optional<_Up>& __x) {
  1035. return static_cast<bool>(__x) ? __v > *__x : true;
  1036. }
  1037. template <class _Tp, class _Up>
  1038. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  1039. is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
  1040. bool >
  1041. operator>=(const optional<_Tp>& __x, const _Up& __v) {
  1042. return static_cast<bool>(__x) ? *__x >= __v : false;
  1043. }
  1044. template <class _Tp, class _Up>
  1045. _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<
  1046. is_convertible_v<decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>()), bool>,
  1047. bool >
  1048. operator>=(const _Tp& __v, const optional<_Up>& __x) {
  1049. return static_cast<bool>(__x) ? __v >= *__x : true;
  1050. }
  1051. # if _LIBCPP_STD_VER >= 20
  1052. template <class _Tp, class _Up>
  1053. requires(!__is_derived_from_optional<_Up>) && three_way_comparable_with<_Tp, _Up>
  1054. _LIBCPP_HIDE_FROM_ABI constexpr compare_three_way_result_t<_Tp, _Up>
  1055. operator<=>(const optional<_Tp>& __x, const _Up& __v) {
  1056. return __x.has_value() ? *__x <=> __v : strong_ordering::less;
  1057. }
  1058. # endif // _LIBCPP_STD_VER >= 20
  1059. template <class _Tp>
  1060. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  1061. enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, void >
  1062. swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
  1063. __x.swap(__y);
  1064. }
  1065. template <class _Tp>
  1066. _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
  1067. return optional<decay_t<_Tp>>(std::forward<_Tp>(__v));
  1068. }
  1069. template <class _Tp, class... _Args>
  1070. _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
  1071. return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
  1072. }
  1073. template <class _Tp, class _Up, class... _Args>
  1074. _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) {
  1075. return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
  1076. }
  1077. template <class _Tp>
  1078. struct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > {
  1079. # if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
  1080. _LIBCPP_DEPRECATED_IN_CXX17 typedef optional<_Tp> argument_type;
  1081. _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
  1082. # endif
  1083. _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
  1084. return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
  1085. }
  1086. };
  1087. _LIBCPP_END_NAMESPACE_STD
  1088. #endif // _LIBCPP_STD_VER >= 17
  1089. _LIBCPP_POP_MACROS
  1090. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1091. # include <atomic>
  1092. # include <climits>
  1093. # include <concepts>
  1094. # include <ctime>
  1095. # include <iterator>
  1096. # include <limits>
  1097. # include <memory>
  1098. # include <ratio>
  1099. # include <stdexcept>
  1100. # include <tuple>
  1101. # include <type_traits>
  1102. # include <typeinfo>
  1103. # include <utility>
  1104. # include <variant>
  1105. #endif
  1106. #endif // _LIBCPP_OPTIONAL