variant 59 KB

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