variant 69 KB

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