optional 37 KB

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