optional 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. // <optional> -*- C++ -*-
  2. // Copyright (C) 2013-2021 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 include/optional
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_OPTIONAL
  24. #define _GLIBCXX_OPTIONAL 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201703L
  27. #include <utility>
  28. #include <type_traits>
  29. #include <exception>
  30. #include <new>
  31. #include <initializer_list>
  32. #include <bits/exception_defines.h>
  33. #include <bits/functional_hash.h>
  34. #include <bits/enable_special_members.h>
  35. #if __cplusplus > 201703L
  36. # include <compare>
  37. #endif
  38. namespace std _GLIBCXX_VISIBILITY(default)
  39. {
  40. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  41. /**
  42. * @addtogroup utilities
  43. * @{
  44. */
  45. #define __cpp_lib_optional 201606L
  46. template<typename _Tp>
  47. class optional;
  48. /// Tag type to disengage optional objects.
  49. struct nullopt_t
  50. {
  51. // Do not user-declare default constructor at all for
  52. // optional_value = {} syntax to work.
  53. // nullopt_t() = delete;
  54. // Used for constructing nullopt.
  55. enum class _Construct { _Token };
  56. // Must be constexpr for nullopt_t to be literal.
  57. explicit constexpr nullopt_t(_Construct) { }
  58. };
  59. /// Tag to disengage optional objects.
  60. inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
  61. /**
  62. * @brief Exception class thrown when a disengaged optional object is
  63. * dereferenced.
  64. * @ingroup exceptions
  65. */
  66. class bad_optional_access : public exception
  67. {
  68. public:
  69. bad_optional_access() = default;
  70. virtual ~bad_optional_access() = default;
  71. const char* what() const noexcept override
  72. { return "bad optional access"; }
  73. };
  74. void
  75. __throw_bad_optional_access()
  76. __attribute__((__noreturn__));
  77. // XXX Does not belong here.
  78. inline void
  79. __throw_bad_optional_access()
  80. { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
  81. // This class template manages construction/destruction of
  82. // the contained value for a std::optional.
  83. template <typename _Tp>
  84. struct _Optional_payload_base
  85. {
  86. using _Stored_type = remove_const_t<_Tp>;
  87. _Optional_payload_base() = default;
  88. ~_Optional_payload_base() = default;
  89. template<typename... _Args>
  90. constexpr
  91. _Optional_payload_base(in_place_t __tag, _Args&&... __args)
  92. : _M_payload(__tag, std::forward<_Args>(__args)...),
  93. _M_engaged(true)
  94. { }
  95. template<typename _Up, typename... _Args>
  96. constexpr
  97. _Optional_payload_base(std::initializer_list<_Up> __il,
  98. _Args&&... __args)
  99. : _M_payload(__il, std::forward<_Args>(__args)...),
  100. _M_engaged(true)
  101. { }
  102. // Constructor used by _Optional_base copy constructor when the
  103. // contained value is not trivially copy constructible.
  104. constexpr
  105. _Optional_payload_base(bool __engaged,
  106. const _Optional_payload_base& __other)
  107. {
  108. if (__other._M_engaged)
  109. this->_M_construct(__other._M_get());
  110. }
  111. // Constructor used by _Optional_base move constructor when the
  112. // contained value is not trivially move constructible.
  113. constexpr
  114. _Optional_payload_base(bool __engaged,
  115. _Optional_payload_base&& __other)
  116. {
  117. if (__other._M_engaged)
  118. this->_M_construct(std::move(__other._M_get()));
  119. }
  120. // Copy constructor is only used to when the contained value is
  121. // trivially copy constructible.
  122. _Optional_payload_base(const _Optional_payload_base&) = default;
  123. // Move constructor is only used to when the contained value is
  124. // trivially copy constructible.
  125. _Optional_payload_base(_Optional_payload_base&&) = default;
  126. _Optional_payload_base&
  127. operator=(const _Optional_payload_base&) = default;
  128. _Optional_payload_base&
  129. operator=(_Optional_payload_base&&) = default;
  130. // used to perform non-trivial copy assignment.
  131. constexpr void
  132. _M_copy_assign(const _Optional_payload_base& __other)
  133. {
  134. if (this->_M_engaged && __other._M_engaged)
  135. this->_M_get() = __other._M_get();
  136. else
  137. {
  138. if (__other._M_engaged)
  139. this->_M_construct(__other._M_get());
  140. else
  141. this->_M_reset();
  142. }
  143. }
  144. // used to perform non-trivial move assignment.
  145. constexpr void
  146. _M_move_assign(_Optional_payload_base&& __other)
  147. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  148. is_nothrow_move_assignable<_Tp>>)
  149. {
  150. if (this->_M_engaged && __other._M_engaged)
  151. this->_M_get() = std::move(__other._M_get());
  152. else
  153. {
  154. if (__other._M_engaged)
  155. this->_M_construct(std::move(__other._M_get()));
  156. else
  157. this->_M_reset();
  158. }
  159. }
  160. struct _Empty_byte { };
  161. template<typename _Up, bool = is_trivially_destructible_v<_Up>>
  162. union _Storage
  163. {
  164. constexpr _Storage() noexcept : _M_empty() { }
  165. template<typename... _Args>
  166. constexpr
  167. _Storage(in_place_t, _Args&&... __args)
  168. : _M_value(std::forward<_Args>(__args)...)
  169. { }
  170. template<typename _Vp, typename... _Args>
  171. constexpr
  172. _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
  173. : _M_value(__il, std::forward<_Args>(__args)...)
  174. { }
  175. _Empty_byte _M_empty;
  176. _Up _M_value;
  177. };
  178. template<typename _Up>
  179. union _Storage<_Up, false>
  180. {
  181. constexpr _Storage() noexcept : _M_empty() { }
  182. template<typename... _Args>
  183. constexpr
  184. _Storage(in_place_t, _Args&&... __args)
  185. : _M_value(std::forward<_Args>(__args)...)
  186. { }
  187. template<typename _Vp, typename... _Args>
  188. constexpr
  189. _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
  190. : _M_value(__il, std::forward<_Args>(__args)...)
  191. { }
  192. // User-provided destructor is needed when _Up has non-trivial dtor.
  193. ~_Storage() { }
  194. _Empty_byte _M_empty;
  195. _Up _M_value;
  196. };
  197. _Storage<_Stored_type> _M_payload;
  198. bool _M_engaged = false;
  199. template<typename... _Args>
  200. void
  201. _M_construct(_Args&&... __args)
  202. noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
  203. {
  204. ::new ((void *) std::__addressof(this->_M_payload))
  205. _Stored_type(std::forward<_Args>(__args)...);
  206. this->_M_engaged = true;
  207. }
  208. constexpr void
  209. _M_destroy() noexcept
  210. {
  211. _M_engaged = false;
  212. _M_payload._M_value.~_Stored_type();
  213. }
  214. // The _M_get() operations have _M_engaged as a precondition.
  215. // They exist to access the contained value with the appropriate
  216. // const-qualification, because _M_payload has had the const removed.
  217. constexpr _Tp&
  218. _M_get() noexcept
  219. { return this->_M_payload._M_value; }
  220. constexpr const _Tp&
  221. _M_get() const noexcept
  222. { return this->_M_payload._M_value; }
  223. // _M_reset is a 'safe' operation with no precondition.
  224. constexpr void
  225. _M_reset() noexcept
  226. {
  227. if (this->_M_engaged)
  228. _M_destroy();
  229. }
  230. };
  231. // Class template that manages the payload for optionals.
  232. template <typename _Tp,
  233. bool /*_HasTrivialDestructor*/ =
  234. is_trivially_destructible_v<_Tp>,
  235. bool /*_HasTrivialCopy */ =
  236. is_trivially_copy_assignable_v<_Tp>
  237. && is_trivially_copy_constructible_v<_Tp>,
  238. bool /*_HasTrivialMove */ =
  239. is_trivially_move_assignable_v<_Tp>
  240. && is_trivially_move_constructible_v<_Tp>>
  241. struct _Optional_payload;
  242. // Payload for potentially-constexpr optionals (trivial copy/move/destroy).
  243. template <typename _Tp>
  244. struct _Optional_payload<_Tp, true, true, true>
  245. : _Optional_payload_base<_Tp>
  246. {
  247. using _Optional_payload_base<_Tp>::_Optional_payload_base;
  248. _Optional_payload() = default;
  249. };
  250. // Payload for optionals with non-trivial copy construction/assignment.
  251. template <typename _Tp>
  252. struct _Optional_payload<_Tp, true, false, true>
  253. : _Optional_payload_base<_Tp>
  254. {
  255. using _Optional_payload_base<_Tp>::_Optional_payload_base;
  256. _Optional_payload() = default;
  257. ~_Optional_payload() = default;
  258. _Optional_payload(const _Optional_payload&) = default;
  259. _Optional_payload(_Optional_payload&&) = default;
  260. _Optional_payload& operator=(_Optional_payload&&) = default;
  261. // Non-trivial copy assignment.
  262. constexpr
  263. _Optional_payload&
  264. operator=(const _Optional_payload& __other)
  265. {
  266. this->_M_copy_assign(__other);
  267. return *this;
  268. }
  269. };
  270. // Payload for optionals with non-trivial move construction/assignment.
  271. template <typename _Tp>
  272. struct _Optional_payload<_Tp, true, true, false>
  273. : _Optional_payload_base<_Tp>
  274. {
  275. using _Optional_payload_base<_Tp>::_Optional_payload_base;
  276. _Optional_payload() = default;
  277. ~_Optional_payload() = default;
  278. _Optional_payload(const _Optional_payload&) = default;
  279. _Optional_payload(_Optional_payload&&) = default;
  280. _Optional_payload& operator=(const _Optional_payload&) = default;
  281. // Non-trivial move assignment.
  282. constexpr
  283. _Optional_payload&
  284. operator=(_Optional_payload&& __other)
  285. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  286. is_nothrow_move_assignable<_Tp>>)
  287. {
  288. this->_M_move_assign(std::move(__other));
  289. return *this;
  290. }
  291. };
  292. // Payload for optionals with non-trivial copy and move assignment.
  293. template <typename _Tp>
  294. struct _Optional_payload<_Tp, true, false, false>
  295. : _Optional_payload_base<_Tp>
  296. {
  297. using _Optional_payload_base<_Tp>::_Optional_payload_base;
  298. _Optional_payload() = default;
  299. ~_Optional_payload() = default;
  300. _Optional_payload(const _Optional_payload&) = default;
  301. _Optional_payload(_Optional_payload&&) = default;
  302. // Non-trivial copy assignment.
  303. constexpr
  304. _Optional_payload&
  305. operator=(const _Optional_payload& __other)
  306. {
  307. this->_M_copy_assign(__other);
  308. return *this;
  309. }
  310. // Non-trivial move assignment.
  311. constexpr
  312. _Optional_payload&
  313. operator=(_Optional_payload&& __other)
  314. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  315. is_nothrow_move_assignable<_Tp>>)
  316. {
  317. this->_M_move_assign(std::move(__other));
  318. return *this;
  319. }
  320. };
  321. // Payload for optionals with non-trivial destructors.
  322. template <typename _Tp, bool _Copy, bool _Move>
  323. struct _Optional_payload<_Tp, false, _Copy, _Move>
  324. : _Optional_payload<_Tp, true, false, false>
  325. {
  326. // Base class implements all the constructors and assignment operators:
  327. using _Optional_payload<_Tp, true, false, false>::_Optional_payload;
  328. _Optional_payload() = default;
  329. _Optional_payload(const _Optional_payload&) = default;
  330. _Optional_payload(_Optional_payload&&) = default;
  331. _Optional_payload& operator=(const _Optional_payload&) = default;
  332. _Optional_payload& operator=(_Optional_payload&&) = default;
  333. // Destructor needs to destroy the contained value:
  334. ~_Optional_payload() { this->_M_reset(); }
  335. };
  336. // Common base class for _Optional_base<T> to avoid repeating these
  337. // member functions in each specialization.
  338. template<typename _Tp, typename _Dp>
  339. class _Optional_base_impl
  340. {
  341. protected:
  342. using _Stored_type = remove_const_t<_Tp>;
  343. // The _M_construct operation has !_M_engaged as a precondition
  344. // while _M_destruct has _M_engaged as a precondition.
  345. template<typename... _Args>
  346. void
  347. _M_construct(_Args&&... __args)
  348. noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
  349. {
  350. ::new
  351. (std::__addressof(static_cast<_Dp*>(this)->_M_payload._M_payload))
  352. _Stored_type(std::forward<_Args>(__args)...);
  353. static_cast<_Dp*>(this)->_M_payload._M_engaged = true;
  354. }
  355. void
  356. _M_destruct() noexcept
  357. { static_cast<_Dp*>(this)->_M_payload._M_destroy(); }
  358. // _M_reset is a 'safe' operation with no precondition.
  359. constexpr void
  360. _M_reset() noexcept
  361. { static_cast<_Dp*>(this)->_M_payload._M_reset(); }
  362. constexpr bool _M_is_engaged() const noexcept
  363. { return static_cast<const _Dp*>(this)->_M_payload._M_engaged; }
  364. // The _M_get operations have _M_engaged as a precondition.
  365. constexpr _Tp&
  366. _M_get() noexcept
  367. {
  368. __glibcxx_assert(this->_M_is_engaged());
  369. return static_cast<_Dp*>(this)->_M_payload._M_get();
  370. }
  371. constexpr const _Tp&
  372. _M_get() const noexcept
  373. {
  374. __glibcxx_assert(this->_M_is_engaged());
  375. return static_cast<const _Dp*>(this)->_M_payload._M_get();
  376. }
  377. };
  378. /**
  379. * @brief Class template that provides copy/move constructors of optional.
  380. *
  381. * Such a separate base class template is necessary in order to
  382. * conditionally make copy/move constructors trivial.
  383. *
  384. * When the contained value is trivially copy/move constructible,
  385. * the copy/move constructors of _Optional_base will invoke the
  386. * trivial copy/move constructor of _Optional_payload. Otherwise,
  387. * they will invoke _Optional_payload(bool, const _Optional_payload&)
  388. * or _Optional_payload(bool, _Optional_payload&&) to initialize
  389. * the contained value, if copying/moving an engaged optional.
  390. *
  391. * Whether the other special members are trivial is determined by the
  392. * _Optional_payload<_Tp> specialization used for the _M_payload member.
  393. *
  394. * @see optional, _Enable_special_members
  395. */
  396. template<typename _Tp,
  397. bool = is_trivially_copy_constructible_v<_Tp>,
  398. bool = is_trivially_move_constructible_v<_Tp>>
  399. struct _Optional_base
  400. : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
  401. {
  402. // Constructors for disengaged optionals.
  403. constexpr _Optional_base() = default;
  404. // Constructors for engaged optionals.
  405. template<typename... _Args,
  406. enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
  407. constexpr explicit
  408. _Optional_base(in_place_t, _Args&&... __args)
  409. : _M_payload(in_place, std::forward<_Args>(__args)...)
  410. { }
  411. template<typename _Up, typename... _Args,
  412. enable_if_t<is_constructible_v<_Tp,
  413. initializer_list<_Up>&,
  414. _Args...>, bool> = false>
  415. constexpr explicit
  416. _Optional_base(in_place_t,
  417. initializer_list<_Up> __il,
  418. _Args&&... __args)
  419. : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
  420. { }
  421. // Copy and move constructors.
  422. constexpr
  423. _Optional_base(const _Optional_base& __other)
  424. : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
  425. { }
  426. constexpr
  427. _Optional_base(_Optional_base&& __other)
  428. noexcept(is_nothrow_move_constructible_v<_Tp>)
  429. : _M_payload(__other._M_payload._M_engaged,
  430. std::move(__other._M_payload))
  431. { }
  432. // Assignment operators.
  433. _Optional_base& operator=(const _Optional_base&) = default;
  434. _Optional_base& operator=(_Optional_base&&) = default;
  435. _Optional_payload<_Tp> _M_payload;
  436. };
  437. template<typename _Tp>
  438. struct _Optional_base<_Tp, false, true>
  439. : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
  440. {
  441. // Constructors for disengaged optionals.
  442. constexpr _Optional_base() = default;
  443. // Constructors for engaged optionals.
  444. template<typename... _Args,
  445. enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
  446. constexpr explicit
  447. _Optional_base(in_place_t, _Args&&... __args)
  448. : _M_payload(in_place, std::forward<_Args>(__args)...)
  449. { }
  450. template<typename _Up, typename... _Args,
  451. enable_if_t<is_constructible_v<_Tp,
  452. initializer_list<_Up>&,
  453. _Args...>, bool> = false>
  454. constexpr explicit
  455. _Optional_base(in_place_t,
  456. initializer_list<_Up> __il,
  457. _Args... __args)
  458. : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
  459. { }
  460. // Copy and move constructors.
  461. constexpr _Optional_base(const _Optional_base& __other)
  462. : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
  463. { }
  464. constexpr _Optional_base(_Optional_base&& __other) = default;
  465. // Assignment operators.
  466. _Optional_base& operator=(const _Optional_base&) = default;
  467. _Optional_base& operator=(_Optional_base&&) = default;
  468. _Optional_payload<_Tp> _M_payload;
  469. };
  470. template<typename _Tp>
  471. struct _Optional_base<_Tp, true, false>
  472. : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
  473. {
  474. // Constructors for disengaged optionals.
  475. constexpr _Optional_base() = default;
  476. // Constructors for engaged optionals.
  477. template<typename... _Args,
  478. enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
  479. constexpr explicit
  480. _Optional_base(in_place_t, _Args&&... __args)
  481. : _M_payload(in_place, std::forward<_Args>(__args)...)
  482. { }
  483. template<typename _Up, typename... _Args,
  484. enable_if_t<is_constructible_v<_Tp,
  485. initializer_list<_Up>&,
  486. _Args...>, bool> = false>
  487. constexpr explicit
  488. _Optional_base(in_place_t,
  489. initializer_list<_Up> __il,
  490. _Args&&... __args)
  491. : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
  492. { }
  493. // Copy and move constructors.
  494. constexpr _Optional_base(const _Optional_base& __other) = default;
  495. constexpr
  496. _Optional_base(_Optional_base&& __other)
  497. noexcept(is_nothrow_move_constructible_v<_Tp>)
  498. : _M_payload(__other._M_payload._M_engaged,
  499. std::move(__other._M_payload))
  500. { }
  501. // Assignment operators.
  502. _Optional_base& operator=(const _Optional_base&) = default;
  503. _Optional_base& operator=(_Optional_base&&) = default;
  504. _Optional_payload<_Tp> _M_payload;
  505. };
  506. template<typename _Tp>
  507. struct _Optional_base<_Tp, true, true>
  508. : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
  509. {
  510. // Constructors for disengaged optionals.
  511. constexpr _Optional_base() = default;
  512. // Constructors for engaged optionals.
  513. template<typename... _Args,
  514. enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
  515. constexpr explicit
  516. _Optional_base(in_place_t, _Args&&... __args)
  517. : _M_payload(in_place, std::forward<_Args>(__args)...)
  518. { }
  519. template<typename _Up, typename... _Args,
  520. enable_if_t<is_constructible_v<_Tp,
  521. initializer_list<_Up>&,
  522. _Args...>, bool> = false>
  523. constexpr explicit
  524. _Optional_base(in_place_t,
  525. initializer_list<_Up> __il,
  526. _Args&&... __args)
  527. : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
  528. { }
  529. // Copy and move constructors.
  530. constexpr _Optional_base(const _Optional_base& __other) = default;
  531. constexpr _Optional_base(_Optional_base&& __other) = default;
  532. // Assignment operators.
  533. _Optional_base& operator=(const _Optional_base&) = default;
  534. _Optional_base& operator=(_Optional_base&&) = default;
  535. _Optional_payload<_Tp> _M_payload;
  536. };
  537. template<typename _Tp>
  538. class optional;
  539. template<typename _Tp, typename _Up>
  540. using __converts_from_optional =
  541. __or_<is_constructible<_Tp, const optional<_Up>&>,
  542. is_constructible<_Tp, optional<_Up>&>,
  543. is_constructible<_Tp, const optional<_Up>&&>,
  544. is_constructible<_Tp, optional<_Up>&&>,
  545. is_convertible<const optional<_Up>&, _Tp>,
  546. is_convertible<optional<_Up>&, _Tp>,
  547. is_convertible<const optional<_Up>&&, _Tp>,
  548. is_convertible<optional<_Up>&&, _Tp>>;
  549. template<typename _Tp, typename _Up>
  550. using __assigns_from_optional =
  551. __or_<is_assignable<_Tp&, const optional<_Up>&>,
  552. is_assignable<_Tp&, optional<_Up>&>,
  553. is_assignable<_Tp&, const optional<_Up>&&>,
  554. is_assignable<_Tp&, optional<_Up>&&>>;
  555. /**
  556. * @brief Class template for optional values.
  557. */
  558. template<typename _Tp>
  559. class optional
  560. : private _Optional_base<_Tp>,
  561. private _Enable_copy_move<
  562. // Copy constructor.
  563. is_copy_constructible_v<_Tp>,
  564. // Copy assignment.
  565. __and_v<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>,
  566. // Move constructor.
  567. is_move_constructible_v<_Tp>,
  568. // Move assignment.
  569. __and_v<is_move_constructible<_Tp>, is_move_assignable<_Tp>>,
  570. // Unique tag type.
  571. optional<_Tp>>
  572. {
  573. static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
  574. static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
  575. static_assert(!is_reference_v<_Tp>);
  576. private:
  577. using _Base = _Optional_base<_Tp>;
  578. // SFINAE helpers
  579. template<typename _Up>
  580. using __not_self = __not_<is_same<optional, __remove_cvref_t<_Up>>>;
  581. template<typename _Up>
  582. using __not_tag = __not_<is_same<in_place_t, __remove_cvref_t<_Up>>>;
  583. template<typename... _Cond>
  584. using _Requires = enable_if_t<__and_v<_Cond...>, bool>;
  585. public:
  586. using value_type = _Tp;
  587. constexpr optional() noexcept { }
  588. constexpr optional(nullopt_t) noexcept { }
  589. // Converting constructors for engaged optionals.
  590. template<typename _Up = _Tp,
  591. _Requires<__not_self<_Up>, __not_tag<_Up>,
  592. is_constructible<_Tp, _Up>,
  593. is_convertible<_Up, _Tp>> = true>
  594. constexpr
  595. optional(_Up&& __t)
  596. noexcept(is_nothrow_constructible_v<_Tp, _Up>)
  597. : _Base(std::in_place, std::forward<_Up>(__t)) { }
  598. template<typename _Up = _Tp,
  599. _Requires<__not_self<_Up>, __not_tag<_Up>,
  600. is_constructible<_Tp, _Up>,
  601. __not_<is_convertible<_Up, _Tp>>> = false>
  602. explicit constexpr
  603. optional(_Up&& __t)
  604. noexcept(is_nothrow_constructible_v<_Tp, _Up>)
  605. : _Base(std::in_place, std::forward<_Up>(__t)) { }
  606. template<typename _Up,
  607. _Requires<__not_<is_same<_Tp, _Up>>,
  608. is_constructible<_Tp, const _Up&>,
  609. is_convertible<const _Up&, _Tp>,
  610. __not_<__converts_from_optional<_Tp, _Up>>> = true>
  611. constexpr
  612. optional(const optional<_Up>& __t)
  613. noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
  614. {
  615. if (__t)
  616. emplace(*__t);
  617. }
  618. template<typename _Up,
  619. _Requires<__not_<is_same<_Tp, _Up>>,
  620. is_constructible<_Tp, const _Up&>,
  621. __not_<is_convertible<const _Up&, _Tp>>,
  622. __not_<__converts_from_optional<_Tp, _Up>>> = false>
  623. explicit constexpr
  624. optional(const optional<_Up>& __t)
  625. noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
  626. {
  627. if (__t)
  628. emplace(*__t);
  629. }
  630. template<typename _Up,
  631. _Requires<__not_<is_same<_Tp, _Up>>,
  632. is_constructible<_Tp, _Up>,
  633. is_convertible<_Up, _Tp>,
  634. __not_<__converts_from_optional<_Tp, _Up>>> = true>
  635. constexpr
  636. optional(optional<_Up>&& __t)
  637. noexcept(is_nothrow_constructible_v<_Tp, _Up>)
  638. {
  639. if (__t)
  640. emplace(std::move(*__t));
  641. }
  642. template<typename _Up,
  643. _Requires<__not_<is_same<_Tp, _Up>>,
  644. is_constructible<_Tp, _Up>,
  645. __not_<is_convertible<_Up, _Tp>>,
  646. __not_<__converts_from_optional<_Tp, _Up>>> = false>
  647. explicit constexpr
  648. optional(optional<_Up>&& __t)
  649. noexcept(is_nothrow_constructible_v<_Tp, _Up>)
  650. {
  651. if (__t)
  652. emplace(std::move(*__t));
  653. }
  654. template<typename... _Args,
  655. _Requires<is_constructible<_Tp, _Args...>> = false>
  656. explicit constexpr
  657. optional(in_place_t, _Args&&... __args)
  658. noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
  659. : _Base(std::in_place, std::forward<_Args>(__args)...) { }
  660. template<typename _Up, typename... _Args,
  661. _Requires<is_constructible<_Tp,
  662. initializer_list<_Up>&,
  663. _Args...>> = false>
  664. explicit constexpr
  665. optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
  666. noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
  667. _Args...>)
  668. : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
  669. // Assignment operators.
  670. optional&
  671. operator=(nullopt_t) noexcept
  672. {
  673. this->_M_reset();
  674. return *this;
  675. }
  676. template<typename _Up = _Tp>
  677. enable_if_t<__and_v<__not_self<_Up>,
  678. __not_<__and_<is_scalar<_Tp>,
  679. is_same<_Tp, decay_t<_Up>>>>,
  680. is_constructible<_Tp, _Up>,
  681. is_assignable<_Tp&, _Up>>,
  682. optional&>
  683. operator=(_Up&& __u)
  684. noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
  685. is_nothrow_assignable<_Tp&, _Up>>)
  686. {
  687. if (this->_M_is_engaged())
  688. this->_M_get() = std::forward<_Up>(__u);
  689. else
  690. this->_M_construct(std::forward<_Up>(__u));
  691. return *this;
  692. }
  693. template<typename _Up>
  694. enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
  695. is_constructible<_Tp, const _Up&>,
  696. is_assignable<_Tp&, _Up>,
  697. __not_<__converts_from_optional<_Tp, _Up>>,
  698. __not_<__assigns_from_optional<_Tp, _Up>>>,
  699. optional&>
  700. operator=(const optional<_Up>& __u)
  701. noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
  702. is_nothrow_assignable<_Tp&, const _Up&>>)
  703. {
  704. if (__u)
  705. {
  706. if (this->_M_is_engaged())
  707. this->_M_get() = *__u;
  708. else
  709. this->_M_construct(*__u);
  710. }
  711. else
  712. {
  713. this->_M_reset();
  714. }
  715. return *this;
  716. }
  717. template<typename _Up>
  718. enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
  719. is_constructible<_Tp, _Up>,
  720. is_assignable<_Tp&, _Up>,
  721. __not_<__converts_from_optional<_Tp, _Up>>,
  722. __not_<__assigns_from_optional<_Tp, _Up>>>,
  723. optional&>
  724. operator=(optional<_Up>&& __u)
  725. noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
  726. is_nothrow_assignable<_Tp&, _Up>>)
  727. {
  728. if (__u)
  729. {
  730. if (this->_M_is_engaged())
  731. this->_M_get() = std::move(*__u);
  732. else
  733. this->_M_construct(std::move(*__u));
  734. }
  735. else
  736. {
  737. this->_M_reset();
  738. }
  739. return *this;
  740. }
  741. template<typename... _Args>
  742. enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
  743. emplace(_Args&&... __args)
  744. noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
  745. {
  746. this->_M_reset();
  747. this->_M_construct(std::forward<_Args>(__args)...);
  748. return this->_M_get();
  749. }
  750. template<typename _Up, typename... _Args>
  751. enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
  752. _Tp&>
  753. emplace(initializer_list<_Up> __il, _Args&&... __args)
  754. noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
  755. _Args...>)
  756. {
  757. this->_M_reset();
  758. this->_M_construct(__il, std::forward<_Args>(__args)...);
  759. return this->_M_get();
  760. }
  761. // Destructor is implicit, implemented in _Optional_base.
  762. // Swap.
  763. void
  764. swap(optional& __other)
  765. noexcept(is_nothrow_move_constructible_v<_Tp>
  766. && is_nothrow_swappable_v<_Tp>)
  767. {
  768. using std::swap;
  769. if (this->_M_is_engaged() && __other._M_is_engaged())
  770. swap(this->_M_get(), __other._M_get());
  771. else if (this->_M_is_engaged())
  772. {
  773. __other._M_construct(std::move(this->_M_get()));
  774. this->_M_destruct();
  775. }
  776. else if (__other._M_is_engaged())
  777. {
  778. this->_M_construct(std::move(__other._M_get()));
  779. __other._M_destruct();
  780. }
  781. }
  782. // Observers.
  783. constexpr const _Tp*
  784. operator->() const noexcept
  785. { return std::__addressof(this->_M_get()); }
  786. constexpr _Tp*
  787. operator->() noexcept
  788. { return std::__addressof(this->_M_get()); }
  789. constexpr const _Tp&
  790. operator*() const& noexcept
  791. { return this->_M_get(); }
  792. constexpr _Tp&
  793. operator*()& noexcept
  794. { return this->_M_get(); }
  795. constexpr _Tp&&
  796. operator*()&& noexcept
  797. { return std::move(this->_M_get()); }
  798. constexpr const _Tp&&
  799. operator*() const&& noexcept
  800. { return std::move(this->_M_get()); }
  801. constexpr explicit operator bool() const noexcept
  802. { return this->_M_is_engaged(); }
  803. constexpr bool has_value() const noexcept
  804. { return this->_M_is_engaged(); }
  805. constexpr const _Tp&
  806. value() const&
  807. {
  808. return this->_M_is_engaged()
  809. ? this->_M_get()
  810. : (__throw_bad_optional_access(), this->_M_get());
  811. }
  812. constexpr _Tp&
  813. value()&
  814. {
  815. return this->_M_is_engaged()
  816. ? this->_M_get()
  817. : (__throw_bad_optional_access(), this->_M_get());
  818. }
  819. constexpr _Tp&&
  820. value()&&
  821. {
  822. return this->_M_is_engaged()
  823. ? std::move(this->_M_get())
  824. : (__throw_bad_optional_access(), std::move(this->_M_get()));
  825. }
  826. constexpr const _Tp&&
  827. value() const&&
  828. {
  829. return this->_M_is_engaged()
  830. ? std::move(this->_M_get())
  831. : (__throw_bad_optional_access(), std::move(this->_M_get()));
  832. }
  833. template<typename _Up>
  834. constexpr _Tp
  835. value_or(_Up&& __u) const&
  836. {
  837. static_assert(is_copy_constructible_v<_Tp>);
  838. static_assert(is_convertible_v<_Up&&, _Tp>);
  839. return this->_M_is_engaged()
  840. ? this->_M_get() : static_cast<_Tp>(std::forward<_Up>(__u));
  841. }
  842. template<typename _Up>
  843. constexpr _Tp
  844. value_or(_Up&& __u) &&
  845. {
  846. static_assert(is_move_constructible_v<_Tp>);
  847. static_assert(is_convertible_v<_Up&&, _Tp>);
  848. return this->_M_is_engaged()
  849. ? std::move(this->_M_get())
  850. : static_cast<_Tp>(std::forward<_Up>(__u));
  851. }
  852. void reset() noexcept { this->_M_reset(); }
  853. };
  854. template<typename _Tp>
  855. using __optional_relop_t =
  856. enable_if_t<is_convertible<_Tp, bool>::value, bool>;
  857. template<typename _Tp, typename _Up>
  858. using __optional_eq_t = __optional_relop_t<
  859. decltype(std::declval<const _Tp&>() == std::declval<const _Up&>())
  860. >;
  861. template<typename _Tp, typename _Up>
  862. using __optional_ne_t = __optional_relop_t<
  863. decltype(std::declval<const _Tp&>() != std::declval<const _Up&>())
  864. >;
  865. template<typename _Tp, typename _Up>
  866. using __optional_lt_t = __optional_relop_t<
  867. decltype(std::declval<const _Tp&>() < std::declval<const _Up&>())
  868. >;
  869. template<typename _Tp, typename _Up>
  870. using __optional_gt_t = __optional_relop_t<
  871. decltype(std::declval<const _Tp&>() > std::declval<const _Up&>())
  872. >;
  873. template<typename _Tp, typename _Up>
  874. using __optional_le_t = __optional_relop_t<
  875. decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>())
  876. >;
  877. template<typename _Tp, typename _Up>
  878. using __optional_ge_t = __optional_relop_t<
  879. decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>())
  880. >;
  881. // Comparisons between optional values.
  882. template<typename _Tp, typename _Up>
  883. constexpr auto
  884. operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
  885. -> __optional_eq_t<_Tp, _Up>
  886. {
  887. return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
  888. && (!__lhs || *__lhs == *__rhs);
  889. }
  890. template<typename _Tp, typename _Up>
  891. constexpr auto
  892. operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
  893. -> __optional_ne_t<_Tp, _Up>
  894. {
  895. return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
  896. || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
  897. }
  898. template<typename _Tp, typename _Up>
  899. constexpr auto
  900. operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
  901. -> __optional_lt_t<_Tp, _Up>
  902. {
  903. return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
  904. }
  905. template<typename _Tp, typename _Up>
  906. constexpr auto
  907. operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
  908. -> __optional_gt_t<_Tp, _Up>
  909. {
  910. return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
  911. }
  912. template<typename _Tp, typename _Up>
  913. constexpr auto
  914. operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
  915. -> __optional_le_t<_Tp, _Up>
  916. {
  917. return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
  918. }
  919. template<typename _Tp, typename _Up>
  920. constexpr auto
  921. operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
  922. -> __optional_ge_t<_Tp, _Up>
  923. {
  924. return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
  925. }
  926. #ifdef __cpp_lib_three_way_comparison
  927. template<typename _Tp, three_way_comparable_with<_Tp> _Up>
  928. constexpr compare_three_way_result_t<_Tp, _Up>
  929. operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
  930. {
  931. return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
  932. }
  933. #endif
  934. // Comparisons with nullopt.
  935. template<typename _Tp>
  936. constexpr bool
  937. operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
  938. { return !__lhs; }
  939. #ifdef __cpp_lib_three_way_comparison
  940. template<typename _Tp>
  941. constexpr strong_ordering
  942. operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
  943. { return bool(__x) <=> false; }
  944. #else
  945. template<typename _Tp>
  946. constexpr bool
  947. operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
  948. { return !__rhs; }
  949. template<typename _Tp>
  950. constexpr bool
  951. operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
  952. { return static_cast<bool>(__lhs); }
  953. template<typename _Tp>
  954. constexpr bool
  955. operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
  956. { return static_cast<bool>(__rhs); }
  957. template<typename _Tp>
  958. constexpr bool
  959. operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
  960. { return false; }
  961. template<typename _Tp>
  962. constexpr bool
  963. operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
  964. { return static_cast<bool>(__rhs); }
  965. template<typename _Tp>
  966. constexpr bool
  967. operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
  968. { return static_cast<bool>(__lhs); }
  969. template<typename _Tp>
  970. constexpr bool
  971. operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
  972. { return false; }
  973. template<typename _Tp>
  974. constexpr bool
  975. operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
  976. { return !__lhs; }
  977. template<typename _Tp>
  978. constexpr bool
  979. operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
  980. { return true; }
  981. template<typename _Tp>
  982. constexpr bool
  983. operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
  984. { return true; }
  985. template<typename _Tp>
  986. constexpr bool
  987. operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
  988. { return !__rhs; }
  989. #endif // three-way-comparison
  990. // Comparisons with value type.
  991. template<typename _Tp, typename _Up>
  992. constexpr auto
  993. operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
  994. -> __optional_eq_t<_Tp, _Up>
  995. { return __lhs && *__lhs == __rhs; }
  996. template<typename _Tp, typename _Up>
  997. constexpr auto
  998. operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
  999. -> __optional_eq_t<_Up, _Tp>
  1000. { return __rhs && __lhs == *__rhs; }
  1001. template<typename _Tp, typename _Up>
  1002. constexpr auto
  1003. operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
  1004. -> __optional_ne_t<_Tp, _Up>
  1005. { return !__lhs || *__lhs != __rhs; }
  1006. template<typename _Tp, typename _Up>
  1007. constexpr auto
  1008. operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
  1009. -> __optional_ne_t<_Up, _Tp>
  1010. { return !__rhs || __lhs != *__rhs; }
  1011. template<typename _Tp, typename _Up>
  1012. constexpr auto
  1013. operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
  1014. -> __optional_lt_t<_Tp, _Up>
  1015. { return !__lhs || *__lhs < __rhs; }
  1016. template<typename _Tp, typename _Up>
  1017. constexpr auto
  1018. operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
  1019. -> __optional_lt_t<_Up, _Tp>
  1020. { return __rhs && __lhs < *__rhs; }
  1021. template<typename _Tp, typename _Up>
  1022. constexpr auto
  1023. operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
  1024. -> __optional_gt_t<_Tp, _Up>
  1025. { return __lhs && *__lhs > __rhs; }
  1026. template<typename _Tp, typename _Up>
  1027. constexpr auto
  1028. operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
  1029. -> __optional_gt_t<_Up, _Tp>
  1030. { return !__rhs || __lhs > *__rhs; }
  1031. template<typename _Tp, typename _Up>
  1032. constexpr auto
  1033. operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
  1034. -> __optional_le_t<_Tp, _Up>
  1035. { return !__lhs || *__lhs <= __rhs; }
  1036. template<typename _Tp, typename _Up>
  1037. constexpr auto
  1038. operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
  1039. -> __optional_le_t<_Up, _Tp>
  1040. { return __rhs && __lhs <= *__rhs; }
  1041. template<typename _Tp, typename _Up>
  1042. constexpr auto
  1043. operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
  1044. -> __optional_ge_t<_Tp, _Up>
  1045. { return __lhs && *__lhs >= __rhs; }
  1046. template<typename _Tp, typename _Up>
  1047. constexpr auto
  1048. operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
  1049. -> __optional_ge_t<_Up, _Tp>
  1050. { return !__rhs || __lhs >= *__rhs; }
  1051. #ifdef __cpp_lib_three_way_comparison
  1052. template<typename _Tp, typename _Up>
  1053. constexpr compare_three_way_result_t<_Tp, _Up>
  1054. operator<=>(const optional<_Tp>& __x, const _Up& __v)
  1055. { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
  1056. #endif
  1057. // Swap and creation functions.
  1058. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1059. // 2748. swappable traits for optionals
  1060. template<typename _Tp>
  1061. inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
  1062. swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
  1063. noexcept(noexcept(__lhs.swap(__rhs)))
  1064. { __lhs.swap(__rhs); }
  1065. template<typename _Tp>
  1066. enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
  1067. swap(optional<_Tp>&, optional<_Tp>&) = delete;
  1068. template<typename _Tp>
  1069. constexpr
  1070. enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>,
  1071. optional<decay_t<_Tp>>>
  1072. make_optional(_Tp&& __t)
  1073. noexcept(is_nothrow_constructible_v<optional<decay_t<_Tp>>, _Tp>)
  1074. { return optional<decay_t<_Tp>>{ std::forward<_Tp>(__t) }; }
  1075. template<typename _Tp, typename... _Args>
  1076. constexpr
  1077. enable_if_t<is_constructible_v<_Tp, _Args...>,
  1078. optional<_Tp>>
  1079. make_optional(_Args&&... __args)
  1080. noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
  1081. { return optional<_Tp>{ in_place, std::forward<_Args>(__args)... }; }
  1082. template<typename _Tp, typename _Up, typename... _Args>
  1083. constexpr
  1084. enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
  1085. optional<_Tp>>
  1086. make_optional(initializer_list<_Up> __il, _Args&&... __args)
  1087. noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
  1088. { return optional<_Tp>{ in_place, __il, std::forward<_Args>(__args)... }; }
  1089. // Hash.
  1090. template<typename _Tp, typename _Up = remove_const_t<_Tp>,
  1091. bool = __poison_hash<_Up>::__enable_hash_call>
  1092. struct __optional_hash_call_base
  1093. {
  1094. size_t
  1095. operator()(const optional<_Tp>& __t) const
  1096. noexcept(noexcept(hash<_Up>{}(*__t)))
  1097. {
  1098. // We pick an arbitrary hash for disengaged optionals which hopefully
  1099. // usual values of _Tp won't typically hash to.
  1100. constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
  1101. return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
  1102. }
  1103. };
  1104. template<typename _Tp, typename _Up>
  1105. struct __optional_hash_call_base<_Tp, _Up, false> {};
  1106. template<typename _Tp>
  1107. struct hash<optional<_Tp>>
  1108. : private __poison_hash<remove_const_t<_Tp>>,
  1109. public __optional_hash_call_base<_Tp>
  1110. {
  1111. using result_type [[__deprecated__]] = size_t;
  1112. using argument_type [[__deprecated__]] = optional<_Tp>;
  1113. };
  1114. template<typename _Tp>
  1115. struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
  1116. { };
  1117. /// @}
  1118. #if __cpp_deduction_guides >= 201606
  1119. template <typename _Tp> optional(_Tp) -> optional<_Tp>;
  1120. #endif
  1121. _GLIBCXX_END_NAMESPACE_VERSION
  1122. } // namespace std
  1123. #endif // C++17
  1124. #endif // _GLIBCXX_OPTIONAL