variant 61 KB

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