variant 61 KB

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