optional 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. // <optional> -*- C++ -*-
  2. // Copyright (C) 2013-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/optional
  21. * This is a TS C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_EXPERIMENTAL_OPTIONAL
  24. #define _GLIBCXX_EXPERIMENTAL_OPTIONAL 1
  25. /**
  26. * @defgroup experimental Experimental
  27. *
  28. * Components specified by various Technical Specifications.
  29. */
  30. #if __cplusplus <= 201103L
  31. # include <bits/c++14_warning.h>
  32. #else
  33. #include <utility>
  34. #include <type_traits>
  35. #include <stdexcept>
  36. #include <new>
  37. #include <initializer_list>
  38. #include <bits/functexcept.h>
  39. #include <bits/functional_hash.h>
  40. #include <bits/enable_special_members.h>
  41. namespace std _GLIBCXX_VISIBILITY(default)
  42. {
  43. namespace experimental
  44. {
  45. inline namespace fundamentals_v1
  46. {
  47. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  48. /**
  49. * @defgroup optional Optional values
  50. * @ingroup experimental
  51. *
  52. * Class template for optional values and surrounding facilities, as
  53. * described in n3793 "A proposal to add a utility class to represent
  54. * optional objects (Revision 5)".
  55. *
  56. * @{
  57. */
  58. #define __cpp_lib_experimental_optional 201411
  59. // All subsequent [X.Y.n] references are against n3793.
  60. // [X.Y.4]
  61. template<typename _Tp>
  62. class optional;
  63. // [X.Y.5]
  64. /// Tag type for in-place construction.
  65. struct in_place_t { };
  66. /// Tag for in-place construction.
  67. constexpr in_place_t in_place { };
  68. // [X.Y.6]
  69. /// Tag type to disengage optional objects.
  70. struct nullopt_t
  71. {
  72. // Do not user-declare default constructor at all for
  73. // optional_value = {} syntax to work.
  74. // nullopt_t() = delete;
  75. // Used for constructing nullopt.
  76. enum class _Construct { _Token };
  77. // Must be constexpr for nullopt_t to be literal.
  78. explicit constexpr nullopt_t(_Construct) { }
  79. };
  80. // [X.Y.6]
  81. /// Tag to disengage optional objects.
  82. constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
  83. // [X.Y.7]
  84. /**
  85. * @brief Exception class thrown when a disengaged optional object is
  86. * dereferenced.
  87. * @ingroup exceptions
  88. */
  89. class bad_optional_access : public logic_error
  90. {
  91. public:
  92. bad_optional_access() : logic_error("bad optional access") { }
  93. // XXX This constructor is non-standard. Should not be inline
  94. explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
  95. virtual ~bad_optional_access() noexcept = default;
  96. };
  97. void
  98. __throw_bad_optional_access(const char*)
  99. __attribute__((__noreturn__));
  100. // XXX Does not belong here.
  101. inline void
  102. __throw_bad_optional_access(const char* __s)
  103. { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
  104. template<typename _Tp, typename = void>
  105. struct _Has_addressof_mem : std::false_type { };
  106. template<typename _Tp>
  107. struct _Has_addressof_mem<_Tp,
  108. __void_t<decltype( std::declval<const _Tp&>().operator&() )>
  109. >
  110. : std::true_type { };
  111. template<typename _Tp, typename = void>
  112. struct _Has_addressof_free : std::false_type { };
  113. template<typename _Tp>
  114. struct _Has_addressof_free<_Tp,
  115. __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
  116. >
  117. : std::true_type { };
  118. /**
  119. * @brief Trait that detects the presence of an overloaded unary operator&.
  120. *
  121. * Practically speaking this detects the presence of such an operator when
  122. * called on a const-qualified lvalue (i.e.
  123. * declval<_Tp * const&>().operator&()).
  124. */
  125. template<typename _Tp>
  126. struct _Has_addressof
  127. : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
  128. { };
  129. /**
  130. * @brief An overload that attempts to take the address of an lvalue as a
  131. * constant expression. Falls back to __addressof in the presence of an
  132. * overloaded addressof operator (unary operator&), in which case the call
  133. * will not be a constant expression.
  134. */
  135. template<typename _Tp, enable_if_t<!_Has_addressof<_Tp>::value, int>...>
  136. constexpr _Tp* __constexpr_addressof(_Tp& __t)
  137. { return &__t; }
  138. /**
  139. * @brief Fallback overload that defers to __addressof.
  140. */
  141. template<typename _Tp, enable_if_t<_Has_addressof<_Tp>::value, int>...>
  142. inline _Tp* __constexpr_addressof(_Tp& __t)
  143. { return std::__addressof(__t); }
  144. /**
  145. * @brief Class template that holds the necessary state for @ref optional
  146. * and that has the responsibility for construction and the special members.
  147. *
  148. * Such a separate base class template is necessary in order to
  149. * conditionally enable the special members (e.g. copy/move constructors).
  150. * Note that this means that @ref _Optional_base implements the
  151. * functionality for copy and move assignment, but not for converting
  152. * assignment.
  153. *
  154. * @see optional, _Enable_special_members
  155. */
  156. template<typename _Tp, bool _ShouldProvideDestructor =
  157. !is_trivially_destructible<_Tp>::value>
  158. class _Optional_base
  159. {
  160. private:
  161. // Remove const to avoid prohibition of reusing object storage for
  162. // const-qualified types in [3.8/9]. This is strictly internal
  163. // and even optional itself is oblivious to it.
  164. using _Stored_type = remove_const_t<_Tp>;
  165. public:
  166. // [X.Y.4.1] Constructors.
  167. // Constructors for disengaged optionals.
  168. constexpr _Optional_base() noexcept
  169. : _M_empty{} { }
  170. constexpr _Optional_base(nullopt_t) noexcept
  171. : _Optional_base{} { }
  172. // Constructors for engaged optionals.
  173. constexpr _Optional_base(const _Tp& __t)
  174. : _M_payload(__t), _M_engaged(true) { }
  175. constexpr _Optional_base(_Tp&& __t)
  176. : _M_payload(std::move(__t)), _M_engaged(true) { }
  177. template<typename... _Args>
  178. constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
  179. : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
  180. template<typename _Up, typename... _Args,
  181. enable_if_t<is_constructible<_Tp,
  182. initializer_list<_Up>&,
  183. _Args&&...>::value,
  184. int>...>
  185. constexpr explicit _Optional_base(in_place_t,
  186. initializer_list<_Up> __il,
  187. _Args&&... __args)
  188. : _M_payload(__il, std::forward<_Args>(__args)...),
  189. _M_engaged(true) { }
  190. // Copy and move constructors.
  191. _Optional_base(const _Optional_base& __other)
  192. {
  193. if (__other._M_engaged)
  194. this->_M_construct(__other._M_get());
  195. }
  196. _Optional_base(_Optional_base&& __other)
  197. noexcept(is_nothrow_move_constructible<_Tp>())
  198. {
  199. if (__other._M_engaged)
  200. this->_M_construct(std::move(__other._M_get()));
  201. }
  202. // [X.Y.4.3] (partly) Assignment.
  203. _Optional_base&
  204. operator=(const _Optional_base& __other)
  205. {
  206. if (this->_M_engaged && __other._M_engaged)
  207. this->_M_get() = __other._M_get();
  208. else
  209. {
  210. if (__other._M_engaged)
  211. this->_M_construct(__other._M_get());
  212. else
  213. this->_M_reset();
  214. }
  215. return *this;
  216. }
  217. _Optional_base&
  218. operator=(_Optional_base&& __other)
  219. noexcept(__and_<is_nothrow_move_constructible<_Tp>,
  220. is_nothrow_move_assignable<_Tp>>())
  221. {
  222. if (this->_M_engaged && __other._M_engaged)
  223. this->_M_get() = std::move(__other._M_get());
  224. else
  225. {
  226. if (__other._M_engaged)
  227. this->_M_construct(std::move(__other._M_get()));
  228. else
  229. this->_M_reset();
  230. }
  231. return *this;
  232. }
  233. // [X.Y.4.2] Destructor.
  234. ~_Optional_base()
  235. {
  236. if (this->_M_engaged)
  237. this->_M_payload.~_Stored_type();
  238. }
  239. // The following functionality is also needed by optional, hence the
  240. // protected accessibility.
  241. protected:
  242. constexpr bool _M_is_engaged() const noexcept
  243. { return this->_M_engaged; }
  244. // The _M_get operations have _M_engaged as a precondition.
  245. constexpr _Tp&
  246. _M_get() noexcept
  247. { return _M_payload; }
  248. constexpr const _Tp&
  249. _M_get() const noexcept
  250. { return _M_payload; }
  251. // The _M_construct operation has !_M_engaged as a precondition
  252. // while _M_destruct has _M_engaged as a precondition.
  253. template<typename... _Args>
  254. void
  255. _M_construct(_Args&&... __args)
  256. noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
  257. {
  258. ::new (std::__addressof(this->_M_payload))
  259. _Stored_type(std::forward<_Args>(__args)...);
  260. this->_M_engaged = true;
  261. }
  262. void
  263. _M_destruct()
  264. {
  265. this->_M_engaged = false;
  266. this->_M_payload.~_Stored_type();
  267. }
  268. // _M_reset is a 'safe' operation with no precondition.
  269. void
  270. _M_reset()
  271. {
  272. if (this->_M_engaged)
  273. this->_M_destruct();
  274. }
  275. private:
  276. struct _Empty_byte { };
  277. union {
  278. _Empty_byte _M_empty;
  279. _Stored_type _M_payload;
  280. };
  281. bool _M_engaged = false;
  282. };
  283. /// Partial specialization that is exactly identical to the primary template
  284. /// save for not providing a destructor, to fulfill triviality requirements.
  285. template<typename _Tp>
  286. class _Optional_base<_Tp, false>
  287. {
  288. private:
  289. using _Stored_type = remove_const_t<_Tp>;
  290. public:
  291. constexpr _Optional_base() noexcept
  292. : _M_empty{} { }
  293. constexpr _Optional_base(nullopt_t) noexcept
  294. : _Optional_base{} { }
  295. constexpr _Optional_base(const _Tp& __t)
  296. : _M_payload(__t), _M_engaged(true) { }
  297. constexpr _Optional_base(_Tp&& __t)
  298. : _M_payload(std::move(__t)), _M_engaged(true) { }
  299. template<typename... _Args>
  300. constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
  301. : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
  302. template<typename _Up, typename... _Args,
  303. enable_if_t<is_constructible<_Tp,
  304. initializer_list<_Up>&,
  305. _Args&&...>::value,
  306. int>...>
  307. constexpr explicit _Optional_base(in_place_t,
  308. initializer_list<_Up> __il,
  309. _Args&&... __args)
  310. : _M_payload(__il, std::forward<_Args>(__args)...),
  311. _M_engaged(true) { }
  312. _Optional_base(const _Optional_base& __other)
  313. {
  314. if (__other._M_engaged)
  315. this->_M_construct(__other._M_get());
  316. }
  317. _Optional_base(_Optional_base&& __other)
  318. noexcept(is_nothrow_move_constructible<_Tp>())
  319. {
  320. if (__other._M_engaged)
  321. this->_M_construct(std::move(__other._M_get()));
  322. }
  323. _Optional_base&
  324. operator=(const _Optional_base& __other)
  325. {
  326. if (this->_M_engaged && __other._M_engaged)
  327. this->_M_get() = __other._M_get();
  328. else
  329. {
  330. if (__other._M_engaged)
  331. this->_M_construct(__other._M_get());
  332. else
  333. this->_M_reset();
  334. }
  335. return *this;
  336. }
  337. _Optional_base&
  338. operator=(_Optional_base&& __other)
  339. noexcept(__and_<is_nothrow_move_constructible<_Tp>,
  340. is_nothrow_move_assignable<_Tp>>())
  341. {
  342. if (this->_M_engaged && __other._M_engaged)
  343. this->_M_get() = std::move(__other._M_get());
  344. else
  345. {
  346. if (__other._M_engaged)
  347. this->_M_construct(std::move(__other._M_get()));
  348. else
  349. this->_M_reset();
  350. }
  351. return *this;
  352. }
  353. // Sole difference
  354. // ~_Optional_base() noexcept = default;
  355. protected:
  356. constexpr bool _M_is_engaged() const noexcept
  357. { return this->_M_engaged; }
  358. _Tp&
  359. _M_get() noexcept
  360. { return _M_payload; }
  361. constexpr const _Tp&
  362. _M_get() const noexcept
  363. { return _M_payload; }
  364. template<typename... _Args>
  365. void
  366. _M_construct(_Args&&... __args)
  367. noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
  368. {
  369. ::new (std::__addressof(this->_M_payload))
  370. _Stored_type(std::forward<_Args>(__args)...);
  371. this->_M_engaged = true;
  372. }
  373. void
  374. _M_destruct()
  375. {
  376. this->_M_engaged = false;
  377. this->_M_payload.~_Stored_type();
  378. }
  379. void
  380. _M_reset()
  381. {
  382. if (this->_M_engaged)
  383. this->_M_destruct();
  384. }
  385. private:
  386. struct _Empty_byte { };
  387. union
  388. {
  389. _Empty_byte _M_empty;
  390. _Stored_type _M_payload;
  391. };
  392. bool _M_engaged = false;
  393. };
  394. /**
  395. * @brief Class template for optional values.
  396. */
  397. template<typename _Tp>
  398. class optional
  399. : private _Optional_base<_Tp>,
  400. private _Enable_copy_move<
  401. // Copy constructor.
  402. is_copy_constructible<_Tp>::value,
  403. // Copy assignment.
  404. __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
  405. // Move constructor.
  406. is_move_constructible<_Tp>::value,
  407. // Move assignment.
  408. __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
  409. // Unique tag type.
  410. optional<_Tp>>
  411. {
  412. static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
  413. __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
  414. __not_<is_reference<_Tp>>>(),
  415. "Invalid instantiation of optional<T>");
  416. private:
  417. using _Base = _Optional_base<_Tp>;
  418. public:
  419. using value_type = _Tp;
  420. // _Optional_base has the responsibility for construction.
  421. using _Base::_Base;
  422. // [X.Y.4.3] (partly) Assignment.
  423. optional&
  424. operator=(nullopt_t) noexcept
  425. {
  426. this->_M_reset();
  427. return *this;
  428. }
  429. template<typename _Up>
  430. enable_if_t<is_same<_Tp, decay_t<_Up>>::value, optional&>
  431. operator=(_Up&& __u)
  432. {
  433. static_assert(__and_<is_constructible<_Tp, _Up>,
  434. is_assignable<_Tp&, _Up>>(),
  435. "Cannot assign to value type from argument");
  436. if (this->_M_is_engaged())
  437. this->_M_get() = std::forward<_Up>(__u);
  438. else
  439. this->_M_construct(std::forward<_Up>(__u));
  440. return *this;
  441. }
  442. template<typename... _Args>
  443. void
  444. emplace(_Args&&... __args)
  445. {
  446. static_assert(is_constructible<_Tp, _Args&&...>(),
  447. "Cannot emplace value type from arguments");
  448. this->_M_reset();
  449. this->_M_construct(std::forward<_Args>(__args)...);
  450. }
  451. template<typename _Up, typename... _Args>
  452. enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
  453. _Args&&...>::value>
  454. emplace(initializer_list<_Up> __il, _Args&&... __args)
  455. {
  456. this->_M_reset();
  457. this->_M_construct(__il, std::forward<_Args>(__args)...);
  458. }
  459. // [X.Y.4.2] Destructor is implicit, implemented in _Optional_base.
  460. // [X.Y.4.4] Swap.
  461. void
  462. swap(optional& __other)
  463. noexcept(is_nothrow_move_constructible<_Tp>()
  464. && noexcept(swap(declval<_Tp&>(), declval<_Tp&>())))
  465. {
  466. using std::swap;
  467. if (this->_M_is_engaged() && __other._M_is_engaged())
  468. swap(this->_M_get(), __other._M_get());
  469. else if (this->_M_is_engaged())
  470. {
  471. __other._M_construct(std::move(this->_M_get()));
  472. this->_M_destruct();
  473. }
  474. else if (__other._M_is_engaged())
  475. {
  476. this->_M_construct(std::move(__other._M_get()));
  477. __other._M_destruct();
  478. }
  479. }
  480. // [X.Y.4.5] Observers.
  481. constexpr const _Tp*
  482. operator->() const
  483. { return __constexpr_addressof(this->_M_get()); }
  484. _Tp*
  485. operator->()
  486. { return std::__addressof(this->_M_get()); }
  487. constexpr const _Tp&
  488. operator*() const&
  489. { return this->_M_get(); }
  490. constexpr _Tp&
  491. operator*()&
  492. { return this->_M_get(); }
  493. constexpr _Tp&&
  494. operator*()&&
  495. { return std::move(this->_M_get()); }
  496. constexpr const _Tp&&
  497. operator*() const&&
  498. { return std::move(this->_M_get()); }
  499. constexpr explicit operator bool() const noexcept
  500. { return this->_M_is_engaged(); }
  501. constexpr const _Tp&
  502. value() const&
  503. {
  504. return this->_M_is_engaged()
  505. ? this->_M_get()
  506. : (__throw_bad_optional_access("Attempt to access value of a "
  507. "disengaged optional object"),
  508. this->_M_get());
  509. }
  510. constexpr _Tp&
  511. value()&
  512. {
  513. return this->_M_is_engaged()
  514. ? this->_M_get()
  515. : (__throw_bad_optional_access("Attempt to access value of a "
  516. "disengaged optional object"),
  517. this->_M_get());
  518. }
  519. constexpr _Tp&&
  520. value()&&
  521. {
  522. return this->_M_is_engaged()
  523. ? std::move(this->_M_get())
  524. : (__throw_bad_optional_access("Attempt to access value of a "
  525. "disengaged optional object"),
  526. std::move(this->_M_get()));
  527. }
  528. constexpr const _Tp&&
  529. value() const&&
  530. {
  531. return this->_M_is_engaged()
  532. ? std::move(this->_M_get())
  533. : (__throw_bad_optional_access("Attempt to access value of a "
  534. "disengaged optional object"),
  535. std::move(this->_M_get()));
  536. }
  537. template<typename _Up>
  538. constexpr _Tp
  539. value_or(_Up&& __u) const&
  540. {
  541. static_assert(__and_<is_copy_constructible<_Tp>,
  542. is_convertible<_Up&&, _Tp>>(),
  543. "Cannot return value");
  544. return this->_M_is_engaged()
  545. ? this->_M_get()
  546. : static_cast<_Tp>(std::forward<_Up>(__u));
  547. }
  548. template<typename _Up>
  549. _Tp
  550. value_or(_Up&& __u) &&
  551. {
  552. static_assert(__and_<is_move_constructible<_Tp>,
  553. is_convertible<_Up&&, _Tp>>(),
  554. "Cannot return value" );
  555. return this->_M_is_engaged()
  556. ? std::move(this->_M_get())
  557. : static_cast<_Tp>(std::forward<_Up>(__u));
  558. }
  559. };
  560. // [X.Y.8] Comparisons between optional values.
  561. template<typename _Tp>
  562. constexpr bool
  563. operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
  564. {
  565. return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
  566. && (!__lhs || *__lhs == *__rhs);
  567. }
  568. template<typename _Tp>
  569. constexpr bool
  570. operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
  571. { return !(__lhs == __rhs); }
  572. template<typename _Tp>
  573. constexpr bool
  574. operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
  575. {
  576. return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
  577. }
  578. template<typename _Tp>
  579. constexpr bool
  580. operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
  581. { return __rhs < __lhs; }
  582. template<typename _Tp>
  583. constexpr bool
  584. operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
  585. { return !(__rhs < __lhs); }
  586. template<typename _Tp>
  587. constexpr bool
  588. operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
  589. { return !(__lhs < __rhs); }
  590. // [X.Y.9] Comparisons with nullopt.
  591. template<typename _Tp>
  592. constexpr bool
  593. operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
  594. { return !__lhs; }
  595. template<typename _Tp>
  596. constexpr bool
  597. operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
  598. { return !__rhs; }
  599. template<typename _Tp>
  600. constexpr bool
  601. operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
  602. { return static_cast<bool>(__lhs); }
  603. template<typename _Tp>
  604. constexpr bool
  605. operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
  606. { return static_cast<bool>(__rhs); }
  607. template<typename _Tp>
  608. constexpr bool
  609. operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
  610. { return false; }
  611. template<typename _Tp>
  612. constexpr bool
  613. operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
  614. { return static_cast<bool>(__rhs); }
  615. template<typename _Tp>
  616. constexpr bool
  617. operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
  618. { return static_cast<bool>(__lhs); }
  619. template<typename _Tp>
  620. constexpr bool
  621. operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
  622. { return false; }
  623. template<typename _Tp>
  624. constexpr bool
  625. operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
  626. { return !__lhs; }
  627. template<typename _Tp>
  628. constexpr bool
  629. operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
  630. { return true; }
  631. template<typename _Tp>
  632. constexpr bool
  633. operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
  634. { return true; }
  635. template<typename _Tp>
  636. constexpr bool
  637. operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
  638. { return !__rhs; }
  639. // [X.Y.10] Comparisons with value type.
  640. template<typename _Tp>
  641. constexpr bool
  642. operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
  643. { return __lhs && *__lhs == __rhs; }
  644. template<typename _Tp>
  645. constexpr bool
  646. operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
  647. { return __rhs && __lhs == *__rhs; }
  648. template<typename _Tp>
  649. constexpr bool
  650. operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
  651. { return !__lhs || !(*__lhs == __rhs); }
  652. template<typename _Tp>
  653. constexpr bool
  654. operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
  655. { return !__rhs || !(__lhs == *__rhs); }
  656. template<typename _Tp>
  657. constexpr bool
  658. operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
  659. { return !__lhs || *__lhs < __rhs; }
  660. template<typename _Tp>
  661. constexpr bool
  662. operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
  663. { return __rhs && __lhs < *__rhs; }
  664. template<typename _Tp>
  665. constexpr bool
  666. operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
  667. { return __lhs && __rhs < *__lhs; }
  668. template<typename _Tp>
  669. constexpr bool
  670. operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
  671. { return !__rhs || *__rhs < __lhs; }
  672. template<typename _Tp>
  673. constexpr bool
  674. operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
  675. { return !__lhs || !(__rhs < *__lhs); }
  676. template<typename _Tp>
  677. constexpr bool
  678. operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
  679. { return __rhs && !(*__rhs < __lhs); }
  680. template<typename _Tp>
  681. constexpr bool
  682. operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
  683. { return __lhs && !(*__lhs < __rhs); }
  684. template<typename _Tp>
  685. constexpr bool
  686. operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
  687. { return !__rhs || !(__lhs < *__rhs); }
  688. // [X.Y.11]
  689. template<typename _Tp>
  690. inline void
  691. swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
  692. noexcept(noexcept(__lhs.swap(__rhs)))
  693. { __lhs.swap(__rhs); }
  694. template<typename _Tp>
  695. constexpr optional<decay_t<_Tp>>
  696. make_optional(_Tp&& __t)
  697. { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
  698. // @} group optional
  699. _GLIBCXX_END_NAMESPACE_VERSION
  700. } // namespace fundamentals_v1
  701. }
  702. // [X.Y.12]
  703. template<typename _Tp>
  704. struct hash<experimental::optional<_Tp>>
  705. {
  706. using result_type = size_t;
  707. using argument_type = experimental::optional<_Tp>;
  708. size_t
  709. operator()(const experimental::optional<_Tp>& __t) const
  710. noexcept(noexcept(hash<_Tp> {}(*__t)))
  711. {
  712. // We pick an arbitrary hash for disengaged optionals which hopefully
  713. // usual values of _Tp won't typically hash to.
  714. constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
  715. return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
  716. }
  717. };
  718. }
  719. #endif // C++14
  720. #endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL