variant 66 KB

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