variant 64 KB

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