optional 52 KB

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