variant 67 KB

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