variant 57 KB

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