variant 65 KB

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