optional 42 KB

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