variant 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_VARIANT
  10. #define _LIBCPP_VARIANT
  11. /*
  12. variant synopsis
  13. namespace std {
  14. // 20.7.2, class template variant
  15. template <class... Types>
  16. class variant {
  17. public:
  18. // 20.7.2.1, constructors
  19. constexpr variant() noexcept(see below);
  20. constexpr variant(const variant&);
  21. constexpr variant(variant&&) noexcept(see below);
  22. template <class T> constexpr variant(T&&) noexcept(see below);
  23. template <class T, class... Args>
  24. constexpr explicit variant(in_place_type_t<T>, Args&&...);
  25. template <class T, class U, class... Args>
  26. constexpr explicit variant(
  27. in_place_type_t<T>, initializer_list<U>, Args&&...);
  28. template <size_t I, class... Args>
  29. constexpr explicit variant(in_place_index_t<I>, Args&&...);
  30. template <size_t I, class U, class... Args>
  31. constexpr explicit variant(
  32. in_place_index_t<I>, initializer_list<U>, Args&&...);
  33. // 20.7.2.2, destructor
  34. ~variant();
  35. // 20.7.2.3, assignment
  36. constexpr variant& operator=(const variant&);
  37. constexpr variant& operator=(variant&&) noexcept(see below);
  38. template <class T> variant& operator=(T&&) noexcept(see below);
  39. // 20.7.2.4, modifiers
  40. template <class T, class... Args>
  41. T& emplace(Args&&...);
  42. template <class T, class U, class... Args>
  43. T& emplace(initializer_list<U>, Args&&...);
  44. template <size_t I, class... Args>
  45. variant_alternative_t<I, variant>& emplace(Args&&...);
  46. template <size_t I, class U, class... Args>
  47. variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
  48. // 20.7.2.5, value status
  49. constexpr bool valueless_by_exception() const noexcept;
  50. constexpr size_t index() const noexcept;
  51. // 20.7.2.6, swap
  52. void swap(variant&) noexcept(see below);
  53. // [variant.visit], visitation
  54. template<class Self, class Visitor>
  55. constexpr decltype(auto) visit(this Self&&, Visitor&&); // Since C++26
  56. template<class R, class Self, class Visitor>
  57. constexpr R visit(this Self&&, Visitor&&); // Since C++26
  58. };
  59. // 20.7.3, variant helper classes
  60. template <class T> struct variant_size; // undefined
  61. template <class T>
  62. inline constexpr size_t variant_size_v = variant_size<T>::value;
  63. template <class T> struct variant_size<const T>;
  64. template <class T> struct variant_size<volatile T>;
  65. template <class T> struct variant_size<const volatile T>;
  66. template <class... Types>
  67. struct variant_size<variant<Types...>>;
  68. template <size_t I, class T> struct variant_alternative; // undefined
  69. template <size_t I, class T>
  70. using variant_alternative_t = typename variant_alternative<I, T>::type;
  71. template <size_t I, class T> struct variant_alternative<I, const T>;
  72. template <size_t I, class T> struct variant_alternative<I, volatile T>;
  73. template <size_t I, class T> struct variant_alternative<I, const volatile T>;
  74. template <size_t I, class... Types>
  75. struct variant_alternative<I, variant<Types...>>;
  76. inline constexpr size_t variant_npos = -1;
  77. // 20.7.4, value access
  78. template <class T, class... Types>
  79. constexpr bool holds_alternative(const variant<Types...>&) noexcept;
  80. template <size_t I, class... Types>
  81. constexpr variant_alternative_t<I, variant<Types...>>&
  82. get(variant<Types...>&);
  83. template <size_t I, class... Types>
  84. constexpr variant_alternative_t<I, variant<Types...>>&&
  85. get(variant<Types...>&&);
  86. template <size_t I, class... Types>
  87. constexpr variant_alternative_t<I, variant<Types...>> const&
  88. get(const variant<Types...>&);
  89. template <size_t I, class... Types>
  90. constexpr variant_alternative_t<I, variant<Types...>> const&&
  91. get(const variant<Types...>&&);
  92. template <class T, class... Types>
  93. constexpr T& get(variant<Types...>&);
  94. template <class T, class... Types>
  95. constexpr T&& get(variant<Types...>&&);
  96. template <class T, class... Types>
  97. constexpr const T& get(const variant<Types...>&);
  98. template <class T, class... Types>
  99. constexpr const T&& get(const variant<Types...>&&);
  100. template <size_t I, class... Types>
  101. constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
  102. get_if(variant<Types...>*) noexcept;
  103. template <size_t I, class... Types>
  104. constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
  105. get_if(const variant<Types...>*) noexcept;
  106. template <class T, class... Types>
  107. constexpr add_pointer_t<T>
  108. get_if(variant<Types...>*) noexcept;
  109. template <class T, class... Types>
  110. constexpr add_pointer_t<const T>
  111. get_if(const variant<Types...>*) noexcept;
  112. // 20.7.5, relational operators
  113. template <class... Types>
  114. constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
  115. template <class... Types>
  116. constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
  117. template <class... Types>
  118. constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
  119. template <class... Types>
  120. constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
  121. template <class... Types>
  122. constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
  123. template <class... Types>
  124. constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
  125. template <class... Types> requires (three_way_comparable<Types> && ...)
  126. constexpr common_comparison_category_t<compare_three_way_result_t<Types>...>
  127. operator<=>(const variant<Types...>&, const variant<Types...>&); // since C++20
  128. // 20.7.6, visitation
  129. template <class Visitor, class... Variants>
  130. constexpr see below visit(Visitor&&, Variants&&...);
  131. template <class R, class Visitor, class... Variants>
  132. constexpr R visit(Visitor&&, Variants&&...); // since C++20
  133. // 20.7.7, class monostate
  134. struct monostate;
  135. // 20.7.8, monostate relational operators
  136. constexpr bool operator==(monostate, monostate) noexcept;
  137. constexpr bool operator!=(monostate, monostate) noexcept; // until C++20
  138. constexpr bool operator<(monostate, monostate) noexcept; // until C++20
  139. constexpr bool operator>(monostate, monostate) noexcept; // until C++20
  140. constexpr bool operator<=(monostate, monostate) noexcept; // until C++20
  141. constexpr bool operator>=(monostate, monostate) noexcept; // until C++20
  142. constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // since C++20
  143. // 20.7.9, specialized algorithms
  144. template <class... Types>
  145. void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
  146. // 20.7.10, class bad_variant_access
  147. class bad_variant_access;
  148. // 20.7.11, hash support
  149. template <class T> struct hash;
  150. template <class... Types> struct hash<variant<Types...>>;
  151. template <> struct hash<monostate>;
  152. } // namespace std
  153. */
  154. #include <__availability>
  155. #include <__compare/common_comparison_category.h>
  156. #include <__compare/compare_three_way_result.h>
  157. #include <__compare/three_way_comparable.h>
  158. #include <__config>
  159. #include <__exception/exception.h>
  160. #include <__functional/hash.h>
  161. #include <__functional/invoke.h>
  162. #include <__functional/operations.h>
  163. #include <__functional/unary_function.h>
  164. #include <__memory/addressof.h>
  165. #include <__type_traits/add_const.h>
  166. #include <__type_traits/add_cv.h>
  167. #include <__type_traits/add_pointer.h>
  168. #include <__type_traits/add_volatile.h>
  169. #include <__type_traits/dependent_type.h>
  170. #include <__type_traits/is_array.h>
  171. #include <__type_traits/is_destructible.h>
  172. #include <__type_traits/is_nothrow_move_constructible.h>
  173. #include <__type_traits/is_trivially_copy_assignable.h>
  174. #include <__type_traits/is_trivially_copy_constructible.h>
  175. #include <__type_traits/is_trivially_destructible.h>
  176. #include <__type_traits/is_trivially_move_assignable.h>
  177. #include <__type_traits/is_trivially_move_constructible.h>
  178. #include <__type_traits/is_void.h>
  179. #include <__type_traits/remove_const.h>
  180. #include <__type_traits/type_identity.h>
  181. #include <__type_traits/void_t.h>
  182. #include <__utility/declval.h>
  183. #include <__utility/forward.h>
  184. #include <__utility/forward_like.h>
  185. #include <__utility/in_place.h>
  186. #include <__utility/move.h>
  187. #include <__utility/swap.h>
  188. #include <__variant/monostate.h>
  189. #include <__verbose_abort>
  190. #include <initializer_list>
  191. #include <limits>
  192. #include <new>
  193. #include <tuple>
  194. #include <version>
  195. // standard-mandated includes
  196. // [variant.syn]
  197. #include <compare>
  198. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  199. # pragma GCC system_header
  200. #endif
  201. _LIBCPP_PUSH_MACROS
  202. #include <__undef_macros>
  203. namespace std { // explicitly not using versioning namespace
  204. class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
  205. public:
  206. const char* what() const _NOEXCEPT override;
  207. };
  208. } // namespace std
  209. _LIBCPP_BEGIN_NAMESPACE_STD
  210. #if _LIBCPP_STD_VER >= 17
  211. // Light N-dimensional array of function pointers. Used in place of std::array to avoid
  212. // adding a dependency.
  213. template <class _Tp, size_t _Size>
  214. struct __farray {
  215. static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
  216. _Tp __buf_[_Size] = {};
  217. _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t __n) const noexcept { return __buf_[__n]; }
  218. };
  219. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS void
  220. __throw_bad_variant_access() {
  221. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  222. throw bad_variant_access();
  223. # else
  224. _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode");
  225. # endif
  226. }
  227. template <class... _Types>
  228. class _LIBCPP_TEMPLATE_VIS variant;
  229. template <class _Tp>
  230. struct _LIBCPP_TEMPLATE_VIS variant_size;
  231. template <class _Tp>
  232. inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
  233. template <class _Tp>
  234. struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
  235. template <class _Tp>
  236. struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
  237. template <class _Tp>
  238. struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> : variant_size<_Tp> {};
  239. template <class... _Types>
  240. struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> : integral_constant<size_t, sizeof...(_Types)> {};
  241. template <size_t _Ip, class _Tp>
  242. struct _LIBCPP_TEMPLATE_VIS variant_alternative;
  243. template <size_t _Ip, class _Tp>
  244. using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
  245. template <size_t _Ip, class _Tp>
  246. struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> : add_const<variant_alternative_t<_Ip, _Tp>> {};
  247. template <size_t _Ip, class _Tp>
  248. struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
  249. template <size_t _Ip, class _Tp>
  250. struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> : add_cv<variant_alternative_t<_Ip, _Tp>> {};
  251. template <size_t _Ip, class... _Types>
  252. struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
  253. static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
  254. using type = __type_pack_element<_Ip, _Types...>;
  255. };
  256. inline constexpr size_t variant_npos = static_cast<size_t>(-1);
  257. _LIBCPP_HIDE_FROM_ABI constexpr int __choose_index_type(unsigned int __num_elem) {
  258. if (__num_elem < numeric_limits<unsigned char>::max())
  259. return 0;
  260. if (__num_elem < numeric_limits<unsigned short>::max())
  261. return 1;
  262. return 2;
  263. }
  264. template <size_t _NumAlts>
  265. using __variant_index_t =
  266. # ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
  267. unsigned int;
  268. # else
  269. std::tuple_element_t< __choose_index_type(_NumAlts), std::tuple<unsigned char, unsigned short, unsigned int> >;
  270. # endif
  271. template <class _IndexType>
  272. constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
  273. template <class... _Types>
  274. class _LIBCPP_TEMPLATE_VIS variant;
  275. template <class... _Types>
  276. _LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& __as_variant(variant<_Types...>& __vs) noexcept {
  277. return __vs;
  278. }
  279. template <class... _Types>
  280. _LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>& __as_variant(const variant<_Types...>& __vs) noexcept {
  281. return __vs;
  282. }
  283. template <class... _Types>
  284. _LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&& __as_variant(variant<_Types...>&& __vs) noexcept {
  285. return std::move(__vs);
  286. }
  287. template <class... _Types>
  288. _LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&& __as_variant(const variant<_Types...>&& __vs) noexcept {
  289. return std::move(__vs);
  290. }
  291. namespace __find_detail {
  292. template <class _Tp, class... _Types>
  293. _LIBCPP_HIDE_FROM_ABI constexpr size_t __find_index() {
  294. constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
  295. size_t __result = __not_found;
  296. for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
  297. if (__matches[__i]) {
  298. if (__result != __not_found) {
  299. return __ambiguous;
  300. }
  301. __result = __i;
  302. }
  303. }
  304. return __result;
  305. }
  306. template <size_t _Index>
  307. struct __find_unambiguous_index_sfinae_impl : integral_constant<size_t, _Index> {};
  308. template <>
  309. struct __find_unambiguous_index_sfinae_impl<__not_found> {};
  310. template <>
  311. struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
  312. template <class _Tp, class... _Types>
  313. struct __find_unambiguous_index_sfinae : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
  314. } // namespace __find_detail
  315. namespace __variant_detail {
  316. struct __valueless_t {};
  317. enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
  318. template <typename _Tp, template <typename> class _IsTriviallyAvailable, template <typename> class _IsAvailable>
  319. constexpr _Trait __trait =
  320. _IsTriviallyAvailable<_Tp>::value ? _Trait::_TriviallyAvailable
  321. : _IsAvailable<_Tp>::value
  322. ? _Trait::_Available
  323. : _Trait::_Unavailable;
  324. _LIBCPP_HIDE_FROM_ABI constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
  325. _Trait __result = _Trait::_TriviallyAvailable;
  326. for (_Trait __t : __traits) {
  327. if (static_cast<int>(__t) > static_cast<int>(__result)) {
  328. __result = __t;
  329. }
  330. }
  331. return __result;
  332. }
  333. template <typename... _Types>
  334. struct __traits {
  335. static constexpr _Trait __copy_constructible_trait =
  336. __variant_detail::__common_trait({__trait<_Types, is_trivially_copy_constructible, is_copy_constructible>...});
  337. static constexpr _Trait __move_constructible_trait =
  338. __variant_detail::__common_trait({__trait<_Types, is_trivially_move_constructible, is_move_constructible>...});
  339. static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait(
  340. {__copy_constructible_trait, __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
  341. static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait(
  342. {__move_constructible_trait, __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
  343. static constexpr _Trait __destructible_trait =
  344. __variant_detail::__common_trait({__trait<_Types, is_trivially_destructible, is_destructible>...});
  345. };
  346. namespace __access {
  347. struct __union {
  348. template <class _Vp>
  349. _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
  350. return std::forward<_Vp>(__v).__head;
  351. }
  352. template <class _Vp, size_t _Ip>
  353. _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
  354. return __get_alt(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
  355. }
  356. };
  357. struct __base {
  358. template <size_t _Ip, class _Vp>
  359. _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
  360. return __union::__get_alt(std::forward<_Vp>(__v).__data, in_place_index<_Ip>);
  361. }
  362. };
  363. struct __variant {
  364. template <size_t _Ip, class _Vp>
  365. _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
  366. return __base::__get_alt<_Ip>(std::forward<_Vp>(__v).__impl_);
  367. }
  368. };
  369. } // namespace __access
  370. namespace __visitation {
  371. struct __base {
  372. template <class _Visitor, class... _Vs>
  373. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
  374. __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
  375. constexpr auto __fdiagonal = __make_fdiagonal<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
  376. return __fdiagonal[__index](std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
  377. }
  378. template <class _Visitor, class... _Vs>
  379. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
  380. constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
  381. return __at(__fmatrix, __vs.index()...)(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
  382. }
  383. private:
  384. template <class _Tp>
  385. _LIBCPP_HIDE_FROM_ABI static constexpr const _Tp& __at(const _Tp& __elem) {
  386. return __elem;
  387. }
  388. template <class _Tp, size_t _Np, typename... _Indices>
  389. _LIBCPP_HIDE_FROM_ABI static constexpr auto&&
  390. __at(const __farray<_Tp, _Np>& __elems, size_t __index, _Indices... __indices) {
  391. return __at(__elems[__index], __indices...);
  392. }
  393. template <class _Fp, class... _Fs>
  394. static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() {
  395. static_assert(
  396. __all<is_same_v<_Fp, _Fs>...>::value, "`std::visit` requires the visitor to have a single return type.");
  397. }
  398. template <class... _Fs>
  399. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_farray(_Fs&&... __fs) {
  400. __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>();
  401. using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>;
  402. return __result{{std::forward<_Fs>(__fs)...}};
  403. }
  404. template <size_t... _Is>
  405. struct __dispatcher {
  406. template <class _Fp, class... _Vs>
  407. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
  408. return std::__invoke(static_cast<_Fp>(__f), __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
  409. }
  410. };
  411. template <class _Fp, class... _Vs, size_t... _Is>
  412. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_dispatch(index_sequence<_Is...>) {
  413. return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
  414. }
  415. template <size_t _Ip, class _Fp, class... _Vs>
  416. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl() {
  417. return __make_dispatch<_Fp, _Vs...>(index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
  418. }
  419. template <class _Fp, class... _Vs, size_t... _Is>
  420. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
  421. return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
  422. }
  423. template <class _Fp, class _Vp, class... _Vs>
  424. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal() {
  425. constexpr size_t __np = __remove_cvref_t<_Vp>::__size();
  426. static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value);
  427. return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{});
  428. }
  429. template <class _Fp, class... _Vs, size_t... _Is>
  430. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
  431. return __make_dispatch<_Fp, _Vs...>(__is);
  432. }
  433. template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
  434. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  435. __make_fmatrix_impl(index_sequence<_Is...>, index_sequence<_Js...>, _Ls... __ls) {
  436. return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(index_sequence<_Is..., _Js>{}, __ls...)...);
  437. }
  438. template <class _Fp, class... _Vs>
  439. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix() {
  440. return __make_fmatrix_impl<_Fp, _Vs...>(
  441. index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...);
  442. }
  443. };
  444. struct __variant {
  445. template <class _Visitor, class... _Vs>
  446. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
  447. __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
  448. return __base::__visit_alt_at(__index, std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__impl_...);
  449. }
  450. template <class _Visitor, class... _Vs>
  451. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
  452. return __base::__visit_alt(
  453. std::forward<_Visitor>(__visitor), std::__as_variant(std::forward<_Vs>(__vs)).__impl_...);
  454. }
  455. template <class _Visitor, class... _Vs>
  456. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
  457. __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
  458. return __visit_alt_at(__index, __make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
  459. }
  460. template <class _Visitor, class... _Vs>
  461. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
  462. return __visit_alt(__make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
  463. }
  464. # if _LIBCPP_STD_VER >= 20
  465. template <class _Rp, class _Visitor, class... _Vs>
  466. _LIBCPP_HIDE_FROM_ABI static constexpr _Rp __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
  467. return __visit_alt(__make_value_visitor<_Rp>(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
  468. }
  469. # endif
  470. private:
  471. template <class _Visitor, class... _Values>
  472. static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() {
  473. static_assert(is_invocable_v<_Visitor, _Values...>, "`std::visit` requires the visitor to be exhaustive.");
  474. }
  475. template <class _Visitor>
  476. struct __value_visitor {
  477. template <class... _Alts>
  478. _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Alts&&... __alts) const {
  479. __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
  480. return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
  481. }
  482. _Visitor&& __visitor;
  483. };
  484. # if _LIBCPP_STD_VER >= 20
  485. template <class _Rp, class _Visitor>
  486. struct __value_visitor_return_type {
  487. template <class... _Alts>
  488. _LIBCPP_HIDE_FROM_ABI constexpr _Rp operator()(_Alts&&... __alts) const {
  489. __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
  490. if constexpr (is_void_v<_Rp>) {
  491. std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
  492. } else {
  493. return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
  494. }
  495. }
  496. _Visitor&& __visitor;
  497. };
  498. # endif
  499. template <class _Visitor>
  500. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
  501. return __value_visitor<_Visitor>{std::forward<_Visitor>(__visitor)};
  502. }
  503. # if _LIBCPP_STD_VER >= 20
  504. template <class _Rp, class _Visitor>
  505. _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
  506. return __value_visitor_return_type<_Rp, _Visitor>{std::forward<_Visitor>(__visitor)};
  507. }
  508. # endif
  509. };
  510. } // namespace __visitation
  511. template <size_t _Index, class _Tp>
  512. struct _LIBCPP_TEMPLATE_VIS __alt {
  513. using __value_type = _Tp;
  514. template <class... _Args>
  515. _LIBCPP_HIDE_FROM_ABI explicit constexpr __alt(in_place_t, _Args&&... __args)
  516. : __value(std::forward<_Args>(__args)...) {}
  517. __value_type __value;
  518. };
  519. template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
  520. union _LIBCPP_TEMPLATE_VIS __union;
  521. template <_Trait _DestructibleTrait, size_t _Index>
  522. union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
  523. # define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
  524. template <size_t _Index, class _Tp, class... _Types> \
  525. union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, _Index, _Tp, _Types...> { \
  526. public: \
  527. _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
  528. \
  529. template <class... _Args> \
  530. _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
  531. : __head(in_place, std::forward<_Args>(__args)...) {} \
  532. \
  533. template <size_t _Ip, class... _Args> \
  534. _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
  535. : __tail(in_place_index<_Ip - 1>, std::forward<_Args>(__args)...) {} \
  536. \
  537. __union(const __union&) = default; \
  538. __union(__union&&) = default; \
  539. \
  540. destructor \
  541. \
  542. __union& \
  543. operator=(const __union&) = default; \
  544. __union& operator=(__union&&) = default; \
  545. \
  546. private: \
  547. char __dummy; \
  548. __alt<_Index, _Tp> __head; \
  549. __union<destructible_trait, _Index + 1, _Types...> __tail; \
  550. \
  551. friend struct __access::__union; \
  552. }
  553. _LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
  554. _LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union(){});
  555. _LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
  556. # undef _LIBCPP_VARIANT_UNION
  557. template <_Trait _DestructibleTrait, class... _Types>
  558. class _LIBCPP_TEMPLATE_VIS __base {
  559. public:
  560. using __index_t = __variant_index_t<sizeof...(_Types)>;
  561. _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(__valueless_t __tag) noexcept
  562. : __data(__tag), __index(__variant_npos<__index_t>) {}
  563. template <size_t _Ip, class... _Args>
  564. _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
  565. : __data(in_place_index<_Ip>, std::forward<_Args>(__args)...), __index(_Ip) {}
  566. _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept { return index() == variant_npos; }
  567. _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept {
  568. return __index == __variant_npos<__index_t> ? variant_npos : __index;
  569. }
  570. protected:
  571. _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() & { return *this; }
  572. _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() && { return std::move(*this); }
  573. _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const& { return *this; }
  574. _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const&& { return std::move(*this); }
  575. _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return sizeof...(_Types); }
  576. __union<_DestructibleTrait, 0, _Types...> __data;
  577. __index_t __index;
  578. friend struct __access::__base;
  579. friend struct __visitation::__base;
  580. };
  581. template <class _Traits, _Trait = _Traits::__destructible_trait>
  582. class _LIBCPP_TEMPLATE_VIS __dtor;
  583. # define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
  584. template <class... _Types> \
  585. class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, destructible_trait> \
  586. : public __base<destructible_trait, _Types...> { \
  587. using __base_type = __base<destructible_trait, _Types...>; \
  588. using __index_t = typename __base_type::__index_t; \
  589. \
  590. public: \
  591. using __base_type::__base_type; \
  592. using __base_type::operator=; \
  593. \
  594. __dtor(const __dtor&) = default; \
  595. __dtor(__dtor&&) = default; \
  596. destructor __dtor& operator=(const __dtor&) = default; \
  597. __dtor& operator=(__dtor&&) = default; \
  598. \
  599. protected: \
  600. inline _LIBCPP_HIDE_FROM_ABI destroy \
  601. }
  602. _LIBCPP_VARIANT_DESTRUCTOR(
  603. _Trait::_TriviallyAvailable, ~__dtor() = default;
  604. , void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
  605. _LIBCPP_VARIANT_DESTRUCTOR(
  606. _Trait::_Available,
  607. ~__dtor() { __destroy(); },
  608. void __destroy() noexcept {
  609. if (!this->valueless_by_exception()) {
  610. __visitation::__base::__visit_alt(
  611. [](auto& __alt) noexcept {
  612. using __alt_type = __remove_cvref_t<decltype(__alt)>;
  613. __alt.~__alt_type();
  614. },
  615. *this);
  616. }
  617. this->__index = __variant_npos<__index_t>;
  618. });
  619. _LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable, ~__dtor() = delete;, void __destroy() noexcept = delete;);
  620. # undef _LIBCPP_VARIANT_DESTRUCTOR
  621. template <class _Traits>
  622. class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
  623. using __base_type = __dtor<_Traits>;
  624. public:
  625. using __base_type::__base_type;
  626. using __base_type::operator=;
  627. protected:
  628. template <size_t _Ip, class _Tp, class... _Args>
  629. _LIBCPP_HIDE_FROM_ABI static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
  630. ::new ((void*)std::addressof(__a)) __alt<_Ip, _Tp>(in_place, std::forward<_Args>(__args)...);
  631. return __a.__value;
  632. }
  633. template <class _Rhs>
  634. _LIBCPP_HIDE_FROM_ABI static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
  635. __lhs.__destroy();
  636. if (!__rhs.valueless_by_exception()) {
  637. auto __rhs_index = __rhs.index();
  638. __visitation::__base::__visit_alt_at(
  639. __rhs_index,
  640. [](auto& __lhs_alt, auto&& __rhs_alt) {
  641. __construct_alt(__lhs_alt, std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
  642. },
  643. __lhs,
  644. std::forward<_Rhs>(__rhs));
  645. __lhs.__index = __rhs_index;
  646. }
  647. }
  648. };
  649. template <class _Traits, _Trait = _Traits::__move_constructible_trait>
  650. class _LIBCPP_TEMPLATE_VIS __move_constructor;
  651. # define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, move_constructor) \
  652. template <class... _Types> \
  653. class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, move_constructible_trait> \
  654. : public __ctor<__traits<_Types...>> { \
  655. using __base_type = __ctor<__traits<_Types...>>; \
  656. \
  657. public: \
  658. using __base_type::__base_type; \
  659. using __base_type::operator=; \
  660. \
  661. __move_constructor(const __move_constructor&) = default; \
  662. move_constructor ~__move_constructor() = default; \
  663. __move_constructor& operator=(const __move_constructor&) = default; \
  664. __move_constructor& operator=(__move_constructor&&) = default; \
  665. }
  666. _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_TriviallyAvailable,
  667. __move_constructor(__move_constructor&& __that) = default;);
  668. _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
  669. _Trait::_Available,
  670. __move_constructor(__move_constructor&& __that) noexcept(__all<is_nothrow_move_constructible_v<_Types>...>::value)
  671. : __move_constructor(__valueless_t{}) { this->__generic_construct(*this, std::move(__that)); });
  672. _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_Unavailable, __move_constructor(__move_constructor&&) = delete;);
  673. # undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
  674. template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
  675. class _LIBCPP_TEMPLATE_VIS __copy_constructor;
  676. # define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, copy_constructor) \
  677. template <class... _Types> \
  678. class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, copy_constructible_trait> \
  679. : public __move_constructor<__traits<_Types...>> { \
  680. using __base_type = __move_constructor<__traits<_Types...>>; \
  681. \
  682. public: \
  683. using __base_type::__base_type; \
  684. using __base_type::operator=; \
  685. \
  686. copy_constructor __copy_constructor(__copy_constructor&&) = default; \
  687. ~__copy_constructor() = default; \
  688. __copy_constructor& operator=(const __copy_constructor&) = default; \
  689. __copy_constructor& operator=(__copy_constructor&&) = default; \
  690. }
  691. _LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_TriviallyAvailable,
  692. __copy_constructor(const __copy_constructor& __that) = default;);
  693. _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
  694. _Trait::_Available, __copy_constructor(const __copy_constructor& __that)
  695. : __copy_constructor(__valueless_t{}) { this->__generic_construct(*this, __that); });
  696. _LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_Unavailable, __copy_constructor(const __copy_constructor&) = delete;);
  697. # undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
  698. template <class _Traits>
  699. class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
  700. using __base_type = __copy_constructor<_Traits>;
  701. public:
  702. using __base_type::__base_type;
  703. using __base_type::operator=;
  704. template <size_t _Ip, class... _Args>
  705. _LIBCPP_HIDE_FROM_ABI auto& __emplace(_Args&&... __args) {
  706. this->__destroy();
  707. auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Args>(__args)...);
  708. this->__index = _Ip;
  709. return __res;
  710. }
  711. protected:
  712. template <size_t _Ip, class _Tp, class _Arg>
  713. _LIBCPP_HIDE_FROM_ABI void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
  714. if (this->index() == _Ip) {
  715. __a.__value = std::forward<_Arg>(__arg);
  716. } else {
  717. struct {
  718. _LIBCPP_HIDE_FROM_ABI void operator()(true_type) const { __this->__emplace<_Ip>(std::forward<_Arg>(__arg)); }
  719. _LIBCPP_HIDE_FROM_ABI void operator()(false_type) const {
  720. __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
  721. }
  722. __assignment* __this;
  723. _Arg&& __arg;
  724. } __impl{this, std::forward<_Arg>(__arg)};
  725. __impl(bool_constant < is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v < _Tp >> {});
  726. }
  727. }
  728. template <class _That>
  729. _LIBCPP_HIDE_FROM_ABI void __generic_assign(_That&& __that) {
  730. if (this->valueless_by_exception() && __that.valueless_by_exception()) {
  731. // do nothing.
  732. } else if (__that.valueless_by_exception()) {
  733. this->__destroy();
  734. } else {
  735. __visitation::__base::__visit_alt_at(
  736. __that.index(),
  737. [this](auto& __this_alt, auto&& __that_alt) {
  738. this->__assign_alt(__this_alt, std::forward<decltype(__that_alt)>(__that_alt).__value);
  739. },
  740. *this,
  741. std::forward<_That>(__that));
  742. }
  743. }
  744. };
  745. template <class _Traits, _Trait = _Traits::__move_assignable_trait>
  746. class _LIBCPP_TEMPLATE_VIS __move_assignment;
  747. # define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, move_assignment) \
  748. template <class... _Types> \
  749. class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, move_assignable_trait> \
  750. : public __assignment<__traits<_Types...>> { \
  751. using __base_type = __assignment<__traits<_Types...>>; \
  752. \
  753. public: \
  754. using __base_type::__base_type; \
  755. using __base_type::operator=; \
  756. \
  757. __move_assignment(const __move_assignment&) = default; \
  758. __move_assignment(__move_assignment&&) = default; \
  759. ~__move_assignment() = default; \
  760. __move_assignment& operator=(const __move_assignment&) = default; \
  761. move_assignment \
  762. }
  763. _LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_TriviallyAvailable,
  764. __move_assignment& operator=(__move_assignment&& __that) = default;);
  765. _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
  766. _Trait::_Available,
  767. __move_assignment&
  768. operator=(__move_assignment&& __that) noexcept(
  769. __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_move_assignable_v<_Types>)...>::value) {
  770. this->__generic_assign(std::move(__that));
  771. return *this;
  772. });
  773. _LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_Unavailable, __move_assignment& operator=(__move_assignment&&) = delete;);
  774. # undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
  775. template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
  776. class _LIBCPP_TEMPLATE_VIS __copy_assignment;
  777. # define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, copy_assignment) \
  778. template <class... _Types> \
  779. class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, copy_assignable_trait> \
  780. : public __move_assignment<__traits<_Types...>> { \
  781. using __base_type = __move_assignment<__traits<_Types...>>; \
  782. \
  783. public: \
  784. using __base_type::__base_type; \
  785. using __base_type::operator=; \
  786. \
  787. __copy_assignment(const __copy_assignment&) = default; \
  788. __copy_assignment(__copy_assignment&&) = default; \
  789. ~__copy_assignment() = default; \
  790. copy_assignment __copy_assignment& operator=(__copy_assignment&&) = default; \
  791. }
  792. _LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_TriviallyAvailable,
  793. __copy_assignment& operator=(const __copy_assignment& __that) = default;);
  794. _LIBCPP_VARIANT_COPY_ASSIGNMENT(
  795. _Trait::_Available, __copy_assignment& operator=(const __copy_assignment& __that) {
  796. this->__generic_assign(__that);
  797. return *this;
  798. });
  799. _LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_Unavailable, __copy_assignment& operator=(const __copy_assignment&) = delete;);
  800. # undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
  801. template <class... _Types>
  802. class _LIBCPP_TEMPLATE_VIS __impl : public __copy_assignment<__traits<_Types...>> {
  803. using __base_type = __copy_assignment<__traits<_Types...>>;
  804. public:
  805. using __base_type::__base_type; // get in_place_index_t constructor & friends
  806. _LIBCPP_HIDE_FROM_ABI __impl(__impl const&) = default;
  807. _LIBCPP_HIDE_FROM_ABI __impl(__impl&&) = default;
  808. _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default;
  809. _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&) = default;
  810. template <size_t _Ip, class _Arg>
  811. _LIBCPP_HIDE_FROM_ABI void __assign(_Arg&& __arg) {
  812. this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Arg>(__arg));
  813. }
  814. inline _LIBCPP_HIDE_FROM_ABI void __swap(__impl& __that) {
  815. if (this->valueless_by_exception() && __that.valueless_by_exception()) {
  816. // do nothing.
  817. } else if (this->index() == __that.index()) {
  818. __visitation::__base::__visit_alt_at(
  819. this->index(),
  820. [](auto& __this_alt, auto& __that_alt) {
  821. using std::swap;
  822. swap(__this_alt.__value, __that_alt.__value);
  823. },
  824. *this,
  825. __that);
  826. } else {
  827. __impl* __lhs = this;
  828. __impl* __rhs = std::addressof(__that);
  829. if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
  830. std::swap(__lhs, __rhs);
  831. }
  832. __impl __tmp(std::move(*__rhs));
  833. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  834. if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
  835. this->__generic_construct(*__rhs, std::move(*__lhs));
  836. } else {
  837. // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
  838. // and `__tmp` is nothrow move constructible then we move `__tmp` back
  839. // into `__rhs` and provide the strong exception safety guarantee.
  840. try {
  841. this->__generic_construct(*__rhs, std::move(*__lhs));
  842. } catch (...) {
  843. if (__tmp.__move_nothrow()) {
  844. this->__generic_construct(*__rhs, std::move(__tmp));
  845. }
  846. throw;
  847. }
  848. }
  849. # else
  850. // this isn't consolidated with the `if constexpr` branch above due to
  851. // `throw` being ill-formed with exceptions disabled even when discarded.
  852. this->__generic_construct(*__rhs, std::move(*__lhs));
  853. # endif
  854. this->__generic_construct(*__lhs, std::move(__tmp));
  855. }
  856. }
  857. private:
  858. inline _LIBCPP_HIDE_FROM_ABI bool __move_nothrow() const {
  859. constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
  860. return this->valueless_by_exception() || __results[this->index()];
  861. }
  862. };
  863. struct __no_narrowing_check {
  864. template <class _Dest, class _Source>
  865. using _Apply = __type_identity<_Dest>;
  866. };
  867. struct __narrowing_check {
  868. template <class _Dest>
  869. static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
  870. template <class _Dest, class _Source>
  871. using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
  872. };
  873. template <class _Dest, class _Source>
  874. using __check_for_narrowing _LIBCPP_NODEBUG = typename _If<
  875. # ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
  876. false &&
  877. # endif
  878. is_arithmetic<_Dest>::value,
  879. __narrowing_check,
  880. __no_narrowing_check >::template _Apply<_Dest, _Source>;
  881. template <class _Tp, size_t _Idx>
  882. struct __overload {
  883. template <class _Up>
  884. auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
  885. };
  886. // TODO(LLVM-19): Remove all occurrences of this macro.
  887. # ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
  888. template <class _Tp, size_t>
  889. struct __overload_bool {
  890. template <class _Up, class _Ap = __remove_cvref_t<_Up>>
  891. auto operator()(bool, _Up&&) const -> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
  892. };
  893. template <size_t _Idx>
  894. struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
  895. template <size_t _Idx>
  896. struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
  897. template <size_t _Idx>
  898. struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
  899. template <size_t _Idx>
  900. struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
  901. # endif
  902. template <class... _Bases>
  903. struct __all_overloads : _Bases... {
  904. void operator()() const;
  905. using _Bases::operator()...;
  906. };
  907. template <class _IdxSeq>
  908. struct __make_overloads_imp;
  909. template <size_t... _Idx>
  910. struct __make_overloads_imp<__tuple_indices<_Idx...> > {
  911. template <class... _Types>
  912. using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
  913. };
  914. template <class... _Types>
  915. using _MakeOverloads _LIBCPP_NODEBUG =
  916. typename __make_overloads_imp< __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
  917. template <class _Tp, class... _Types>
  918. using __best_match_t = typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
  919. } // namespace __variant_detail
  920. template <class _Visitor, class... _Vs, typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
  921. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
  922. visit(_Visitor&& __visitor, _Vs&&... __vs);
  923. # if _LIBCPP_STD_VER >= 20
  924. template <class _Rp,
  925. class _Visitor,
  926. class... _Vs,
  927. typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
  928. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
  929. visit(_Visitor&& __visitor, _Vs&&... __vs);
  930. # endif
  931. template <class... _Types>
  932. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant
  933. : private __sfinae_ctor_base< __all<is_copy_constructible_v<_Types>...>::value,
  934. __all<is_move_constructible_v<_Types>...>::value>,
  935. private __sfinae_assign_base<
  936. __all<(is_copy_constructible_v<_Types> && is_copy_assignable_v<_Types>)...>::value,
  937. __all<(is_move_constructible_v<_Types> && is_move_assignable_v<_Types>)...>::value> {
  938. static_assert(0 < sizeof...(_Types), "variant must consist of at least one alternative.");
  939. static_assert(__all<!is_array_v<_Types>...>::value, "variant can not have an array type as an alternative.");
  940. static_assert(__all<!is_reference_v<_Types>...>::value, "variant can not have a reference type as an alternative.");
  941. static_assert(__all<!is_void_v<_Types>...>::value, "variant can not have a void type as an alternative.");
  942. using __first_type = variant_alternative_t<0, variant>;
  943. public:
  944. template <bool _Dummy = true,
  945. enable_if_t<__dependent_type<is_default_constructible<__first_type>, _Dummy>::value, int> = 0>
  946. _LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
  947. : __impl_(in_place_index<0>) {}
  948. _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
  949. _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default;
  950. template < class _Arg,
  951. enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
  952. enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0,
  953. enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
  954. class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
  955. size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
  956. enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
  957. _LIBCPP_HIDE_FROM_ABI constexpr variant(_Arg&& __arg) noexcept(is_nothrow_constructible_v<_Tp, _Arg>)
  958. : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {}
  959. template <size_t _Ip,
  960. class... _Args,
  961. class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
  962. class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
  963. enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
  964. _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_index_t<_Ip>, _Args&&... __args) noexcept(
  965. is_nothrow_constructible_v<_Tp, _Args...>)
  966. : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
  967. template < size_t _Ip,
  968. class _Up,
  969. class... _Args,
  970. enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
  971. class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
  972. enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
  973. _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
  974. in_place_index_t<_Ip>,
  975. initializer_list<_Up> __il,
  976. _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
  977. : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
  978. template < class _Tp,
  979. class... _Args,
  980. size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
  981. enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
  982. _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
  983. is_nothrow_constructible_v<_Tp, _Args...>)
  984. : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
  985. template < class _Tp,
  986. class _Up,
  987. class... _Args,
  988. size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
  989. enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
  990. _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
  991. in_place_type_t<_Tp>,
  992. initializer_list<_Up> __il,
  993. _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
  994. : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
  995. _LIBCPP_HIDE_FROM_ABI ~variant() = default;
  996. _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
  997. _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default;
  998. template < class _Arg,
  999. enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
  1000. class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
  1001. size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
  1002. enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, int> = 0>
  1003. _LIBCPP_HIDE_FROM_ABI variant&
  1004. operator=(_Arg&& __arg) noexcept(is_nothrow_assignable_v<_Tp&, _Arg> && is_nothrow_constructible_v<_Tp, _Arg>) {
  1005. __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg));
  1006. return *this;
  1007. }
  1008. template < size_t _Ip,
  1009. class... _Args,
  1010. enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
  1011. class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
  1012. enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
  1013. _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
  1014. return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
  1015. }
  1016. template < size_t _Ip,
  1017. class _Up,
  1018. class... _Args,
  1019. enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
  1020. class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
  1021. enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
  1022. _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
  1023. return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
  1024. }
  1025. template < class _Tp,
  1026. class... _Args,
  1027. size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
  1028. enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
  1029. _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
  1030. return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
  1031. }
  1032. template < class _Tp,
  1033. class _Up,
  1034. class... _Args,
  1035. size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
  1036. enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
  1037. _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
  1038. return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
  1039. }
  1040. _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
  1041. return __impl_.valueless_by_exception();
  1042. }
  1043. _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
  1044. template < bool _Dummy = true,
  1045. enable_if_t< __all<(__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
  1046. __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
  1047. int> = 0>
  1048. _LIBCPP_HIDE_FROM_ABI void swap(variant& __that) noexcept(
  1049. __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_swappable_v<_Types>)...>::value) {
  1050. __impl_.__swap(__that.__impl_);
  1051. }
  1052. # if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
  1053. // Helper class to implement [variant.visit]/10
  1054. // Constraints: The call to visit does not use an explicit template-argument-list
  1055. // that begins with a type template-argument.
  1056. struct __variant_visit_barrier_tag {
  1057. _LIBCPP_HIDE_FROM_ABI explicit __variant_visit_barrier_tag() = default;
  1058. };
  1059. template <__variant_visit_barrier_tag = __variant_visit_barrier_tag{}, class _Self, class _Visitor>
  1060. _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(this _Self&& __self, _Visitor&& __visitor) {
  1061. using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>;
  1062. return std::visit(std::forward<_Visitor>(__visitor), (_VariantT)__self);
  1063. }
  1064. template <class _Rp, class _Self, class _Visitor>
  1065. _LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(this _Self&& __self, _Visitor&& __visitor) {
  1066. using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>;
  1067. return std::visit<_Rp>(std::forward<_Visitor>(__visitor), (_VariantT)__self);
  1068. }
  1069. # endif
  1070. private:
  1071. __variant_detail::__impl<_Types...> __impl_;
  1072. friend struct __variant_detail::__access::__variant;
  1073. friend struct __variant_detail::__visitation::__variant;
  1074. };
  1075. template <size_t _Ip, class... _Types>
  1076. _LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
  1077. return __v.index() == _Ip;
  1078. }
  1079. template <class _Tp, class... _Types>
  1080. _LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
  1081. return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
  1082. }
  1083. template <size_t _Ip, class _Vp>
  1084. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr auto&& __generic_get(_Vp&& __v) {
  1085. using __variant_detail::__access::__variant;
  1086. if (!std::__holds_alternative<_Ip>(__v)) {
  1087. __throw_bad_variant_access();
  1088. }
  1089. return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
  1090. }
  1091. template <size_t _Ip, class... _Types>
  1092. _LIBCPP_HIDE_FROM_ABI
  1093. _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&
  1094. get(variant<_Types...>& __v) {
  1095. static_assert(_Ip < sizeof...(_Types));
  1096. static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
  1097. return std::__generic_get<_Ip>(__v);
  1098. }
  1099. template <size_t _Ip, class... _Types>
  1100. _LIBCPP_HIDE_FROM_ABI
  1101. _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
  1102. get(variant<_Types...>&& __v) {
  1103. static_assert(_Ip < sizeof...(_Types));
  1104. static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
  1105. return std::__generic_get<_Ip>(std::move(__v));
  1106. }
  1107. template <size_t _Ip, class... _Types>
  1108. _LIBCPP_HIDE_FROM_ABI
  1109. _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
  1110. get(const variant<_Types...>& __v) {
  1111. static_assert(_Ip < sizeof...(_Types));
  1112. static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
  1113. return std::__generic_get<_Ip>(__v);
  1114. }
  1115. template <size_t _Ip, class... _Types>
  1116. _LIBCPP_HIDE_FROM_ABI
  1117. _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
  1118. get(const variant<_Types...>&& __v) {
  1119. static_assert(_Ip < sizeof...(_Types));
  1120. static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
  1121. return std::__generic_get<_Ip>(std::move(__v));
  1122. }
  1123. template <class _Tp, class... _Types>
  1124. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp& get(variant<_Types...>& __v) {
  1125. static_assert(!is_void_v<_Tp>);
  1126. return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
  1127. }
  1128. template <class _Tp, class... _Types>
  1129. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp&& get(variant<_Types...>&& __v) {
  1130. static_assert(!is_void_v<_Tp>);
  1131. return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
  1132. }
  1133. template <class _Tp, class... _Types>
  1134. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&
  1135. get(const variant<_Types...>& __v) {
  1136. static_assert(!is_void_v<_Tp>);
  1137. return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
  1138. }
  1139. template <class _Tp, class... _Types>
  1140. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&&
  1141. get(const variant<_Types...>&& __v) {
  1142. static_assert(!is_void_v<_Tp>);
  1143. return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
  1144. }
  1145. template <size_t _Ip, class _Vp>
  1146. _LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
  1147. using __variant_detail::__access::__variant;
  1148. return __v && std::__holds_alternative<_Ip>(*__v) ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) : nullptr;
  1149. }
  1150. template <size_t _Ip, class... _Types>
  1151. _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
  1152. get_if(variant<_Types...>* __v) noexcept {
  1153. static_assert(_Ip < sizeof...(_Types));
  1154. static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
  1155. return std::__generic_get_if<_Ip>(__v);
  1156. }
  1157. template <size_t _Ip, class... _Types>
  1158. _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
  1159. get_if(const variant<_Types...>* __v) noexcept {
  1160. static_assert(_Ip < sizeof...(_Types));
  1161. static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
  1162. return std::__generic_get_if<_Ip>(__v);
  1163. }
  1164. template <class _Tp, class... _Types>
  1165. _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
  1166. static_assert(!is_void_v<_Tp>);
  1167. return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
  1168. }
  1169. template <class _Tp, class... _Types>
  1170. _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
  1171. static_assert(!is_void_v<_Tp>);
  1172. return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
  1173. }
  1174. template <class _Operator>
  1175. struct __convert_to_bool {
  1176. template <class _T1, class _T2>
  1177. _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_T1&& __t1, _T2&& __t2) const {
  1178. static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value,
  1179. "the relational operator does not return a type which is implicitly convertible to bool");
  1180. return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
  1181. }
  1182. };
  1183. template <class... _Types>
  1184. _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
  1185. using __variant_detail::__visitation::__variant;
  1186. if (__lhs.index() != __rhs.index())
  1187. return false;
  1188. if (__lhs.valueless_by_exception())
  1189. return true;
  1190. return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
  1191. }
  1192. # if _LIBCPP_STD_VER >= 20
  1193. template <class... _Types>
  1194. requires(three_way_comparable<_Types> && ...)
  1195. _LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
  1196. operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
  1197. using __variant_detail::__visitation::__variant;
  1198. using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
  1199. if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
  1200. return strong_ordering::equal;
  1201. if (__lhs.valueless_by_exception())
  1202. return strong_ordering::less;
  1203. if (__rhs.valueless_by_exception())
  1204. return strong_ordering::greater;
  1205. if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
  1206. return __c;
  1207. auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
  1208. return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
  1209. }
  1210. # endif // _LIBCPP_STD_VER >= 20
  1211. template <class... _Types>
  1212. _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
  1213. using __variant_detail::__visitation::__variant;
  1214. if (__lhs.index() != __rhs.index())
  1215. return true;
  1216. if (__lhs.valueless_by_exception())
  1217. return false;
  1218. return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
  1219. }
  1220. template <class... _Types>
  1221. _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
  1222. using __variant_detail::__visitation::__variant;
  1223. if (__rhs.valueless_by_exception())
  1224. return false;
  1225. if (__lhs.valueless_by_exception())
  1226. return true;
  1227. if (__lhs.index() < __rhs.index())
  1228. return true;
  1229. if (__lhs.index() > __rhs.index())
  1230. return false;
  1231. return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
  1232. }
  1233. template <class... _Types>
  1234. _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
  1235. using __variant_detail::__visitation::__variant;
  1236. if (__lhs.valueless_by_exception())
  1237. return false;
  1238. if (__rhs.valueless_by_exception())
  1239. return true;
  1240. if (__lhs.index() > __rhs.index())
  1241. return true;
  1242. if (__lhs.index() < __rhs.index())
  1243. return false;
  1244. return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
  1245. }
  1246. template <class... _Types>
  1247. _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
  1248. using __variant_detail::__visitation::__variant;
  1249. if (__lhs.valueless_by_exception())
  1250. return true;
  1251. if (__rhs.valueless_by_exception())
  1252. return false;
  1253. if (__lhs.index() < __rhs.index())
  1254. return true;
  1255. if (__lhs.index() > __rhs.index())
  1256. return false;
  1257. return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
  1258. }
  1259. template <class... _Types>
  1260. _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
  1261. using __variant_detail::__visitation::__variant;
  1262. if (__rhs.valueless_by_exception())
  1263. return true;
  1264. if (__lhs.valueless_by_exception())
  1265. return false;
  1266. if (__lhs.index() > __rhs.index())
  1267. return true;
  1268. if (__lhs.index() < __rhs.index())
  1269. return false;
  1270. return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
  1271. }
  1272. template <class... _Vs>
  1273. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void __throw_if_valueless(_Vs&&... __vs) {
  1274. const bool __valueless = (... || std::__as_variant(__vs).valueless_by_exception());
  1275. if (__valueless) {
  1276. __throw_bad_variant_access();
  1277. }
  1278. }
  1279. template < class _Visitor, class... _Vs, typename>
  1280. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
  1281. visit(_Visitor&& __visitor, _Vs&&... __vs) {
  1282. using __variant_detail::__visitation::__variant;
  1283. std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
  1284. return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
  1285. }
  1286. # if _LIBCPP_STD_VER >= 20
  1287. template < class _Rp, class _Visitor, class... _Vs, typename>
  1288. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
  1289. visit(_Visitor&& __visitor, _Vs&&... __vs) {
  1290. using __variant_detail::__visitation::__variant;
  1291. std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
  1292. return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
  1293. }
  1294. # endif
  1295. template <class... _Types>
  1296. _LIBCPP_HIDE_FROM_ABI auto
  1297. swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
  1298. -> decltype(__lhs.swap(__rhs)) {
  1299. return __lhs.swap(__rhs);
  1300. }
  1301. template <class... _Types>
  1302. struct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
  1303. using argument_type = variant<_Types...>;
  1304. using result_type = size_t;
  1305. _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type& __v) const {
  1306. using __variant_detail::__visitation::__variant;
  1307. size_t __res =
  1308. __v.valueless_by_exception()
  1309. ? 299792458 // Random value chosen by the universe upon creation
  1310. : __variant::__visit_alt(
  1311. [](const auto& __alt) {
  1312. using __alt_type = __remove_cvref_t<decltype(__alt)>;
  1313. using __value_type = remove_const_t< typename __alt_type::__value_type>;
  1314. return hash<__value_type>{}(__alt.__value);
  1315. },
  1316. __v);
  1317. return std::__hash_combine(__res, hash<size_t>{}(__v.index()));
  1318. }
  1319. };
  1320. // __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
  1321. // type whereas std::get will throw or returning nullptr. This makes it faster than
  1322. // std::get.
  1323. template <size_t _Ip, class _Vp>
  1324. _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
  1325. using __variant_detail::__access::__variant;
  1326. return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
  1327. }
  1328. template <class _Tp, class... _Types>
  1329. _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
  1330. return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
  1331. }
  1332. template <class _Tp, class... _Types>
  1333. _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
  1334. return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
  1335. }
  1336. #endif // _LIBCPP_STD_VER >= 17
  1337. _LIBCPP_END_NAMESPACE_STD
  1338. _LIBCPP_POP_MACROS
  1339. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1340. # include <exception>
  1341. # include <type_traits>
  1342. # include <typeinfo>
  1343. # include <utility>
  1344. #endif
  1345. #endif // _LIBCPP_VARIANT