Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. // <variant> -*- C++ -*-
  2. // Copyright (C) 2016-2020 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 variant
  21. * This is the <variant> C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_VARIANT
  24. #define _GLIBCXX_VARIANT 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201703L
  27. #include <type_traits>
  28. #include <utility>
  29. #include <bits/enable_special_members.h>
  30. #include <bits/functexcept.h>
  31. #include <bits/move.h>
  32. #include <bits/functional_hash.h>
  33. #include <bits/invoke.h>
  34. #include <ext/aligned_buffer.h>
  35. #include <bits/parse_numbers.h>
  36. #include <bits/stl_iterator_base_types.h>
  37. #include <bits/stl_iterator_base_funcs.h>
  38. #include <bits/stl_construct.h>
  39. #if __cplusplus > 201703L
  40. # include <compare>
  41. #endif
  42. namespace std _GLIBCXX_VISIBILITY(default)
  43. {
  44. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  45. namespace __detail
  46. {
  47. namespace __variant
  48. {
  49. template<size_t _Np, typename... _Types>
  50. struct _Nth_type;
  51. template<size_t _Np, typename _First, typename... _Rest>
  52. struct _Nth_type<_Np, _First, _Rest...>
  53. : _Nth_type<_Np-1, _Rest...> { };
  54. template<typename _First, typename... _Rest>
  55. struct _Nth_type<0, _First, _Rest...>
  56. { using type = _First; };
  57. } // namespace __variant
  58. } // namespace __detail
  59. #define __cpp_lib_variant 201606L
  60. template<typename... _Types> class tuple;
  61. template<typename... _Types> class variant;
  62. template <typename> struct hash;
  63. template<typename _Variant>
  64. struct variant_size;
  65. template<typename _Variant>
  66. struct variant_size<const _Variant> : variant_size<_Variant> {};
  67. template<typename _Variant>
  68. struct variant_size<volatile _Variant> : variant_size<_Variant> {};
  69. template<typename _Variant>
  70. struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
  71. template<typename... _Types>
  72. struct variant_size<variant<_Types...>>
  73. : std::integral_constant<size_t, sizeof...(_Types)> {};
  74. template<typename _Variant>
  75. inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
  76. template<size_t _Np, typename _Variant>
  77. struct variant_alternative;
  78. template<size_t _Np, typename _First, typename... _Rest>
  79. struct variant_alternative<_Np, variant<_First, _Rest...>>
  80. : variant_alternative<_Np-1, variant<_Rest...>> {};
  81. template<typename _First, typename... _Rest>
  82. struct variant_alternative<0, variant<_First, _Rest...>>
  83. { using type = _First; };
  84. template<size_t _Np, typename _Variant>
  85. using variant_alternative_t =
  86. typename variant_alternative<_Np, _Variant>::type;
  87. template<size_t _Np, typename _Variant>
  88. struct variant_alternative<_Np, const _Variant>
  89. { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
  90. template<size_t _Np, typename _Variant>
  91. struct variant_alternative<_Np, volatile _Variant>
  92. { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
  93. template<size_t _Np, typename _Variant>
  94. struct variant_alternative<_Np, const volatile _Variant>
  95. { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
  96. inline constexpr size_t variant_npos = -1;
  97. template<size_t _Np, typename... _Types>
  98. constexpr variant_alternative_t<_Np, variant<_Types...>>&
  99. get(variant<_Types...>&);
  100. template<size_t _Np, typename... _Types>
  101. constexpr variant_alternative_t<_Np, variant<_Types...>>&&
  102. get(variant<_Types...>&&);
  103. template<size_t _Np, typename... _Types>
  104. constexpr variant_alternative_t<_Np, variant<_Types...>> const&
  105. get(const variant<_Types...>&);
  106. template<size_t _Np, typename... _Types>
  107. constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
  108. get(const variant<_Types...>&&);
  109. template<typename _Result_type, typename _Visitor, typename... _Variants>
  110. constexpr decltype(auto)
  111. __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
  112. template <typename... _Types, typename _Tp>
  113. decltype(auto)
  114. __variant_cast(_Tp&& __rhs)
  115. {
  116. if constexpr (is_lvalue_reference_v<_Tp>)
  117. {
  118. if constexpr (is_const_v<remove_reference_t<_Tp>>)
  119. return static_cast<const variant<_Types...>&>(__rhs);
  120. else
  121. return static_cast<variant<_Types...>&>(__rhs);
  122. }
  123. else
  124. return static_cast<variant<_Types...>&&>(__rhs);
  125. }
  126. namespace __detail
  127. {
  128. namespace __variant
  129. {
  130. // Returns the first appearence of _Tp in _Types.
  131. // Returns sizeof...(_Types) if _Tp is not in _Types.
  132. template<typename _Tp, typename... _Types>
  133. struct __index_of : std::integral_constant<size_t, 0> {};
  134. template<typename _Tp, typename... _Types>
  135. inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value;
  136. template<typename _Tp, typename _First, typename... _Rest>
  137. struct __index_of<_Tp, _First, _Rest...> :
  138. std::integral_constant<size_t, is_same_v<_Tp, _First>
  139. ? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
  140. // used for raw visitation
  141. struct __variant_cookie {};
  142. // used for raw visitation with indices passed in
  143. struct __variant_idx_cookie { using type = __variant_idx_cookie; };
  144. // Used to enable deduction (and same-type checking) for std::visit:
  145. template<typename> struct __deduce_visit_result { };
  146. // Visit variants that might be valueless.
  147. template<typename _Visitor, typename... _Variants>
  148. constexpr void
  149. __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
  150. {
  151. std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
  152. std::forward<_Variants>(__variants)...);
  153. }
  154. // Visit variants that might be valueless, passing indices to the visitor.
  155. template<typename _Visitor, typename... _Variants>
  156. constexpr void
  157. __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
  158. {
  159. std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
  160. std::forward<_Variants>(__variants)...);
  161. }
  162. // _Uninitialized<T> is guaranteed to be a trivially destructible type,
  163. // even if T is not.
  164. template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
  165. struct _Uninitialized;
  166. template<typename _Type>
  167. struct _Uninitialized<_Type, true>
  168. {
  169. template<typename... _Args>
  170. constexpr
  171. _Uninitialized(in_place_index_t<0>, _Args&&... __args)
  172. : _M_storage(std::forward<_Args>(__args)...)
  173. { }
  174. constexpr const _Type& _M_get() const & noexcept
  175. { return _M_storage; }
  176. constexpr _Type& _M_get() & noexcept
  177. { return _M_storage; }
  178. constexpr const _Type&& _M_get() const && noexcept
  179. { return std::move(_M_storage); }
  180. constexpr _Type&& _M_get() && noexcept
  181. { return std::move(_M_storage); }
  182. _Type _M_storage;
  183. };
  184. template<typename _Type>
  185. struct _Uninitialized<_Type, false>
  186. {
  187. template<typename... _Args>
  188. constexpr
  189. _Uninitialized(in_place_index_t<0>, _Args&&... __args)
  190. {
  191. ::new ((void*)std::addressof(_M_storage))
  192. _Type(std::forward<_Args>(__args)...);
  193. }
  194. const _Type& _M_get() const & noexcept
  195. { return *_M_storage._M_ptr(); }
  196. _Type& _M_get() & noexcept
  197. { return *_M_storage._M_ptr(); }
  198. const _Type&& _M_get() const && noexcept
  199. { return std::move(*_M_storage._M_ptr()); }
  200. _Type&& _M_get() && noexcept
  201. { return std::move(*_M_storage._M_ptr()); }
  202. __gnu_cxx::__aligned_membuf<_Type> _M_storage;
  203. };
  204. template<typename _Union>
  205. constexpr decltype(auto)
  206. __get(in_place_index_t<0>, _Union&& __u) noexcept
  207. { return std::forward<_Union>(__u)._M_first._M_get(); }
  208. template<size_t _Np, typename _Union>
  209. constexpr decltype(auto)
  210. __get(in_place_index_t<_Np>, _Union&& __u) noexcept
  211. {
  212. return __variant::__get(in_place_index<_Np-1>,
  213. std::forward<_Union>(__u)._M_rest);
  214. }
  215. // Returns the typed storage for __v.
  216. template<size_t _Np, typename _Variant>
  217. constexpr decltype(auto)
  218. __get(_Variant&& __v) noexcept
  219. {
  220. return __variant::__get(std::in_place_index<_Np>,
  221. std::forward<_Variant>(__v)._M_u);
  222. }
  223. template<typename... _Types>
  224. struct _Traits
  225. {
  226. static constexpr bool _S_default_ctor =
  227. is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
  228. static constexpr bool _S_copy_ctor =
  229. (is_copy_constructible_v<_Types> && ...);
  230. static constexpr bool _S_move_ctor =
  231. (is_move_constructible_v<_Types> && ...);
  232. static constexpr bool _S_copy_assign =
  233. _S_copy_ctor
  234. && (is_copy_assignable_v<_Types> && ...);
  235. static constexpr bool _S_move_assign =
  236. _S_move_ctor
  237. && (is_move_assignable_v<_Types> && ...);
  238. static constexpr bool _S_trivial_dtor =
  239. (is_trivially_destructible_v<_Types> && ...);
  240. static constexpr bool _S_trivial_copy_ctor =
  241. (is_trivially_copy_constructible_v<_Types> && ...);
  242. static constexpr bool _S_trivial_move_ctor =
  243. (is_trivially_move_constructible_v<_Types> && ...);
  244. static constexpr bool _S_trivial_copy_assign =
  245. _S_trivial_dtor && _S_trivial_copy_ctor
  246. && (is_trivially_copy_assignable_v<_Types> && ...);
  247. static constexpr bool _S_trivial_move_assign =
  248. _S_trivial_dtor && _S_trivial_move_ctor
  249. && (is_trivially_move_assignable_v<_Types> && ...);
  250. // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
  251. // are always nothrow.
  252. static constexpr bool _S_nothrow_default_ctor =
  253. is_nothrow_default_constructible_v<
  254. typename _Nth_type<0, _Types...>::type>;
  255. static constexpr bool _S_nothrow_copy_ctor = false;
  256. static constexpr bool _S_nothrow_move_ctor =
  257. (is_nothrow_move_constructible_v<_Types> && ...);
  258. static constexpr bool _S_nothrow_copy_assign = false;
  259. static constexpr bool _S_nothrow_move_assign =
  260. _S_nothrow_move_ctor
  261. && (is_nothrow_move_assignable_v<_Types> && ...);
  262. };
  263. // Defines members and ctors.
  264. template<typename... _Types>
  265. union _Variadic_union { };
  266. template<typename _First, typename... _Rest>
  267. union _Variadic_union<_First, _Rest...>
  268. {
  269. constexpr _Variadic_union() : _M_rest() { }
  270. template<typename... _Args>
  271. constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
  272. : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
  273. { }
  274. template<size_t _Np, typename... _Args>
  275. constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
  276. : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
  277. { }
  278. _Uninitialized<_First> _M_first;
  279. _Variadic_union<_Rest...> _M_rest;
  280. };
  281. // _Never_valueless_alt is true for variant alternatives that can
  282. // always be placed in a variant without it becoming valueless.
  283. // For suitably-small, trivially copyable types we can create temporaries
  284. // on the stack and then memcpy them into place.
  285. template<typename _Tp>
  286. struct _Never_valueless_alt
  287. : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
  288. { };
  289. // Specialize _Never_valueless_alt for other types which have a
  290. // non-throwing and cheap move construction and move assignment operator,
  291. // so that emplacing the type will provide the strong exception-safety
  292. // guarantee, by creating and moving a temporary.
  293. // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
  294. // variant using that alternative, so we can't change the value later!
  295. // True if every alternative in _Types... can be emplaced in a variant
  296. // without it becoming valueless. If this is true, variant<_Types...>
  297. // can never be valueless, which enables some minor optimizations.
  298. template <typename... _Types>
  299. constexpr bool __never_valueless()
  300. {
  301. return _Traits<_Types...>::_S_move_assign
  302. && (_Never_valueless_alt<_Types>::value && ...);
  303. }
  304. // Defines index and the dtor, possibly trivial.
  305. template<bool __trivially_destructible, typename... _Types>
  306. struct _Variant_storage;
  307. template <typename... _Types>
  308. using __select_index =
  309. typename __select_int::_Select_int_base<sizeof...(_Types),
  310. unsigned char,
  311. unsigned short>::type::value_type;
  312. template<typename... _Types>
  313. struct _Variant_storage<false, _Types...>
  314. {
  315. constexpr
  316. _Variant_storage()
  317. : _M_index(static_cast<__index_type>(variant_npos))
  318. { }
  319. template<size_t _Np, typename... _Args>
  320. constexpr
  321. _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
  322. : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
  323. _M_index{_Np}
  324. { }
  325. void _M_reset()
  326. {
  327. if (!_M_valid()) [[unlikely]]
  328. return;
  329. std::__do_visit<void>([](auto&& __this_mem) mutable
  330. {
  331. std::_Destroy(std::__addressof(__this_mem));
  332. }, __variant_cast<_Types...>(*this));
  333. _M_index = static_cast<__index_type>(variant_npos);
  334. }
  335. ~_Variant_storage()
  336. { _M_reset(); }
  337. void*
  338. _M_storage() const noexcept
  339. {
  340. return const_cast<void*>(static_cast<const void*>(
  341. std::addressof(_M_u)));
  342. }
  343. constexpr bool
  344. _M_valid() const noexcept
  345. {
  346. if constexpr (__variant::__never_valueless<_Types...>())
  347. return true;
  348. return this->_M_index != __index_type(variant_npos);
  349. }
  350. _Variadic_union<_Types...> _M_u;
  351. using __index_type = __select_index<_Types...>;
  352. __index_type _M_index;
  353. };
  354. template<typename... _Types>
  355. struct _Variant_storage<true, _Types...>
  356. {
  357. constexpr
  358. _Variant_storage()
  359. : _M_index(static_cast<__index_type>(variant_npos))
  360. { }
  361. template<size_t _Np, typename... _Args>
  362. constexpr
  363. _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
  364. : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
  365. _M_index{_Np}
  366. { }
  367. void _M_reset() noexcept
  368. { _M_index = static_cast<__index_type>(variant_npos); }
  369. void*
  370. _M_storage() const noexcept
  371. {
  372. return const_cast<void*>(static_cast<const void*>(
  373. std::addressof(_M_u)));
  374. }
  375. constexpr bool
  376. _M_valid() const noexcept
  377. {
  378. if constexpr (__variant::__never_valueless<_Types...>())
  379. return true;
  380. return this->_M_index != static_cast<__index_type>(variant_npos);
  381. }
  382. _Variadic_union<_Types...> _M_u;
  383. using __index_type = __select_index<_Types...>;
  384. __index_type _M_index;
  385. };
  386. template<typename... _Types>
  387. using _Variant_storage_alias =
  388. _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
  389. template<typename _Tp, typename _Up>
  390. void __variant_construct_single(_Tp&& __lhs, _Up&& __rhs_mem)
  391. {
  392. void* __storage = std::addressof(__lhs._M_u);
  393. using _Type = remove_reference_t<decltype(__rhs_mem)>;
  394. if constexpr (!is_same_v<_Type, __variant_cookie>)
  395. ::new (__storage)
  396. _Type(std::forward<decltype(__rhs_mem)>(__rhs_mem));
  397. }
  398. template<typename... _Types, typename _Tp, typename _Up>
  399. void __variant_construct(_Tp&& __lhs, _Up&& __rhs)
  400. {
  401. __lhs._M_index = __rhs._M_index;
  402. __variant::__raw_visit([&__lhs](auto&& __rhs_mem) mutable
  403. {
  404. __variant_construct_single(std::forward<_Tp>(__lhs),
  405. std::forward<decltype(__rhs_mem)>(__rhs_mem));
  406. }, __variant_cast<_Types...>(std::forward<_Up>(__rhs)));
  407. }
  408. // The following are (Copy|Move) (ctor|assign) layers for forwarding
  409. // triviality and handling non-trivial SMF behaviors.
  410. template<bool, typename... _Types>
  411. struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
  412. {
  413. using _Base = _Variant_storage_alias<_Types...>;
  414. using _Base::_Base;
  415. _Copy_ctor_base(const _Copy_ctor_base& __rhs)
  416. noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
  417. {
  418. __variant_construct<_Types...>(*this, __rhs);
  419. }
  420. _Copy_ctor_base(_Copy_ctor_base&&) = default;
  421. _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
  422. _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
  423. };
  424. template<typename... _Types>
  425. struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
  426. {
  427. using _Base = _Variant_storage_alias<_Types...>;
  428. using _Base::_Base;
  429. };
  430. template<typename... _Types>
  431. using _Copy_ctor_alias =
  432. _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
  433. template<bool, typename... _Types>
  434. struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
  435. {
  436. using _Base = _Copy_ctor_alias<_Types...>;
  437. using _Base::_Base;
  438. _Move_ctor_base(_Move_ctor_base&& __rhs)
  439. noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
  440. {
  441. __variant_construct<_Types...>(*this, std::move(__rhs));
  442. }
  443. template<typename _Up>
  444. void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
  445. {
  446. this->_M_reset();
  447. __variant_construct_single(*this, std::forward<_Up>(__rhs));
  448. this->_M_index = __rhs_index;
  449. }
  450. template<typename _Up>
  451. void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
  452. {
  453. this->_M_reset();
  454. __variant_construct_single(*this, __rhs);
  455. this->_M_index = __rhs_index;
  456. }
  457. _Move_ctor_base(const _Move_ctor_base&) = default;
  458. _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
  459. _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
  460. };
  461. template<typename... _Types>
  462. struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
  463. {
  464. using _Base = _Copy_ctor_alias<_Types...>;
  465. using _Base::_Base;
  466. template<typename _Up>
  467. void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
  468. {
  469. this->_M_reset();
  470. __variant_construct_single(*this, std::forward<_Up>(__rhs));
  471. this->_M_index = __rhs_index;
  472. }
  473. template<typename _Up>
  474. void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
  475. {
  476. this->_M_reset();
  477. __variant_construct_single(*this, __rhs);
  478. this->_M_index = __rhs_index;
  479. }
  480. };
  481. template<typename... _Types>
  482. using _Move_ctor_alias =
  483. _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
  484. template<bool, typename... _Types>
  485. struct _Copy_assign_base : _Move_ctor_alias<_Types...>
  486. {
  487. using _Base = _Move_ctor_alias<_Types...>;
  488. using _Base::_Base;
  489. _Copy_assign_base&
  490. operator=(const _Copy_assign_base& __rhs)
  491. noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
  492. {
  493. __variant::__raw_idx_visit(
  494. [this](auto&& __rhs_mem, auto __rhs_index) mutable
  495. {
  496. if constexpr (__rhs_index != variant_npos)
  497. {
  498. if (this->_M_index == __rhs_index)
  499. __variant::__get<__rhs_index>(*this) = __rhs_mem;
  500. else
  501. {
  502. using __rhs_type = __remove_cvref_t<decltype(__rhs_mem)>;
  503. if constexpr (is_nothrow_copy_constructible_v<__rhs_type>
  504. || !is_nothrow_move_constructible_v<__rhs_type>)
  505. // The standard says this->emplace<__rhs_type>(__rhs_mem)
  506. // should be used here, but _M_destructive_copy is
  507. // equivalent in this case. Either copy construction
  508. // doesn't throw, so _M_destructive_copy gives strong
  509. // exception safety guarantee, or both copy construction
  510. // and move construction can throw, so emplace only gives
  511. // basic exception safety anyway.
  512. this->_M_destructive_copy(__rhs_index, __rhs_mem);
  513. else
  514. __variant_cast<_Types...>(*this)
  515. = variant<_Types...>(std::in_place_index<__rhs_index>,
  516. __rhs_mem);
  517. }
  518. }
  519. else
  520. this->_M_reset();
  521. }, __variant_cast<_Types...>(__rhs));
  522. return *this;
  523. }
  524. _Copy_assign_base(const _Copy_assign_base&) = default;
  525. _Copy_assign_base(_Copy_assign_base&&) = default;
  526. _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
  527. };
  528. template<typename... _Types>
  529. struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
  530. {
  531. using _Base = _Move_ctor_alias<_Types...>;
  532. using _Base::_Base;
  533. };
  534. template<typename... _Types>
  535. using _Copy_assign_alias =
  536. _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
  537. template<bool, typename... _Types>
  538. struct _Move_assign_base : _Copy_assign_alias<_Types...>
  539. {
  540. using _Base = _Copy_assign_alias<_Types...>;
  541. using _Base::_Base;
  542. _Move_assign_base&
  543. operator=(_Move_assign_base&& __rhs)
  544. noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
  545. {
  546. __variant::__raw_idx_visit(
  547. [this](auto&& __rhs_mem, auto __rhs_index) mutable
  548. {
  549. if constexpr (__rhs_index != variant_npos)
  550. {
  551. if (this->_M_index == __rhs_index)
  552. __variant::__get<__rhs_index>(*this) = std::move(__rhs_mem);
  553. else
  554. __variant_cast<_Types...>(*this)
  555. .template emplace<__rhs_index>(std::move(__rhs_mem));
  556. }
  557. else
  558. this->_M_reset();
  559. }, __variant_cast<_Types...>(__rhs));
  560. return *this;
  561. }
  562. _Move_assign_base(const _Move_assign_base&) = default;
  563. _Move_assign_base(_Move_assign_base&&) = default;
  564. _Move_assign_base& operator=(const _Move_assign_base&) = default;
  565. };
  566. template<typename... _Types>
  567. struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
  568. {
  569. using _Base = _Copy_assign_alias<_Types...>;
  570. using _Base::_Base;
  571. };
  572. template<typename... _Types>
  573. using _Move_assign_alias =
  574. _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
  575. template<typename... _Types>
  576. struct _Variant_base : _Move_assign_alias<_Types...>
  577. {
  578. using _Base = _Move_assign_alias<_Types...>;
  579. constexpr
  580. _Variant_base()
  581. noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
  582. : _Variant_base(in_place_index<0>) { }
  583. template<size_t _Np, typename... _Args>
  584. constexpr explicit
  585. _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
  586. : _Base(__i, std::forward<_Args>(__args)...)
  587. { }
  588. _Variant_base(const _Variant_base&) = default;
  589. _Variant_base(_Variant_base&&) = default;
  590. _Variant_base& operator=(const _Variant_base&) = default;
  591. _Variant_base& operator=(_Variant_base&&) = default;
  592. };
  593. // For how many times does _Tp appear in _Tuple?
  594. template<typename _Tp, typename _Tuple>
  595. struct __tuple_count;
  596. template<typename _Tp, typename _Tuple>
  597. inline constexpr size_t __tuple_count_v =
  598. __tuple_count<_Tp, _Tuple>::value;
  599. template<typename _Tp, typename... _Types>
  600. struct __tuple_count<_Tp, tuple<_Types...>>
  601. : integral_constant<size_t, 0> { };
  602. template<typename _Tp, typename _First, typename... _Rest>
  603. struct __tuple_count<_Tp, tuple<_First, _Rest...>>
  604. : integral_constant<
  605. size_t,
  606. __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
  607. // TODO: Reuse this in <tuple> ?
  608. template<typename _Tp, typename... _Types>
  609. inline constexpr bool __exactly_once =
  610. __tuple_count_v<_Tp, tuple<_Types...>> == 1;
  611. // Helper used to check for valid conversions that don't involve narrowing.
  612. template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
  613. // Build an imaginary function FUN(Ti) for each alternative type Ti
  614. template<size_t _Ind, typename _Tp, typename _Ti,
  615. bool _Ti_is_cv_bool = is_same_v<remove_cv_t<_Ti>, bool>,
  616. typename = void>
  617. struct _Build_FUN
  618. {
  619. // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
  620. // but only static functions will be considered in the call below.
  621. void _S_fun();
  622. };
  623. // ... for which Ti x[] = {std::forward<T>(t)}; is well-formed,
  624. template<size_t _Ind, typename _Tp, typename _Ti>
  625. struct _Build_FUN<_Ind, _Tp, _Ti, false,
  626. void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
  627. {
  628. // This is the FUN function for type _Ti, with index _Ind
  629. static integral_constant<size_t, _Ind> _S_fun(_Ti);
  630. };
  631. // ... and if Ti is cv bool, remove_cvref_t<T> is bool.
  632. template<size_t _Ind, typename _Tp, typename _Ti>
  633. struct _Build_FUN<_Ind, _Tp, _Ti, true,
  634. enable_if_t<is_same_v<__remove_cvref_t<_Tp>, bool>>>
  635. {
  636. // This is the FUN function for when _Ti is cv bool, with index _Ind
  637. static integral_constant<size_t, _Ind> _S_fun(_Ti);
  638. };
  639. template<typename _Tp, typename _Variant,
  640. typename = make_index_sequence<variant_size_v<_Variant>>>
  641. struct _Build_FUNs;
  642. template<typename _Tp, typename... _Ti, size_t... _Ind>
  643. struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
  644. : _Build_FUN<_Ind, _Tp, _Ti>...
  645. {
  646. using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
  647. };
  648. // The index j of the overload FUN(Tj) selected by overload resolution
  649. // for FUN(std::forward<_Tp>(t))
  650. template<typename _Tp, typename _Variant>
  651. using _FUN_type
  652. = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
  653. // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
  654. template<typename _Tp, typename _Variant, typename = void>
  655. struct __accepted_index
  656. : integral_constant<size_t, variant_npos>
  657. { };
  658. template<typename _Tp, typename _Variant>
  659. struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
  660. : _FUN_type<_Tp, _Variant>
  661. { };
  662. // Returns the raw storage for __v.
  663. template<typename _Variant>
  664. void* __get_storage(_Variant&& __v) noexcept
  665. { return __v._M_storage(); }
  666. template <typename _Maybe_variant_cookie, typename _Variant>
  667. struct _Extra_visit_slot_needed
  668. {
  669. template <typename> struct _Variant_never_valueless;
  670. template <typename... _Types>
  671. struct _Variant_never_valueless<variant<_Types...>>
  672. : bool_constant<__variant::__never_valueless<_Types...>()> {};
  673. static constexpr bool value =
  674. (is_same_v<_Maybe_variant_cookie, __variant_cookie>
  675. || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
  676. && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
  677. };
  678. // Used for storing a multi-dimensional vtable.
  679. template<typename _Tp, size_t... _Dimensions>
  680. struct _Multi_array;
  681. // Partial specialization with rank zero, stores a single _Tp element.
  682. template<typename _Tp>
  683. struct _Multi_array<_Tp>
  684. {
  685. template<typename>
  686. struct __untag_result
  687. : false_type
  688. { using element_type = _Tp; };
  689. template <typename... _Args>
  690. struct __untag_result<const void(*)(_Args...)>
  691. : false_type
  692. { using element_type = void(*)(_Args...); };
  693. template <typename... _Args>
  694. struct __untag_result<__variant_cookie(*)(_Args...)>
  695. : false_type
  696. { using element_type = void(*)(_Args...); };
  697. template <typename... _Args>
  698. struct __untag_result<__variant_idx_cookie(*)(_Args...)>
  699. : false_type
  700. { using element_type = void(*)(_Args...); };
  701. template <typename _Res, typename... _Args>
  702. struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
  703. : true_type
  704. { using element_type = _Res(*)(_Args...); };
  705. using __result_is_deduced = __untag_result<_Tp>;
  706. constexpr const typename __untag_result<_Tp>::element_type&
  707. _M_access() const
  708. { return _M_data; }
  709. typename __untag_result<_Tp>::element_type _M_data;
  710. };
  711. // Partial specialization with rank >= 1.
  712. template<typename _Ret,
  713. typename _Visitor,
  714. typename... _Variants,
  715. size_t __first, size_t... __rest>
  716. struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
  717. {
  718. static constexpr size_t __index =
  719. sizeof...(_Variants) - sizeof...(__rest) - 1;
  720. using _Variant = typename _Nth_type<__index, _Variants...>::type;
  721. static constexpr int __do_cookie =
  722. _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
  723. using _Tp = _Ret(*)(_Visitor, _Variants...);
  724. template<typename... _Args>
  725. constexpr decltype(auto)
  726. _M_access(size_t __first_index, _Args... __rest_indices) const
  727. {
  728. return _M_arr[__first_index + __do_cookie]
  729. ._M_access(__rest_indices...);
  730. }
  731. _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
  732. };
  733. // Creates a multi-dimensional vtable recursively.
  734. //
  735. // For example,
  736. // visit([](auto, auto){},
  737. // variant<int, char>(), // typedef'ed as V1
  738. // variant<float, double, long double>()) // typedef'ed as V2
  739. // will trigger instantiations of:
  740. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
  741. // tuple<V1&&, V2&&>, std::index_sequence<>>
  742. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
  743. // tuple<V1&&, V2&&>, std::index_sequence<0>>
  744. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
  745. // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
  746. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
  747. // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
  748. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
  749. // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
  750. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
  751. // tuple<V1&&, V2&&>, std::index_sequence<1>>
  752. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
  753. // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
  754. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
  755. // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
  756. // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
  757. // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
  758. // The returned multi-dimensional vtable can be fast accessed by the visitor
  759. // using index calculation.
  760. template<typename _Array_type, typename _Index_seq>
  761. struct __gen_vtable_impl;
  762. // Defines the _S_apply() member that returns a _Multi_array populated
  763. // with function pointers that perform the visitation expressions e(m)
  764. // for each valid pack of indexes into the variant types _Variants.
  765. //
  766. // This partial specialization builds up the index sequences by recursively
  767. // calling _S_apply() on the next specialization of __gen_vtable_impl.
  768. // The base case of the recursion defines the actual function pointers.
  769. template<typename _Result_type, typename _Visitor, size_t... __dimensions,
  770. typename... _Variants, size_t... __indices>
  771. struct __gen_vtable_impl<
  772. _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
  773. std::index_sequence<__indices...>>
  774. {
  775. using _Next =
  776. remove_reference_t<typename _Nth_type<sizeof...(__indices),
  777. _Variants...>::type>;
  778. using _Array_type =
  779. _Multi_array<_Result_type (*)(_Visitor, _Variants...),
  780. __dimensions...>;
  781. static constexpr _Array_type
  782. _S_apply()
  783. {
  784. _Array_type __vtable{};
  785. _S_apply_all_alts(
  786. __vtable, make_index_sequence<variant_size_v<_Next>>());
  787. return __vtable;
  788. }
  789. template<size_t... __var_indices>
  790. static constexpr void
  791. _S_apply_all_alts(_Array_type& __vtable,
  792. std::index_sequence<__var_indices...>)
  793. {
  794. if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
  795. (_S_apply_single_alt<true, __var_indices>(
  796. __vtable._M_arr[__var_indices + 1],
  797. &(__vtable._M_arr[0])), ...);
  798. else
  799. (_S_apply_single_alt<false, __var_indices>(
  800. __vtable._M_arr[__var_indices]), ...);
  801. }
  802. template<bool __do_cookie, size_t __index, typename _Tp>
  803. static constexpr void
  804. _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
  805. {
  806. if constexpr (__do_cookie)
  807. {
  808. __element = __gen_vtable_impl<
  809. _Tp,
  810. std::index_sequence<__indices..., __index>>::_S_apply();
  811. *__cookie_element = __gen_vtable_impl<
  812. _Tp,
  813. std::index_sequence<__indices..., variant_npos>>::_S_apply();
  814. }
  815. else
  816. {
  817. __element = __gen_vtable_impl<
  818. remove_reference_t<decltype(__element)>,
  819. std::index_sequence<__indices..., __index>>::_S_apply();
  820. }
  821. }
  822. };
  823. // This partial specialization is the base case for the recursion.
  824. // It populates a _Multi_array element with the address of a function
  825. // that invokes the visitor with the alternatives specified by __indices.
  826. template<typename _Result_type, typename _Visitor, typename... _Variants,
  827. size_t... __indices>
  828. struct __gen_vtable_impl<
  829. _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
  830. std::index_sequence<__indices...>>
  831. {
  832. using _Array_type =
  833. _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
  834. template<size_t __index, typename _Variant>
  835. static constexpr decltype(auto)
  836. __element_by_index_or_cookie(_Variant&& __var) noexcept
  837. {
  838. if constexpr (__index != variant_npos)
  839. return __variant::__get<__index>(std::forward<_Variant>(__var));
  840. else
  841. return __variant_cookie{};
  842. }
  843. static constexpr decltype(auto)
  844. __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
  845. {
  846. if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
  847. // For raw visitation using indices, pass the indices to the visitor
  848. // and discard the return value:
  849. std::__invoke(std::forward<_Visitor>(__visitor),
  850. __element_by_index_or_cookie<__indices>(
  851. std::forward<_Variants>(__vars))...,
  852. integral_constant<size_t, __indices>()...);
  853. else if constexpr (is_same_v<_Result_type, __variant_cookie>)
  854. // For raw visitation without indices, and discard the return value:
  855. std::__invoke(std::forward<_Visitor>(__visitor),
  856. __element_by_index_or_cookie<__indices>(
  857. std::forward<_Variants>(__vars))...);
  858. else if constexpr (_Array_type::__result_is_deduced::value)
  859. // For the usual std::visit case deduce the return value:
  860. return std::__invoke(std::forward<_Visitor>(__visitor),
  861. __element_by_index_or_cookie<__indices>(
  862. std::forward<_Variants>(__vars))...);
  863. else // for std::visit<R> use INVOKE<R>
  864. return std::__invoke_r<_Result_type>(
  865. std::forward<_Visitor>(__visitor),
  866. __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
  867. }
  868. static constexpr auto
  869. _S_apply()
  870. { return _Array_type{&__visit_invoke}; }
  871. };
  872. template<typename _Result_type, typename _Visitor, typename... _Variants>
  873. struct __gen_vtable
  874. {
  875. using _Array_type =
  876. _Multi_array<_Result_type (*)(_Visitor, _Variants...),
  877. variant_size_v<remove_reference_t<_Variants>>...>;
  878. static constexpr _Array_type _S_vtable
  879. = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
  880. };
  881. template<size_t _Np, typename _Tp>
  882. struct _Base_dedup : public _Tp { };
  883. template<typename _Variant, typename __indices>
  884. struct _Variant_hash_base;
  885. template<typename... _Types, size_t... __indices>
  886. struct _Variant_hash_base<variant<_Types...>,
  887. std::index_sequence<__indices...>>
  888. : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
  889. } // namespace __variant
  890. } // namespace __detail
  891. template<size_t _Np, typename _Variant, typename... _Args>
  892. void __variant_construct_by_index(_Variant& __v, _Args&&... __args)
  893. {
  894. __v._M_index = _Np;
  895. auto&& __storage = __detail::__variant::__get<_Np>(__v);
  896. ::new ((void*)std::addressof(__storage))
  897. remove_reference_t<decltype(__storage)>
  898. (std::forward<_Args>(__args)...);
  899. }
  900. template<typename _Tp, typename... _Types>
  901. constexpr bool
  902. holds_alternative(const variant<_Types...>& __v) noexcept
  903. {
  904. static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
  905. "T must occur exactly once in alternatives");
  906. return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
  907. }
  908. template<typename _Tp, typename... _Types>
  909. constexpr _Tp& get(variant<_Types...>& __v)
  910. {
  911. static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
  912. "T must occur exactly once in alternatives");
  913. static_assert(!is_void_v<_Tp>, "_Tp must not be void");
  914. return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
  915. }
  916. template<typename _Tp, typename... _Types>
  917. constexpr _Tp&& get(variant<_Types...>&& __v)
  918. {
  919. static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
  920. "T must occur exactly once in alternatives");
  921. static_assert(!is_void_v<_Tp>, "_Tp must not be void");
  922. return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
  923. std::move(__v));
  924. }
  925. template<typename _Tp, typename... _Types>
  926. constexpr const _Tp& get(const variant<_Types...>& __v)
  927. {
  928. static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
  929. "T must occur exactly once in alternatives");
  930. static_assert(!is_void_v<_Tp>, "_Tp must not be void");
  931. return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
  932. }
  933. template<typename _Tp, typename... _Types>
  934. constexpr const _Tp&& get(const variant<_Types...>&& __v)
  935. {
  936. static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
  937. "T must occur exactly once in alternatives");
  938. static_assert(!is_void_v<_Tp>, "_Tp must not be void");
  939. return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
  940. std::move(__v));
  941. }
  942. template<size_t _Np, typename... _Types>
  943. constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
  944. get_if(variant<_Types...>* __ptr) noexcept
  945. {
  946. using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
  947. static_assert(_Np < sizeof...(_Types),
  948. "The index must be in [0, number of alternatives)");
  949. static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
  950. if (__ptr && __ptr->index() == _Np)
  951. return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
  952. return nullptr;
  953. }
  954. template<size_t _Np, typename... _Types>
  955. constexpr
  956. add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
  957. get_if(const variant<_Types...>* __ptr) noexcept
  958. {
  959. using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
  960. static_assert(_Np < sizeof...(_Types),
  961. "The index must be in [0, number of alternatives)");
  962. static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
  963. if (__ptr && __ptr->index() == _Np)
  964. return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
  965. return nullptr;
  966. }
  967. template<typename _Tp, typename... _Types>
  968. constexpr add_pointer_t<_Tp>
  969. get_if(variant<_Types...>* __ptr) noexcept
  970. {
  971. static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
  972. "T must occur exactly once in alternatives");
  973. static_assert(!is_void_v<_Tp>, "_Tp must not be void");
  974. return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
  975. __ptr);
  976. }
  977. template<typename _Tp, typename... _Types>
  978. constexpr add_pointer_t<const _Tp>
  979. get_if(const variant<_Types...>* __ptr) noexcept
  980. {
  981. static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
  982. "T must occur exactly once in alternatives");
  983. static_assert(!is_void_v<_Tp>, "_Tp must not be void");
  984. return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
  985. __ptr);
  986. }
  987. struct monostate { };
  988. #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
  989. template<typename... _Types> \
  990. constexpr bool operator __OP(const variant<_Types...>& __lhs, \
  991. const variant<_Types...>& __rhs) \
  992. { \
  993. bool __ret = true; \
  994. __detail::__variant::__raw_idx_visit( \
  995. [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
  996. { \
  997. if constexpr (__rhs_index != variant_npos) \
  998. { \
  999. if (__lhs.index() == __rhs_index) \
  1000. { \
  1001. auto& __this_mem = std::get<__rhs_index>(__lhs); \
  1002. __ret = __this_mem __OP __rhs_mem; \
  1003. } \
  1004. else \
  1005. __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
  1006. } \
  1007. else \
  1008. __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
  1009. }, __rhs); \
  1010. return __ret; \
  1011. }
  1012. _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
  1013. _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
  1014. _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
  1015. _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
  1016. _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
  1017. _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
  1018. #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
  1019. constexpr bool operator==(monostate, monostate) noexcept { return true; }
  1020. #ifdef __cpp_lib_three_way_comparison
  1021. template<typename... _Types>
  1022. requires (three_way_comparable<_Types> && ...)
  1023. constexpr
  1024. common_comparison_category_t<compare_three_way_result_t<_Types>...>
  1025. operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
  1026. {
  1027. common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
  1028. = strong_ordering::equal;
  1029. __detail::__variant::__raw_idx_visit(
  1030. [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
  1031. {
  1032. if constexpr (__w_index != variant_npos)
  1033. {
  1034. if (__v.index() == __w_index)
  1035. {
  1036. auto& __this_mem = std::get<__w_index>(__v);
  1037. __ret = __this_mem <=> __w_mem;
  1038. return;
  1039. }
  1040. }
  1041. __ret = (__v.index() + 1) <=> (__w_index + 1);
  1042. }, __w);
  1043. return __ret;
  1044. }
  1045. constexpr strong_ordering
  1046. operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
  1047. #else
  1048. constexpr bool operator!=(monostate, monostate) noexcept { return false; }
  1049. constexpr bool operator<(monostate, monostate) noexcept { return false; }
  1050. constexpr bool operator>(monostate, monostate) noexcept { return false; }
  1051. constexpr bool operator<=(monostate, monostate) noexcept { return true; }
  1052. constexpr bool operator>=(monostate, monostate) noexcept { return true; }
  1053. #endif
  1054. template<typename _Visitor, typename... _Variants>
  1055. constexpr decltype(auto) visit(_Visitor&&, _Variants&&...);
  1056. template<typename... _Types>
  1057. inline enable_if_t<(is_move_constructible_v<_Types> && ...)
  1058. && (is_swappable_v<_Types> && ...)>
  1059. swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
  1060. noexcept(noexcept(__lhs.swap(__rhs)))
  1061. { __lhs.swap(__rhs); }
  1062. template<typename... _Types>
  1063. enable_if_t<!((is_move_constructible_v<_Types> && ...)
  1064. && (is_swappable_v<_Types> && ...))>
  1065. swap(variant<_Types...>&, variant<_Types...>&) = delete;
  1066. class bad_variant_access : public exception
  1067. {
  1068. public:
  1069. bad_variant_access() noexcept { }
  1070. const char* what() const noexcept override
  1071. { return _M_reason; }
  1072. private:
  1073. bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
  1074. // Must point to a string with static storage duration:
  1075. const char* _M_reason = "bad variant access";
  1076. friend void __throw_bad_variant_access(const char* __what);
  1077. };
  1078. // Must only be called with a string literal
  1079. inline void
  1080. __throw_bad_variant_access(const char* __what)
  1081. { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
  1082. inline void
  1083. __throw_bad_variant_access(bool __valueless)
  1084. {
  1085. if (__valueless) [[__unlikely__]]
  1086. __throw_bad_variant_access("std::get: variant is valueless");
  1087. else
  1088. __throw_bad_variant_access("std::get: wrong index for variant");
  1089. }
  1090. template<typename... _Types>
  1091. class variant
  1092. : private __detail::__variant::_Variant_base<_Types...>,
  1093. private _Enable_default_constructor<
  1094. __detail::__variant::_Traits<_Types...>::_S_default_ctor,
  1095. variant<_Types...>>,
  1096. private _Enable_copy_move<
  1097. __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
  1098. __detail::__variant::_Traits<_Types...>::_S_copy_assign,
  1099. __detail::__variant::_Traits<_Types...>::_S_move_ctor,
  1100. __detail::__variant::_Traits<_Types...>::_S_move_assign,
  1101. variant<_Types...>>
  1102. {
  1103. private:
  1104. template <typename... _UTypes, typename _Tp>
  1105. friend decltype(auto) __variant_cast(_Tp&&);
  1106. template<size_t _Np, typename _Variant, typename... _Args>
  1107. friend void __variant_construct_by_index(_Variant& __v,
  1108. _Args&&... __args);
  1109. static_assert(sizeof...(_Types) > 0,
  1110. "variant must have at least one alternative");
  1111. static_assert(!(std::is_reference_v<_Types> || ...),
  1112. "variant must have no reference alternative");
  1113. static_assert(!(std::is_void_v<_Types> || ...),
  1114. "variant must have no void alternative");
  1115. using _Base = __detail::__variant::_Variant_base<_Types...>;
  1116. using _Default_ctor_enabler =
  1117. _Enable_default_constructor<
  1118. __detail::__variant::_Traits<_Types...>::_S_default_ctor,
  1119. variant<_Types...>>;
  1120. template<typename _Tp>
  1121. static constexpr bool __not_self
  1122. = !is_same_v<__remove_cvref_t<_Tp>, variant>;
  1123. template<typename _Tp>
  1124. static constexpr bool
  1125. __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
  1126. template<typename _Tp>
  1127. static constexpr size_t __accepted_index
  1128. = __detail::__variant::__accepted_index<_Tp, variant>::value;
  1129. template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
  1130. using __to_type = variant_alternative_t<_Np, variant>;
  1131. template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
  1132. using __accepted_type = __to_type<__accepted_index<_Tp>>;
  1133. template<typename _Tp>
  1134. static constexpr size_t __index_of =
  1135. __detail::__variant::__index_of_v<_Tp, _Types...>;
  1136. using _Traits = __detail::__variant::_Traits<_Types...>;
  1137. template<typename _Tp>
  1138. struct __is_in_place_tag : false_type { };
  1139. template<typename _Tp>
  1140. struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
  1141. template<size_t _Np>
  1142. struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
  1143. template<typename _Tp>
  1144. static constexpr bool __not_in_place_tag
  1145. = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
  1146. public:
  1147. variant() = default;
  1148. variant(const variant& __rhs) = default;
  1149. variant(variant&&) = default;
  1150. variant& operator=(const variant&) = default;
  1151. variant& operator=(variant&&) = default;
  1152. ~variant() = default;
  1153. template<typename _Tp,
  1154. typename = enable_if_t<sizeof...(_Types) != 0>,
  1155. typename = enable_if_t<__not_in_place_tag<_Tp>>,
  1156. typename _Tj = __accepted_type<_Tp&&>,
  1157. typename = enable_if_t<__exactly_once<_Tj>
  1158. && is_constructible_v<_Tj, _Tp>>>
  1159. constexpr
  1160. variant(_Tp&& __t)
  1161. noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
  1162. : variant(in_place_index<__accepted_index<_Tp>>,
  1163. std::forward<_Tp>(__t))
  1164. { }
  1165. template<typename _Tp, typename... _Args,
  1166. typename = enable_if_t<__exactly_once<_Tp>
  1167. && is_constructible_v<_Tp, _Args...>>>
  1168. constexpr explicit
  1169. variant(in_place_type_t<_Tp>, _Args&&... __args)
  1170. : variant(in_place_index<__index_of<_Tp>>,
  1171. std::forward<_Args>(__args)...)
  1172. { }
  1173. template<typename _Tp, typename _Up, typename... _Args,
  1174. typename = enable_if_t<__exactly_once<_Tp>
  1175. && is_constructible_v<_Tp,
  1176. initializer_list<_Up>&, _Args...>>>
  1177. constexpr explicit
  1178. variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
  1179. _Args&&... __args)
  1180. : variant(in_place_index<__index_of<_Tp>>, __il,
  1181. std::forward<_Args>(__args)...)
  1182. { }
  1183. template<size_t _Np, typename... _Args,
  1184. typename _Tp = __to_type<_Np>,
  1185. typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
  1186. constexpr explicit
  1187. variant(in_place_index_t<_Np>, _Args&&... __args)
  1188. : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
  1189. _Default_ctor_enabler(_Enable_default_constructor_tag{})
  1190. { }
  1191. template<size_t _Np, typename _Up, typename... _Args,
  1192. typename _Tp = __to_type<_Np>,
  1193. typename = enable_if_t<is_constructible_v<_Tp,
  1194. initializer_list<_Up>&,
  1195. _Args...>>>
  1196. constexpr explicit
  1197. variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
  1198. _Args&&... __args)
  1199. : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
  1200. _Default_ctor_enabler(_Enable_default_constructor_tag{})
  1201. { }
  1202. template<typename _Tp>
  1203. enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
  1204. && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
  1205. && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
  1206. variant&>
  1207. operator=(_Tp&& __rhs)
  1208. noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
  1209. && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
  1210. {
  1211. constexpr auto __index = __accepted_index<_Tp>;
  1212. if (index() == __index)
  1213. std::get<__index>(*this) = std::forward<_Tp>(__rhs);
  1214. else
  1215. {
  1216. using _Tj = __accepted_type<_Tp&&>;
  1217. if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
  1218. || !is_nothrow_move_constructible_v<_Tj>)
  1219. this->emplace<__index>(std::forward<_Tp>(__rhs));
  1220. else
  1221. operator=(variant(std::forward<_Tp>(__rhs)));
  1222. }
  1223. return *this;
  1224. }
  1225. template<typename _Tp, typename... _Args>
  1226. enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
  1227. _Tp&>
  1228. emplace(_Args&&... __args)
  1229. {
  1230. constexpr size_t __index = __index_of<_Tp>;
  1231. return this->emplace<__index>(std::forward<_Args>(__args)...);
  1232. }
  1233. template<typename _Tp, typename _Up, typename... _Args>
  1234. enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
  1235. && __exactly_once<_Tp>,
  1236. _Tp&>
  1237. emplace(initializer_list<_Up> __il, _Args&&... __args)
  1238. {
  1239. constexpr size_t __index = __index_of<_Tp>;
  1240. return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
  1241. }
  1242. template<size_t _Np, typename... _Args>
  1243. enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
  1244. _Args...>,
  1245. variant_alternative_t<_Np, variant>&>
  1246. emplace(_Args&&... __args)
  1247. {
  1248. static_assert(_Np < sizeof...(_Types),
  1249. "The index must be in [0, number of alternatives)");
  1250. using type = variant_alternative_t<_Np, variant>;
  1251. // Provide the strong exception-safety guarantee when possible,
  1252. // to avoid becoming valueless.
  1253. if constexpr (is_nothrow_constructible_v<type, _Args...>)
  1254. {
  1255. this->_M_reset();
  1256. __variant_construct_by_index<_Np>(*this,
  1257. std::forward<_Args>(__args)...);
  1258. }
  1259. else if constexpr (is_scalar_v<type>)
  1260. {
  1261. // This might invoke a potentially-throwing conversion operator:
  1262. const type __tmp(std::forward<_Args>(__args)...);
  1263. // But these steps won't throw:
  1264. this->_M_reset();
  1265. __variant_construct_by_index<_Np>(*this, __tmp);
  1266. }
  1267. else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
  1268. && _Traits::_S_move_assign)
  1269. {
  1270. // This construction might throw:
  1271. variant __tmp(in_place_index<_Np>,
  1272. std::forward<_Args>(__args)...);
  1273. // But _Never_valueless_alt<type> means this won't:
  1274. *this = std::move(__tmp);
  1275. }
  1276. else
  1277. {
  1278. // This case only provides the basic exception-safety guarantee,
  1279. // i.e. the variant can become valueless.
  1280. this->_M_reset();
  1281. __try
  1282. {
  1283. __variant_construct_by_index<_Np>(*this,
  1284. std::forward<_Args>(__args)...);
  1285. }
  1286. __catch (...)
  1287. {
  1288. this->_M_index = variant_npos;
  1289. __throw_exception_again;
  1290. }
  1291. }
  1292. return std::get<_Np>(*this);
  1293. }
  1294. template<size_t _Np, typename _Up, typename... _Args>
  1295. enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
  1296. initializer_list<_Up>&, _Args...>,
  1297. variant_alternative_t<_Np, variant>&>
  1298. emplace(initializer_list<_Up> __il, _Args&&... __args)
  1299. {
  1300. static_assert(_Np < sizeof...(_Types),
  1301. "The index must be in [0, number of alternatives)");
  1302. using type = variant_alternative_t<_Np, variant>;
  1303. // Provide the strong exception-safety guarantee when possible,
  1304. // to avoid becoming valueless.
  1305. if constexpr (is_nothrow_constructible_v<type,
  1306. initializer_list<_Up>&,
  1307. _Args...>)
  1308. {
  1309. this->_M_reset();
  1310. __variant_construct_by_index<_Np>(*this, __il,
  1311. std::forward<_Args>(__args)...);
  1312. }
  1313. else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
  1314. && _Traits::_S_move_assign)
  1315. {
  1316. // This construction might throw:
  1317. variant __tmp(in_place_index<_Np>, __il,
  1318. std::forward<_Args>(__args)...);
  1319. // But _Never_valueless_alt<type> means this won't:
  1320. *this = std::move(__tmp);
  1321. }
  1322. else
  1323. {
  1324. // This case only provides the basic exception-safety guarantee,
  1325. // i.e. the variant can become valueless.
  1326. this->_M_reset();
  1327. __try
  1328. {
  1329. __variant_construct_by_index<_Np>(*this, __il,
  1330. std::forward<_Args>(__args)...);
  1331. }
  1332. __catch (...)
  1333. {
  1334. this->_M_index = variant_npos;
  1335. __throw_exception_again;
  1336. }
  1337. }
  1338. return std::get<_Np>(*this);
  1339. }
  1340. constexpr bool valueless_by_exception() const noexcept
  1341. { return !this->_M_valid(); }
  1342. constexpr size_t index() const noexcept
  1343. {
  1344. using __index_type = typename _Base::__index_type;
  1345. if constexpr (__detail::__variant::__never_valueless<_Types...>())
  1346. return this->_M_index;
  1347. else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
  1348. return make_signed_t<__index_type>(this->_M_index);
  1349. else
  1350. return size_t(__index_type(this->_M_index + 1)) - 1;
  1351. }
  1352. void
  1353. swap(variant& __rhs)
  1354. noexcept((__is_nothrow_swappable<_Types>::value && ...)
  1355. && is_nothrow_move_constructible_v<variant>)
  1356. {
  1357. __detail::__variant::__raw_idx_visit(
  1358. [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
  1359. {
  1360. if constexpr (__rhs_index != variant_npos)
  1361. {
  1362. if (this->index() == __rhs_index)
  1363. {
  1364. auto& __this_mem =
  1365. std::get<__rhs_index>(*this);
  1366. using std::swap;
  1367. swap(__this_mem, __rhs_mem);
  1368. }
  1369. else
  1370. {
  1371. if (!this->valueless_by_exception()) [[__likely__]]
  1372. {
  1373. auto __tmp(std::move(__rhs_mem));
  1374. __rhs = std::move(*this);
  1375. this->_M_destructive_move(__rhs_index,
  1376. std::move(__tmp));
  1377. }
  1378. else
  1379. {
  1380. this->_M_destructive_move(__rhs_index,
  1381. std::move(__rhs_mem));
  1382. __rhs._M_reset();
  1383. }
  1384. }
  1385. }
  1386. else
  1387. {
  1388. if (!this->valueless_by_exception()) [[__likely__]]
  1389. {
  1390. __rhs = std::move(*this);
  1391. this->_M_reset();
  1392. }
  1393. }
  1394. }, __rhs);
  1395. }
  1396. private:
  1397. #if defined(__clang__) && __clang_major__ <= 7
  1398. public:
  1399. using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
  1400. private:
  1401. #endif
  1402. template<size_t _Np, typename _Vp>
  1403. friend constexpr decltype(auto)
  1404. __detail::__variant::__get(_Vp&& __v) noexcept;
  1405. template<typename _Vp>
  1406. friend void*
  1407. __detail::__variant::__get_storage(_Vp&& __v) noexcept;
  1408. #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
  1409. template<typename... _Tp> \
  1410. friend constexpr bool \
  1411. operator __OP(const variant<_Tp...>& __lhs, \
  1412. const variant<_Tp...>& __rhs);
  1413. _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
  1414. _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
  1415. _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
  1416. _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
  1417. _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
  1418. _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
  1419. #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
  1420. };
  1421. template<size_t _Np, typename... _Types>
  1422. constexpr variant_alternative_t<_Np, variant<_Types...>>&
  1423. get(variant<_Types...>& __v)
  1424. {
  1425. static_assert(_Np < sizeof...(_Types),
  1426. "The index must be in [0, number of alternatives)");
  1427. if (__v.index() != _Np)
  1428. __throw_bad_variant_access(__v.valueless_by_exception());
  1429. return __detail::__variant::__get<_Np>(__v);
  1430. }
  1431. template<size_t _Np, typename... _Types>
  1432. constexpr variant_alternative_t<_Np, variant<_Types...>>&&
  1433. get(variant<_Types...>&& __v)
  1434. {
  1435. static_assert(_Np < sizeof...(_Types),
  1436. "The index must be in [0, number of alternatives)");
  1437. if (__v.index() != _Np)
  1438. __throw_bad_variant_access(__v.valueless_by_exception());
  1439. return __detail::__variant::__get<_Np>(std::move(__v));
  1440. }
  1441. template<size_t _Np, typename... _Types>
  1442. constexpr const variant_alternative_t<_Np, variant<_Types...>>&
  1443. get(const variant<_Types...>& __v)
  1444. {
  1445. static_assert(_Np < sizeof...(_Types),
  1446. "The index must be in [0, number of alternatives)");
  1447. if (__v.index() != _Np)
  1448. __throw_bad_variant_access(__v.valueless_by_exception());
  1449. return __detail::__variant::__get<_Np>(__v);
  1450. }
  1451. template<size_t _Np, typename... _Types>
  1452. constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
  1453. get(const variant<_Types...>&& __v)
  1454. {
  1455. static_assert(_Np < sizeof...(_Types),
  1456. "The index must be in [0, number of alternatives)");
  1457. if (__v.index() != _Np)
  1458. __throw_bad_variant_access(__v.valueless_by_exception());
  1459. return __detail::__variant::__get<_Np>(std::move(__v));
  1460. }
  1461. template<typename _Result_type, typename _Visitor, typename... _Variants>
  1462. constexpr decltype(auto)
  1463. __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
  1464. {
  1465. constexpr auto& __vtable = __detail::__variant::__gen_vtable<
  1466. _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
  1467. auto __func_ptr = __vtable._M_access(__variants.index()...);
  1468. return (*__func_ptr)(std::forward<_Visitor>(__visitor),
  1469. std::forward<_Variants>(__variants)...);
  1470. }
  1471. template<typename _Visitor, typename... _Variants>
  1472. constexpr decltype(auto)
  1473. visit(_Visitor&& __visitor, _Variants&&... __variants)
  1474. {
  1475. if ((__variants.valueless_by_exception() || ...))
  1476. __throw_bad_variant_access("std::visit: variant is valueless");
  1477. using _Result_type = std::invoke_result_t<_Visitor,
  1478. decltype(std::get<0>(std::declval<_Variants>()))...>;
  1479. using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
  1480. return std::__do_visit<_Tag>(std::forward<_Visitor>(__visitor),
  1481. std::forward<_Variants>(__variants)...);
  1482. }
  1483. #if __cplusplus > 201703L
  1484. template<typename _Res, typename _Visitor, typename... _Variants>
  1485. constexpr _Res
  1486. visit(_Visitor&& __visitor, _Variants&&... __variants)
  1487. {
  1488. if ((__variants.valueless_by_exception() || ...))
  1489. __throw_bad_variant_access("std::visit<R>: variant is valueless");
  1490. return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
  1491. std::forward<_Variants>(__variants)...);
  1492. }
  1493. #endif
  1494. template<bool, typename... _Types>
  1495. struct __variant_hash_call_base_impl
  1496. {
  1497. size_t
  1498. operator()(const variant<_Types...>& __t) const
  1499. noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
  1500. {
  1501. size_t __ret;
  1502. __detail::__variant::__raw_visit(
  1503. [&__t, &__ret](auto&& __t_mem) mutable
  1504. {
  1505. using _Type = __remove_cvref_t<decltype(__t_mem)>;
  1506. if constexpr (!is_same_v<_Type,
  1507. __detail::__variant::__variant_cookie>)
  1508. __ret = std::hash<size_t>{}(__t.index())
  1509. + std::hash<_Type>{}(__t_mem);
  1510. else
  1511. __ret = std::hash<size_t>{}(__t.index());
  1512. }, __t);
  1513. return __ret;
  1514. }
  1515. };
  1516. template<typename... _Types>
  1517. struct __variant_hash_call_base_impl<false, _Types...> {};
  1518. template<typename... _Types>
  1519. using __variant_hash_call_base =
  1520. __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
  1521. __enable_hash_call &&...), _Types...>;
  1522. template<typename... _Types>
  1523. struct hash<variant<_Types...>>
  1524. : private __detail::__variant::_Variant_hash_base<
  1525. variant<_Types...>, std::index_sequence_for<_Types...>>,
  1526. public __variant_hash_call_base<_Types...>
  1527. {
  1528. using result_type [[__deprecated__]] = size_t;
  1529. using argument_type [[__deprecated__]] = variant<_Types...>;
  1530. };
  1531. template<>
  1532. struct hash<monostate>
  1533. {
  1534. using result_type [[__deprecated__]] = size_t;
  1535. using argument_type [[__deprecated__]] = monostate;
  1536. size_t
  1537. operator()(const monostate&) const noexcept
  1538. {
  1539. constexpr size_t __magic_monostate_hash = -7777;
  1540. return __magic_monostate_hash;
  1541. }
  1542. };
  1543. template<typename... _Types>
  1544. struct __is_fast_hash<hash<variant<_Types...>>>
  1545. : bool_constant<(__is_fast_hash<_Types>::value && ...)>
  1546. { };
  1547. _GLIBCXX_END_NAMESPACE_VERSION
  1548. } // namespace std
  1549. #endif // C++17
  1550. #endif // _GLIBCXX_VARIANT