blob: 43b6aa77fae6f6f309523310e7fe2e6392359be5 [file] [log] [blame]
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001// -*- C++ -*-
2//===------------------------------ variant -------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Eric Fiselier94cdacc2016-12-02 23:00:05 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VARIANT
11#define _LIBCPP_VARIANT
12
13/*
14 variant synopsis
15
16namespace std {
17
18 // 20.7.2, class template variant
19 template <class... Types>
20 class variant {
21 public:
22
23 // 20.7.2.1, constructors
24 constexpr variant() noexcept(see below);
Louis Dionnecee59012019-01-10 20:06:11 +000025 variant(const variant&); // constexpr in C++20
26 variant(variant&&) noexcept(see below); // constexpr in C++20
Eric Fiselier94cdacc2016-12-02 23:00:05 +000027
28 template <class T> constexpr variant(T&&) noexcept(see below);
29
30 template <class T, class... Args>
31 constexpr explicit variant(in_place_type_t<T>, Args&&...);
32
33 template <class T, class U, class... Args>
34 constexpr explicit variant(
35 in_place_type_t<T>, initializer_list<U>, Args&&...);
36
37 template <size_t I, class... Args>
38 constexpr explicit variant(in_place_index_t<I>, Args&&...);
39
40 template <size_t I, class U, class... Args>
41 constexpr explicit variant(
42 in_place_index_t<I>, initializer_list<U>, Args&&...);
43
44 // 20.7.2.2, destructor
45 ~variant();
46
47 // 20.7.2.3, assignment
Louis Dionnecee59012019-01-10 20:06:11 +000048 variant& operator=(const variant&); // constexpr in C++20
49 variant& operator=(variant&&) noexcept(see below); // constexpr in C++20
Eric Fiselier94cdacc2016-12-02 23:00:05 +000050
51 template <class T> variant& operator=(T&&) noexcept(see below);
52
53 // 20.7.2.4, modifiers
54 template <class T, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000055 T& emplace(Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000056
57 template <class T, class U, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000058 T& emplace(initializer_list<U>, Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000059
60 template <size_t I, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000061 variant_alternative_t<I, variant>& emplace(Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000062
63 template <size_t I, class U, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000064 variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000065
66 // 20.7.2.5, value status
67 constexpr bool valueless_by_exception() const noexcept;
68 constexpr size_t index() const noexcept;
69
70 // 20.7.2.6, swap
71 void swap(variant&) noexcept(see below);
72 };
73
74 // 20.7.3, variant helper classes
75 template <class T> struct variant_size; // undefined
76
77 template <class T>
Marshall Clowf1bf62f2018-01-02 17:17:01 +000078 inline constexpr size_t variant_size_v = variant_size<T>::value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +000079
80 template <class T> struct variant_size<const T>;
81 template <class T> struct variant_size<volatile T>;
82 template <class T> struct variant_size<const volatile T>;
83
84 template <class... Types>
85 struct variant_size<variant<Types...>>;
86
87 template <size_t I, class T> struct variant_alternative; // undefined
88
89 template <size_t I, class T>
90 using variant_alternative_t = typename variant_alternative<I, T>::type;
91
92 template <size_t I, class T> struct variant_alternative<I, const T>;
93 template <size_t I, class T> struct variant_alternative<I, volatile T>;
94 template <size_t I, class T> struct variant_alternative<I, const volatile T>;
95
96 template <size_t I, class... Types>
97 struct variant_alternative<I, variant<Types...>>;
98
Marshall Clowf1bf62f2018-01-02 17:17:01 +000099 inline constexpr size_t variant_npos = -1;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000100
101 // 20.7.4, value access
102 template <class T, class... Types>
103 constexpr bool holds_alternative(const variant<Types...>&) noexcept;
104
105 template <size_t I, class... Types>
106 constexpr variant_alternative_t<I, variant<Types...>>&
107 get(variant<Types...>&);
108
109 template <size_t I, class... Types>
110 constexpr variant_alternative_t<I, variant<Types...>>&&
111 get(variant<Types...>&&);
112
113 template <size_t I, class... Types>
114 constexpr variant_alternative_t<I, variant<Types...>> const&
115 get(const variant<Types...>&);
116
117 template <size_t I, class... Types>
118 constexpr variant_alternative_t<I, variant<Types...>> const&&
119 get(const variant<Types...>&&);
120
121 template <class T, class... Types>
122 constexpr T& get(variant<Types...>&);
123
124 template <class T, class... Types>
125 constexpr T&& get(variant<Types...>&&);
126
127 template <class T, class... Types>
128 constexpr const T& get(const variant<Types...>&);
129
130 template <class T, class... Types>
131 constexpr const T&& get(const variant<Types...>&&);
132
133 template <size_t I, class... Types>
134 constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
135 get_if(variant<Types...>*) noexcept;
136
137 template <size_t I, class... Types>
138 constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
139 get_if(const variant<Types...>*) noexcept;
140
141 template <class T, class... Types>
142 constexpr add_pointer_t<T>
143 get_if(variant<Types...>*) noexcept;
144
145 template <class T, class... Types>
146 constexpr add_pointer_t<const T>
147 get_if(const variant<Types...>*) noexcept;
148
149 // 20.7.5, relational operators
150 template <class... Types>
151 constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
152
153 template <class... Types>
154 constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
155
156 template <class... Types>
157 constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
158
159 template <class... Types>
160 constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
161
162 template <class... Types>
163 constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
164
165 template <class... Types>
166 constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
167
168 // 20.7.6, visitation
169 template <class Visitor, class... Variants>
170 constexpr see below visit(Visitor&&, Variants&&...);
171
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -0500172 template <class R, class Visitor, class... Variants>
173 constexpr R visit(Visitor&&, Variants&&...); // since C++20
174
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000175 // 20.7.7, class monostate
176 struct monostate;
177
178 // 20.7.8, monostate relational operators
179 constexpr bool operator<(monostate, monostate) noexcept;
180 constexpr bool operator>(monostate, monostate) noexcept;
181 constexpr bool operator<=(monostate, monostate) noexcept;
182 constexpr bool operator>=(monostate, monostate) noexcept;
183 constexpr bool operator==(monostate, monostate) noexcept;
184 constexpr bool operator!=(monostate, monostate) noexcept;
185
186 // 20.7.9, specialized algorithms
187 template <class... Types>
188 void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
189
190 // 20.7.10, class bad_variant_access
191 class bad_variant_access;
192
193 // 20.7.11, hash support
194 template <class T> struct hash;
195 template <class... Types> struct hash<variant<Types...>>;
196 template <> struct hash<monostate>;
197
198} // namespace std
199
200*/
201
202#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500203#include <__availability>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000204#include <__tuple>
205#include <array>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400206#include <compare>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000207#include <exception>
208#include <functional>
209#include <initializer_list>
210#include <new>
211#include <tuple>
212#include <type_traits>
213#include <utility>
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000214#include <limits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000215#include <version>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000216
217#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
218#pragma GCC system_header
219#endif
220
Eric Fiselier8625d332017-11-19 04:57:22 +0000221_LIBCPP_PUSH_MACROS
222#include <__undef_macros>
223
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000224namespace std { // explicitly not using versioning namespace
225
Louis Dionnecef92e62018-11-19 15:37:04 +0000226class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000227public:
Saleem Abdulrasool5ee83382016-12-31 17:34:26 +0000228 virtual const char* what() const _NOEXCEPT;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000229};
230
231} // namespace std
232
233_LIBCPP_BEGIN_NAMESPACE_STD
234
Louis Dionne7202cd62020-07-22 15:24:16 -0400235// TODO: GCC 5 lies about its support for C++17 (it says it supports it but it
236// really doesn't). That breaks variant, which uses some C++17 features.
237// Remove this once we drop support for GCC 5.
Louis Dionne129391d2020-07-22 15:59:09 -0400238#if _LIBCPP_STD_VER > 14 && !(defined(_LIBCPP_COMPILER_GCC) && _GNUC_VER_NEW < 6000)
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000239
Eric Fiselier10642ea2016-12-03 01:58:07 +0000240_LIBCPP_NORETURN
241inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000242_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier10642ea2016-12-03 01:58:07 +0000243void __throw_bad_variant_access() {
244#ifndef _LIBCPP_NO_EXCEPTIONS
245 throw bad_variant_access();
246#else
247 _VSTD::abort();
248#endif
249}
250
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000251template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000252class _LIBCPP_TEMPLATE_VIS variant;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000253
254template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000255struct _LIBCPP_TEMPLATE_VIS variant_size;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000256
257template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000258_LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000259
260template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000261struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000262
263template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000264struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000265
266template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000267struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000268 : variant_size<_Tp> {};
269
270template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000271struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000272 : integral_constant<size_t, sizeof...(_Types)> {};
273
274template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000275struct _LIBCPP_TEMPLATE_VIS variant_alternative;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000276
277template <size_t _Ip, class _Tp>
278using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
279
280template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000281struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000282 : add_const<variant_alternative_t<_Ip, _Tp>> {};
283
284template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000285struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000286 : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
287
288template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000289struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000290 : add_cv<variant_alternative_t<_Ip, _Tp>> {};
291
292template <size_t _Ip, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000293struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
Marshall Clow185577f2017-06-12 16:13:17 +0000294 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000295 using type = __type_pack_element<_Ip, _Types...>;
296};
297
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000298_LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1);
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000299
300constexpr int __choose_index_type(unsigned int __num_elem) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500301 if (__num_elem < numeric_limits<unsigned char>::max())
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000302 return 0;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500303 if (__num_elem < numeric_limits<unsigned short>::max())
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000304 return 1;
305 return 2;
306}
307
308template <size_t _NumAlts>
309using __variant_index_t =
310#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
311 unsigned int;
312#else
313 std::tuple_element_t<
314 __choose_index_type(_NumAlts),
315 std::tuple<unsigned char, unsigned short, unsigned int>
316 >;
317#endif
318
319template <class _IndexType>
320constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000321
Marek Kurdej2f5ec002021-03-25 18:09:11 +0100322template <class... _Types>
323class _LIBCPP_TEMPLATE_VIS variant;
324
325template <class... _Types>
326_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&
327__as_variant(variant<_Types...>& __vs) noexcept {
328 return __vs;
329}
330
331template <class... _Types>
332_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&
333__as_variant(const variant<_Types...>& __vs) noexcept {
334 return __vs;
335}
336
337template <class... _Types>
338_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&&
339__as_variant(variant<_Types...>&& __vs) noexcept {
340 return _VSTD::move(__vs);
341}
342
343template <class... _Types>
344_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&&
345__as_variant(const variant<_Types...>&& __vs) noexcept {
346 return _VSTD::move(__vs);
347}
348
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000349namespace __find_detail {
350
351template <class _Tp, class... _Types>
352inline _LIBCPP_INLINE_VISIBILITY
353constexpr size_t __find_index() {
354 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
355 size_t __result = __not_found;
356 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
357 if (__matches[__i]) {
358 if (__result != __not_found) {
359 return __ambiguous;
360 }
361 __result = __i;
362 }
363 }
364 return __result;
365}
366
367template <size_t _Index>
368struct __find_unambiguous_index_sfinae_impl
369 : integral_constant<size_t, _Index> {};
370
371template <>
372struct __find_unambiguous_index_sfinae_impl<__not_found> {};
373
374template <>
375struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
376
377template <class _Tp, class... _Types>
378struct __find_unambiguous_index_sfinae
379 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
380
381} // namespace __find_detail
382
383namespace __variant_detail {
384
385struct __valueless_t {};
386
387enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
388
Eric Fiselier87994112020-11-17 17:34:23 -0500389template <typename _Tp,
390 template <typename> class _IsTriviallyAvailable,
391 template <typename> class _IsAvailable>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000392constexpr _Trait __trait =
393 _IsTriviallyAvailable<_Tp>::value
394 ? _Trait::_TriviallyAvailable
395 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
396
397inline _LIBCPP_INLINE_VISIBILITY
398constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
399 _Trait __result = _Trait::_TriviallyAvailable;
400 for (_Trait __t : __traits) {
401 if (static_cast<int>(__t) > static_cast<int>(__result)) {
402 __result = __t;
403 }
404 }
405 return __result;
406}
407
Eric Fiselier87994112020-11-17 17:34:23 -0500408template <typename... _Types>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000409struct __traits {
410 static constexpr _Trait __copy_constructible_trait =
411 __common_trait({__trait<_Types,
412 is_trivially_copy_constructible,
413 is_copy_constructible>...});
414
415 static constexpr _Trait __move_constructible_trait =
416 __common_trait({__trait<_Types,
417 is_trivially_move_constructible,
418 is_move_constructible>...});
419
420 static constexpr _Trait __copy_assignable_trait = __common_trait(
421 {__copy_constructible_trait,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000422 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
423
424 static constexpr _Trait __move_assignable_trait = __common_trait(
425 {__move_constructible_trait,
426 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
427
428 static constexpr _Trait __destructible_trait = __common_trait(
429 {__trait<_Types, is_trivially_destructible, is_destructible>...});
430};
431
432namespace __access {
433
434struct __union {
435 template <class _Vp>
436 inline _LIBCPP_INLINE_VISIBILITY
437 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
438 return _VSTD::forward<_Vp>(__v).__head;
439 }
440
441 template <class _Vp, size_t _Ip>
442 inline _LIBCPP_INLINE_VISIBILITY
443 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
444 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
445 }
446};
447
448struct __base {
449 template <size_t _Ip, class _Vp>
450 inline _LIBCPP_INLINE_VISIBILITY
451 static constexpr auto&& __get_alt(_Vp&& __v) {
452 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
453 in_place_index<_Ip>);
454 }
455};
456
457struct __variant {
458 template <size_t _Ip, class _Vp>
459 inline _LIBCPP_INLINE_VISIBILITY
460 static constexpr auto&& __get_alt(_Vp&& __v) {
461 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
462 }
463};
464
465} // namespace __access
466
467namespace __visitation {
468
469struct __base {
Eric Fiselier87994112020-11-17 17:34:23 -0500470 template <class _Visitor, class... _Vs>
471 inline _LIBCPP_INLINE_VISIBILITY
472 static constexpr decltype(auto)
473 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
474 constexpr auto __fdiagonal =
475 __make_fdiagonal<_Visitor&&,
476 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
477 return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
478 _VSTD::forward<_Vs>(__vs).__as_base()...);
479 }
480
481 template <class _Visitor, class... _Vs>
482 inline _LIBCPP_INLINE_VISIBILITY
483 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
484 _Vs&&... __vs) {
485 constexpr auto __fmatrix =
486 __make_fmatrix<_Visitor&&,
487 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
488 return __at(__fmatrix, __vs.index()...)(
489 _VSTD::forward<_Visitor>(__visitor),
490 _VSTD::forward<_Vs>(__vs).__as_base()...);
491 }
492
493private:
494 template <class _Tp>
495 inline _LIBCPP_INLINE_VISIBILITY
496 static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
497
498 template <class _Tp, size_t _Np, typename... _Indices>
499 inline _LIBCPP_INLINE_VISIBILITY
500 static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
501 size_t __index, _Indices... __indices) {
502 return __at(__elems[__index], __indices...);
503 }
504
505 template <class _Fp, class... _Fs>
506 static constexpr void __std_visit_visitor_return_type_check() {
507 static_assert(
508 __all<is_same_v<_Fp, _Fs>...>::value,
509 "`std::visit` requires the visitor to have a single return type.");
510 }
511
512 template <class... _Fs>
513 inline _LIBCPP_INLINE_VISIBILITY
514 static constexpr auto __make_farray(_Fs&&... __fs) {
515 __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
516 using __result = array<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
517 return __result{{_VSTD::forward<_Fs>(__fs)...}};
518 }
519
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500520 template <size_t... _Is>
Eric Fiselier87994112020-11-17 17:34:23 -0500521 struct __dispatcher {
522 template <class _Fp, class... _Vs>
Michael Park32334da2020-08-30 12:42:35 -0400523 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500524 static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
Arthur O'Dwyerf0bff152020-11-24 09:59:26 -0500525 return _VSTD::__invoke_constexpr(
Eric Fiselier87994112020-11-17 17:34:23 -0500526 static_cast<_Fp>(__f),
527 __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
Michael Park32334da2020-08-30 12:42:35 -0400528 }
529 };
530
Eric Fiselier87994112020-11-17 17:34:23 -0500531 template <class _Fp, class... _Vs, size_t... _Is>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000532 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500533 static constexpr auto __make_dispatch(index_sequence<_Is...>) {
534 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000535 }
536
Eric Fiselier87994112020-11-17 17:34:23 -0500537 template <size_t _Ip, class _Fp, class... _Vs>
538 inline _LIBCPP_INLINE_VISIBILITY
539 static constexpr auto __make_fdiagonal_impl() {
540 return __make_dispatch<_Fp, _Vs...>(
541 index_sequence<(__identity<_Vs>{}, _Ip)...>{});
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000542 }
Eric Fiselier87994112020-11-17 17:34:23 -0500543
544 template <class _Fp, class... _Vs, size_t... _Is>
545 inline _LIBCPP_INLINE_VISIBILITY
546 static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
547 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
548 }
549
550 template <class _Fp, class _Vp, class... _Vs>
551 inline _LIBCPP_INLINE_VISIBILITY
552 static constexpr auto __make_fdiagonal() {
553 constexpr size_t _Np = __uncvref_t<_Vp>::__size();
554 static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
555 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
556 }
557
558 template <class _Fp, class... _Vs, size_t... _Is>
559 inline _LIBCPP_INLINE_VISIBILITY
560 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
561 return __make_dispatch<_Fp, _Vs...>(__is);
562 }
563
564 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
565 inline _LIBCPP_INLINE_VISIBILITY
566 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
567 index_sequence<_Js...>,
568 _Ls... __ls) {
569 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
570 index_sequence<_Is..., _Js>{}, __ls...)...);
571 }
572
573 template <class _Fp, class... _Vs>
574 inline _LIBCPP_INLINE_VISIBILITY
575 static constexpr auto __make_fmatrix() {
576 return __make_fmatrix_impl<_Fp, _Vs...>(
577 index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000578 }
579};
580
581struct __variant {
Eric Fiselier87994112020-11-17 17:34:23 -0500582 template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000583 inline _LIBCPP_INLINE_VISIBILITY
584 static constexpr decltype(auto)
Eric Fiselier87994112020-11-17 17:34:23 -0500585 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000586 return __base::__visit_alt_at(__index,
Eric Fiselier87994112020-11-17 17:34:23 -0500587 _VSTD::forward<_Visitor>(__visitor),
588 _VSTD::forward<_Vs>(__vs).__impl...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000589 }
590
Eric Fiselier87994112020-11-17 17:34:23 -0500591 template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000592 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500593 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
594 _Vs&&... __vs) {
Marek Kurdej2f5ec002021-03-25 18:09:11 +0100595 return __base::__visit_alt(
596 _VSTD::forward<_Visitor>(__visitor),
597 _VSTD::__as_variant(_VSTD::forward<_Vs>(__vs)).__impl...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000598 }
599
Eric Fiselier87994112020-11-17 17:34:23 -0500600 template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000601 inline _LIBCPP_INLINE_VISIBILITY
602 static constexpr decltype(auto)
Eric Fiselier87994112020-11-17 17:34:23 -0500603 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
604 return __visit_alt_at(
605 __index,
606 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
607 _VSTD::forward<_Vs>(__vs)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000608 }
609
Eric Fiselier87994112020-11-17 17:34:23 -0500610 template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000611 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500612 static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
613 _Vs&&... __vs) {
614 return __visit_alt(
615 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
616 _VSTD::forward<_Vs>(__vs)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000617 }
Marek Kurdej2f5ec002021-03-25 18:09:11 +0100618
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -0500619#if _LIBCPP_STD_VER > 17
620 template <class _Rp, class _Visitor, class... _Vs>
621 inline _LIBCPP_INLINE_VISIBILITY
622 static constexpr _Rp __visit_value(_Visitor&& __visitor,
623 _Vs&&... __vs) {
624 return __visit_alt(
625 __make_value_visitor<_Rp>(_VSTD::forward<_Visitor>(__visitor)),
626 _VSTD::forward<_Vs>(__vs)...);
627 }
628#endif
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000629
630private:
Eric Fiselier87994112020-11-17 17:34:23 -0500631 template <class _Visitor, class... _Values>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000632 static constexpr void __std_visit_exhaustive_visitor_check() {
Eric Fiselier87994112020-11-17 17:34:23 -0500633 static_assert(is_invocable_v<_Visitor, _Values...>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000634 "`std::visit` requires the visitor to be exhaustive.");
635 }
636
Eric Fiselier87994112020-11-17 17:34:23 -0500637 template <class _Visitor>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000638 struct __value_visitor {
639 template <class... _Alts>
640 inline _LIBCPP_INLINE_VISIBILITY
641 constexpr decltype(auto) operator()(_Alts&&... __alts) const {
642 __std_visit_exhaustive_visitor_check<
Eric Fiselier87994112020-11-17 17:34:23 -0500643 _Visitor,
644 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
Arthur O'Dwyerf0bff152020-11-24 09:59:26 -0500645 return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000646 _VSTD::forward<_Alts>(__alts).__value...);
647 }
Eric Fiselier87994112020-11-17 17:34:23 -0500648 _Visitor&& __visitor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000649 };
650
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -0500651#if _LIBCPP_STD_VER > 17
652 template <class _Rp, class _Visitor>
653 struct __value_visitor_return_type {
654 template <class... _Alts>
655 inline _LIBCPP_INLINE_VISIBILITY
656 constexpr _Rp operator()(_Alts&&... __alts) const {
657 __std_visit_exhaustive_visitor_check<
658 _Visitor,
659 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
660 if constexpr (is_void_v<_Rp>) {
661 _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
662 _VSTD::forward<_Alts>(__alts).__value...);
663 }
664 else {
665 return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
666 _VSTD::forward<_Alts>(__alts).__value...);
667 }
668 }
669
670 _Visitor&& __visitor;
671 };
672#endif
673
Eric Fiselier87994112020-11-17 17:34:23 -0500674 template <class _Visitor>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000675 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500676 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
677 return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000678 }
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -0500679
680#if _LIBCPP_STD_VER > 17
681 template <class _Rp, class _Visitor>
682 inline _LIBCPP_INLINE_VISIBILITY
683 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
684 return __value_visitor_return_type<_Rp, _Visitor>{_VSTD::forward<_Visitor>(__visitor)};
685 }
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -0500686#endif
Nico Weber8aa36612021-01-25 15:10:41 -0500687};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000688
689} // namespace __visitation
690
691template <size_t _Index, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000692struct _LIBCPP_TEMPLATE_VIS __alt {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000693 using __value_type = _Tp;
694
695 template <class... _Args>
696 inline _LIBCPP_INLINE_VISIBILITY
697 explicit constexpr __alt(in_place_t, _Args&&... __args)
698 : __value(_VSTD::forward<_Args>(__args)...) {}
699
700 __value_type __value;
701};
702
703template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000704union _LIBCPP_TEMPLATE_VIS __union;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000705
706template <_Trait _DestructibleTrait, size_t _Index>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000707union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000708
709#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
710 template <size_t _Index, class _Tp, class... _Types> \
Eric Fiselier87994112020-11-17 17:34:23 -0500711 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000712 _Index, \
713 _Tp, \
714 _Types...> { \
715 public: \
716 inline _LIBCPP_INLINE_VISIBILITY \
717 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
718 \
719 template <class... _Args> \
720 inline _LIBCPP_INLINE_VISIBILITY \
721 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
722 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
723 \
724 template <size_t _Ip, class... _Args> \
725 inline _LIBCPP_INLINE_VISIBILITY \
726 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
727 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
728 \
729 __union(const __union&) = default; \
730 __union(__union&&) = default; \
731 \
732 destructor \
733 \
734 __union& operator=(const __union&) = default; \
735 __union& operator=(__union&&) = default; \
736 \
737 private: \
738 char __dummy; \
739 __alt<_Index, _Tp> __head; \
740 __union<destructible_trait, _Index + 1, _Types...> __tail; \
741 \
742 friend struct __access::__union; \
743 }
744
745_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
746_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
747_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
748
749#undef _LIBCPP_VARIANT_UNION
750
751template <_Trait _DestructibleTrait, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000752class _LIBCPP_TEMPLATE_VIS __base {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000753public:
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000754 using __index_t = __variant_index_t<sizeof...(_Types)>;
755
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000756 inline _LIBCPP_INLINE_VISIBILITY
757 explicit constexpr __base(__valueless_t tag) noexcept
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000758 : __data(tag), __index(__variant_npos<__index_t>) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000759
760 template <size_t _Ip, class... _Args>
761 inline _LIBCPP_INLINE_VISIBILITY
762 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
Eric Fiselier2adf0872016-12-02 23:17:33 +0000763 :
764 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
765 __index(_Ip) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000766
767 inline _LIBCPP_INLINE_VISIBILITY
768 constexpr bool valueless_by_exception() const noexcept {
769 return index() == variant_npos;
770 }
771
772 inline _LIBCPP_INLINE_VISIBILITY
773 constexpr size_t index() const noexcept {
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000774 return __index == __variant_npos<__index_t> ? variant_npos : __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000775 }
776
777protected:
778 inline _LIBCPP_INLINE_VISIBILITY
779 constexpr auto&& __as_base() & { return *this; }
780
781 inline _LIBCPP_INLINE_VISIBILITY
782 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
783
784 inline _LIBCPP_INLINE_VISIBILITY
785 constexpr auto&& __as_base() const & { return *this; }
786
787 inline _LIBCPP_INLINE_VISIBILITY
788 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
789
790 inline _LIBCPP_INLINE_VISIBILITY
791 static constexpr size_t __size() { return sizeof...(_Types); }
792
793 __union<_DestructibleTrait, 0, _Types...> __data;
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000794 __index_t __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000795
796 friend struct __access::__base;
797 friend struct __visitation::__base;
798};
799
800template <class _Traits, _Trait = _Traits::__destructible_trait>
Louis Dionneff05add2020-10-05 16:30:23 -0400801class _LIBCPP_TEMPLATE_VIS __dtor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000802
803#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
804 template <class... _Types> \
Louis Dionneff05add2020-10-05 16:30:23 -0400805 class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, \
806 destructible_trait> \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000807 : public __base<destructible_trait, _Types...> { \
808 using __base_type = __base<destructible_trait, _Types...>; \
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000809 using __index_t = typename __base_type::__index_t; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000810 \
811 public: \
812 using __base_type::__base_type; \
813 using __base_type::operator=; \
814 \
Louis Dionneff05add2020-10-05 16:30:23 -0400815 __dtor(const __dtor&) = default; \
816 __dtor(__dtor&&) = default; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000817 destructor \
Louis Dionneff05add2020-10-05 16:30:23 -0400818 __dtor& operator=(const __dtor&) = default; \
819 __dtor& operator=(__dtor&&) = default; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000820 \
821 protected: \
822 inline _LIBCPP_INLINE_VISIBILITY \
823 destroy \
824 }
825
826_LIBCPP_VARIANT_DESTRUCTOR(
827 _Trait::_TriviallyAvailable,
Louis Dionneff05add2020-10-05 16:30:23 -0400828 ~__dtor() = default;,
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000829 void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000830
831_LIBCPP_VARIANT_DESTRUCTOR(
832 _Trait::_Available,
Louis Dionneff05add2020-10-05 16:30:23 -0400833 ~__dtor() { __destroy(); },
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000834 void __destroy() noexcept {
835 if (!this->valueless_by_exception()) {
836 __visitation::__base::__visit_alt(
837 [](auto& __alt) noexcept {
Marshall Clowe61ba952018-02-12 15:41:25 +0000838 using __alt_type = __uncvref_t<decltype(__alt)>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000839 __alt.~__alt_type();
840 },
841 *this);
842 }
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000843 this->__index = __variant_npos<__index_t>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000844 });
845
846_LIBCPP_VARIANT_DESTRUCTOR(
847 _Trait::_Unavailable,
Louis Dionneff05add2020-10-05 16:30:23 -0400848 ~__dtor() = delete;,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000849 void __destroy() noexcept = delete;);
850
851#undef _LIBCPP_VARIANT_DESTRUCTOR
852
853template <class _Traits>
Louis Dionneff05add2020-10-05 16:30:23 -0400854class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
855 using __base_type = __dtor<_Traits>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000856
857public:
858 using __base_type::__base_type;
859 using __base_type::operator=;
860
861protected:
862 template <size_t _Ip, class _Tp, class... _Args>
863 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000864 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
Eric Fiselier87994112020-11-17 17:34:23 -0500865 ::new ((void*)_VSTD::addressof(__a))
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000866 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
Eric Fiselier87994112020-11-17 17:34:23 -0500867 return __a.__value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000868 }
869
870 template <class _Rhs>
871 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionneff05add2020-10-05 16:30:23 -0400872 static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000873 __lhs.__destroy();
874 if (!__rhs.valueless_by_exception()) {
875 __visitation::__base::__visit_alt_at(
876 __rhs.index(),
877 [](auto& __lhs_alt, auto&& __rhs_alt) {
878 __construct_alt(
879 __lhs_alt,
880 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
881 },
882 __lhs, _VSTD::forward<_Rhs>(__rhs));
883 __lhs.__index = __rhs.index();
884 }
885 }
886};
887
888template <class _Traits, _Trait = _Traits::__move_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000889class _LIBCPP_TEMPLATE_VIS __move_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000890
891#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
892 move_constructor) \
893 template <class... _Types> \
Louis Dionneff05add2020-10-05 16:30:23 -0400894 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
895 move_constructible_trait> \
896 : public __ctor<__traits<_Types...>> { \
897 using __base_type = __ctor<__traits<_Types...>>; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000898 \
899 public: \
900 using __base_type::__base_type; \
901 using __base_type::operator=; \
902 \
903 __move_constructor(const __move_constructor&) = default; \
904 move_constructor \
905 ~__move_constructor() = default; \
906 __move_constructor& operator=(const __move_constructor&) = default; \
907 __move_constructor& operator=(__move_constructor&&) = default; \
908 }
909
910_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
911 _Trait::_TriviallyAvailable,
912 __move_constructor(__move_constructor&& __that) = default;);
913
914_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
915 _Trait::_Available,
916 __move_constructor(__move_constructor&& __that) noexcept(
917 __all<is_nothrow_move_constructible_v<_Types>...>::value)
918 : __move_constructor(__valueless_t{}) {
919 this->__generic_construct(*this, _VSTD::move(__that));
920 });
921
922_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
923 _Trait::_Unavailable,
924 __move_constructor(__move_constructor&&) = delete;);
925
926#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
927
928template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000929class _LIBCPP_TEMPLATE_VIS __copy_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000930
931#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
932 copy_constructor) \
933 template <class... _Types> \
Eric Fiselier87994112020-11-17 17:34:23 -0500934 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000935 copy_constructible_trait> \
936 : public __move_constructor<__traits<_Types...>> { \
937 using __base_type = __move_constructor<__traits<_Types...>>; \
938 \
939 public: \
940 using __base_type::__base_type; \
941 using __base_type::operator=; \
942 \
943 copy_constructor \
944 __copy_constructor(__copy_constructor&&) = default; \
945 ~__copy_constructor() = default; \
946 __copy_constructor& operator=(const __copy_constructor&) = default; \
947 __copy_constructor& operator=(__copy_constructor&&) = default; \
948 }
949
950_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
951 _Trait::_TriviallyAvailable,
952 __copy_constructor(const __copy_constructor& __that) = default;);
953
954_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
955 _Trait::_Available,
956 __copy_constructor(const __copy_constructor& __that)
957 : __copy_constructor(__valueless_t{}) {
958 this->__generic_construct(*this, __that);
959 });
960
961_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
962 _Trait::_Unavailable,
963 __copy_constructor(const __copy_constructor&) = delete;);
964
965#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
966
967template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000968class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000969 using __base_type = __copy_constructor<_Traits>;
970
971public:
972 using __base_type::__base_type;
973 using __base_type::operator=;
974
975 template <size_t _Ip, class... _Args>
976 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000977 auto& __emplace(_Args&&... __args) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000978 this->__destroy();
Eric Fiselier62928442017-04-15 19:32:02 +0000979 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
Eric Fiselier87994112020-11-17 17:34:23 -0500980 _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000981 this->__index = _Ip;
Eric Fiselier62928442017-04-15 19:32:02 +0000982 return __res;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000983 }
984
985protected:
Michael Park62118352017-06-07 10:22:43 +0000986 template <size_t _Ip, class _Tp, class _Arg>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000987 inline _LIBCPP_INLINE_VISIBILITY
Michael Park62118352017-06-07 10:22:43 +0000988 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000989 if (this->index() == _Ip) {
990 __a.__value = _VSTD::forward<_Arg>(__arg);
991 } else {
992 struct {
993 void operator()(true_type) const {
Michael Park62118352017-06-07 10:22:43 +0000994 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000995 }
996 void operator()(false_type) const {
Michael Park62118352017-06-07 10:22:43 +0000997 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000998 }
999 __assignment* __this;
1000 _Arg&& __arg;
1001 } __impl{this, _VSTD::forward<_Arg>(__arg)};
Michael Park62118352017-06-07 10:22:43 +00001002 __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
1003 !is_nothrow_move_constructible_v<_Tp>>{});
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001004 }
1005 }
1006
1007 template <class _That>
1008 inline _LIBCPP_INLINE_VISIBILITY
1009 void __generic_assign(_That&& __that) {
1010 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1011 // do nothing.
1012 } else if (__that.valueless_by_exception()) {
1013 this->__destroy();
1014 } else {
1015 __visitation::__base::__visit_alt_at(
1016 __that.index(),
1017 [this](auto& __this_alt, auto&& __that_alt) {
1018 this->__assign_alt(
1019 __this_alt,
Michael Park62118352017-06-07 10:22:43 +00001020 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001021 },
1022 *this, _VSTD::forward<_That>(__that));
1023 }
1024 }
1025};
1026
1027template <class _Traits, _Trait = _Traits::__move_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001028class _LIBCPP_TEMPLATE_VIS __move_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001029
1030#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
1031 move_assignment) \
1032 template <class... _Types> \
Eric Fiselier87994112020-11-17 17:34:23 -05001033 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001034 move_assignable_trait> \
1035 : public __assignment<__traits<_Types...>> { \
1036 using __base_type = __assignment<__traits<_Types...>>; \
1037 \
1038 public: \
1039 using __base_type::__base_type; \
1040 using __base_type::operator=; \
1041 \
1042 __move_assignment(const __move_assignment&) = default; \
1043 __move_assignment(__move_assignment&&) = default; \
1044 ~__move_assignment() = default; \
1045 __move_assignment& operator=(const __move_assignment&) = default; \
1046 move_assignment \
1047 }
1048
1049_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1050 _Trait::_TriviallyAvailable,
1051 __move_assignment& operator=(__move_assignment&& __that) = default;);
1052
1053_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1054 _Trait::_Available,
1055 __move_assignment& operator=(__move_assignment&& __that) noexcept(
1056 __all<(is_nothrow_move_constructible_v<_Types> &&
1057 is_nothrow_move_assignable_v<_Types>)...>::value) {
1058 this->__generic_assign(_VSTD::move(__that));
1059 return *this;
1060 });
1061
1062_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1063 _Trait::_Unavailable,
1064 __move_assignment& operator=(__move_assignment&&) = delete;);
1065
1066#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
1067
1068template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001069class _LIBCPP_TEMPLATE_VIS __copy_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001070
1071#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
1072 copy_assignment) \
1073 template <class... _Types> \
Eric Fiselier87994112020-11-17 17:34:23 -05001074 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001075 copy_assignable_trait> \
1076 : public __move_assignment<__traits<_Types...>> { \
1077 using __base_type = __move_assignment<__traits<_Types...>>; \
1078 \
1079 public: \
1080 using __base_type::__base_type; \
1081 using __base_type::operator=; \
1082 \
1083 __copy_assignment(const __copy_assignment&) = default; \
1084 __copy_assignment(__copy_assignment&&) = default; \
1085 ~__copy_assignment() = default; \
1086 copy_assignment \
1087 __copy_assignment& operator=(__copy_assignment&&) = default; \
1088 }
1089
1090_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1091 _Trait::_TriviallyAvailable,
1092 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1093
1094_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1095 _Trait::_Available,
1096 __copy_assignment& operator=(const __copy_assignment& __that) {
1097 this->__generic_assign(__that);
1098 return *this;
1099 });
1100
1101_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1102 _Trait::_Unavailable,
1103 __copy_assignment& operator=(const __copy_assignment&) = delete;);
1104
1105#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1106
1107template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001108class _LIBCPP_TEMPLATE_VIS __impl
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001109 : public __copy_assignment<__traits<_Types...>> {
1110 using __base_type = __copy_assignment<__traits<_Types...>>;
1111
1112public:
1113 using __base_type::__base_type;
1114 using __base_type::operator=;
1115
1116 template <size_t _Ip, class _Arg>
1117 inline _LIBCPP_INLINE_VISIBILITY
1118 void __assign(_Arg&& __arg) {
1119 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
Michael Park62118352017-06-07 10:22:43 +00001120 _VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001121 }
1122
1123 inline _LIBCPP_INLINE_VISIBILITY
1124 void __swap(__impl& __that) {
1125 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1126 // do nothing.
1127 } else if (this->index() == __that.index()) {
1128 __visitation::__base::__visit_alt_at(
1129 this->index(),
1130 [](auto& __this_alt, auto& __that_alt) {
1131 using _VSTD::swap;
1132 swap(__this_alt.__value, __that_alt.__value);
1133 },
1134 *this,
1135 __that);
1136 } else {
1137 __impl* __lhs = this;
1138 __impl* __rhs = _VSTD::addressof(__that);
1139 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1140 _VSTD::swap(__lhs, __rhs);
1141 }
1142 __impl __tmp(_VSTD::move(*__rhs));
Michael Park5dee7342020-06-16 15:15:10 -07001143#ifndef _LIBCPP_NO_EXCEPTIONS
Michael Park6a9ef642020-07-08 10:46:02 -07001144 if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
Michael Park31967aa2020-06-16 13:29:23 -07001145 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1146 } else {
1147 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1148 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1149 // into `__rhs` and provide the strong exception safety guarantee.
1150 try {
1151 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1152 } catch (...) {
1153 if (__tmp.__move_nothrow()) {
1154 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1155 }
1156 throw;
1157 }
1158 }
Michael Park5dee7342020-06-16 15:15:10 -07001159#else
1160 // this isn't consolidated with the `if constexpr` branch above due to
1161 // `throw` being ill-formed with exceptions disabled even when discarded.
1162 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1163#endif
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001164 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1165 }
1166 }
1167
1168private:
1169 inline _LIBCPP_INLINE_VISIBILITY
1170 bool __move_nothrow() const {
1171 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1172 return this->valueless_by_exception() || __results[this->index()];
1173 }
1174};
1175
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001176struct __no_narrowing_check {
1177 template <class _Dest, class _Source>
1178 using _Apply = __identity<_Dest>;
1179};
1180
1181struct __narrowing_check {
1182 template <class _Dest>
1183 static auto __test_impl(_Dest (&&)[1]) -> __identity<_Dest>;
1184 template <class _Dest, class _Source>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001185 using _Apply _LIBCPP_NODEBUG_TYPE = decltype(__test_impl<_Dest>({_VSTD::declval<_Source>()}));
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001186};
1187
1188template <class _Dest, class _Source>
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001189using __check_for_narrowing _LIBCPP_NODEBUG_TYPE =
1190 typename _If<
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001191#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
1192 false &&
1193#endif
1194 is_arithmetic<_Dest>::value,
1195 __narrowing_check,
1196 __no_narrowing_check
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001197 >::template _Apply<_Dest, _Source>;
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001198
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001199template <class _Tp, size_t _Idx>
1200struct __overload {
Zhihao Yuan821efb82019-06-20 22:09:40 +00001201 template <class _Up>
Eric Fiselier80f5dfb2019-07-14 18:31:55 +00001202 auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001203};
1204
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001205template <class _Tp, size_t>
1206struct __overload_bool {
Zhihao Yuan821efb82019-06-20 22:09:40 +00001207 template <class _Up, class _Ap = __uncvref_t<_Up>>
1208 auto operator()(bool, _Up&&) const
1209 -> enable_if_t<is_same_v<_Ap, bool>, __identity<_Tp>>;
1210};
1211
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001212template <size_t _Idx>
1213struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
1214template <size_t _Idx>
1215struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
1216template <size_t _Idx>
1217struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
1218template <size_t _Idx>
1219struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
1220
1221template <class ..._Bases>
1222struct __all_overloads : _Bases... {
1223 void operator()() const;
1224 using _Bases::operator()...;
1225};
1226
1227template <class IdxSeq>
1228struct __make_overloads_imp;
1229
1230template <size_t ..._Idx>
1231struct __make_overloads_imp<__tuple_indices<_Idx...> > {
1232 template <class ..._Types>
1233 using _Apply _LIBCPP_NODEBUG_TYPE = __all_overloads<__overload<_Types, _Idx>...>;
1234};
1235
1236template <class ..._Types>
1237using _MakeOverloads _LIBCPP_NODEBUG_TYPE = typename __make_overloads_imp<
1238 __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
Zhihao Yuan821efb82019-06-20 22:09:40 +00001239
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001240template <class _Tp, class... _Types>
Zhihao Yuan821efb82019-06-20 22:09:40 +00001241using __best_match_t =
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001242 typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001243
1244} // __variant_detail
1245
1246template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001247class _LIBCPP_TEMPLATE_VIS variant
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001248 : private __sfinae_ctor_base<
1249 __all<is_copy_constructible_v<_Types>...>::value,
1250 __all<is_move_constructible_v<_Types>...>::value>,
1251 private __sfinae_assign_base<
1252 __all<(is_copy_constructible_v<_Types> &&
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001253 is_copy_assignable_v<_Types>)...>::value,
1254 __all<(is_move_constructible_v<_Types> &&
1255 is_move_assignable_v<_Types>)...>::value> {
1256 static_assert(0 < sizeof...(_Types),
1257 "variant must consist of at least one alternative.");
1258
1259 static_assert(__all<!is_array_v<_Types>...>::value,
1260 "variant can not have an array type as an alternative.");
1261
1262 static_assert(__all<!is_reference_v<_Types>...>::value,
1263 "variant can not have a reference type as an alternative.");
1264
1265 static_assert(__all<!is_void_v<_Types>...>::value,
1266 "variant can not have a void type as an alternative.");
1267
1268 using __first_type = variant_alternative_t<0, variant>;
1269
1270public:
1271 template <bool _Dummy = true,
1272 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1273 _Dummy>::value,
1274 int> = 0>
1275 inline _LIBCPP_INLINE_VISIBILITY
1276 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1277 : __impl(in_place_index<0>) {}
1278
1279 variant(const variant&) = default;
1280 variant(variant&&) = default;
1281
1282 template <
1283 class _Arg,
Marshall Clowd5bbc242018-02-06 20:56:55 +00001284 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1285 enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
1286 enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001287 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1288 size_t _Ip =
1289 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1290 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1291 inline _LIBCPP_INLINE_VISIBILITY
1292 constexpr variant(_Arg&& __arg) noexcept(
1293 is_nothrow_constructible_v<_Tp, _Arg>)
1294 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1295
1296 template <size_t _Ip, class... _Args,
Eric Fiselier5d2df752018-03-23 23:42:30 +00001297 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1298 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1299 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001300 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5d2df752018-03-23 23:42:30 +00001301 explicit constexpr variant(
1302 in_place_index_t<_Ip>,
1303 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001304 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1305
Eric Fiselier5d2df752018-03-23 23:42:30 +00001306 template <
1307 size_t _Ip,
1308 class _Up,
1309 class... _Args,
1310 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1311 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001312 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1313 int> = 0>
1314 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5d2df752018-03-23 23:42:30 +00001315 explicit constexpr variant(
1316 in_place_index_t<_Ip>,
1317 initializer_list<_Up> __il,
1318 _Args&&... __args) noexcept(
1319 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001320 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1321
1322 template <
1323 class _Tp,
1324 class... _Args,
1325 size_t _Ip =
1326 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1327 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1328 inline _LIBCPP_INLINE_VISIBILITY
1329 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1330 is_nothrow_constructible_v<_Tp, _Args...>)
1331 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1332
1333 template <
1334 class _Tp,
1335 class _Up,
1336 class... _Args,
1337 size_t _Ip =
1338 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1339 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1340 int> = 0>
1341 inline _LIBCPP_INLINE_VISIBILITY
1342 explicit constexpr variant(
1343 in_place_type_t<_Tp>,
1344 initializer_list<_Up> __il,
1345 _Args&&... __args) noexcept(
1346 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1347 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1348
1349 ~variant() = default;
1350
1351 variant& operator=(const variant&) = default;
1352 variant& operator=(variant&&) = default;
1353
1354 template <
1355 class _Arg,
Marshall Clowd5bbc242018-02-06 20:56:55 +00001356 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001357 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1358 size_t _Ip =
1359 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1360 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1361 int> = 0>
1362 inline _LIBCPP_INLINE_VISIBILITY
1363 variant& operator=(_Arg&& __arg) noexcept(
1364 is_nothrow_assignable_v<_Tp&, _Arg> &&
1365 is_nothrow_constructible_v<_Tp, _Arg>) {
1366 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1367 return *this;
1368 }
1369
1370 template <
1371 size_t _Ip,
1372 class... _Args,
1373 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1374 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1375 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1376 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001377 _Tp& emplace(_Args&&... __args) {
1378 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001379 }
1380
1381 template <
1382 size_t _Ip,
1383 class _Up,
1384 class... _Args,
1385 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1386 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1387 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1388 int> = 0>
1389 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001390 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1391 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001392 }
1393
1394 template <
1395 class _Tp,
1396 class... _Args,
1397 size_t _Ip =
1398 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1399 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1400 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001401 _Tp& emplace(_Args&&... __args) {
1402 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001403 }
1404
1405 template <
1406 class _Tp,
1407 class _Up,
1408 class... _Args,
1409 size_t _Ip =
1410 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1411 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1412 int> = 0>
1413 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001414 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1415 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001416 }
1417
1418 inline _LIBCPP_INLINE_VISIBILITY
1419 constexpr bool valueless_by_exception() const noexcept {
1420 return __impl.valueless_by_exception();
1421 }
1422
1423 inline _LIBCPP_INLINE_VISIBILITY
1424 constexpr size_t index() const noexcept { return __impl.index(); }
1425
1426 template <
1427 bool _Dummy = true,
1428 enable_if_t<
1429 __all<(
1430 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1431 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1432 int> = 0>
1433 inline _LIBCPP_INLINE_VISIBILITY
1434 void swap(variant& __that) noexcept(
1435 __all<(is_nothrow_move_constructible_v<_Types> &&
1436 is_nothrow_swappable_v<_Types>)...>::value) {
1437 __impl.__swap(__that.__impl);
1438 }
1439
1440private:
1441 __variant_detail::__impl<_Types...> __impl;
1442
1443 friend struct __variant_detail::__access::__variant;
1444 friend struct __variant_detail::__visitation::__variant;
1445};
1446
1447template <size_t _Ip, class... _Types>
1448inline _LIBCPP_INLINE_VISIBILITY
1449constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1450 return __v.index() == _Ip;
1451}
1452
1453template <class _Tp, class... _Types>
1454inline _LIBCPP_INLINE_VISIBILITY
1455constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1456 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1457}
1458
1459template <size_t _Ip, class _Vp>
1460inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001461_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Richard Smith66d1c552018-08-27 21:41:50 +00001462constexpr auto&& __generic_get(_Vp&& __v) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001463 using __variant_detail::__access::__variant;
1464 if (!__holds_alternative<_Ip>(__v)) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001465 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001466 }
1467 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1468}
1469
1470template <size_t _Ip, class... _Types>
1471inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001472_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001473constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1474 variant<_Types...>& __v) {
1475 static_assert(_Ip < sizeof...(_Types));
1476 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1477 return __generic_get<_Ip>(__v);
1478}
1479
1480template <size_t _Ip, class... _Types>
1481inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001482_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001483constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1484 variant<_Types...>&& __v) {
1485 static_assert(_Ip < sizeof...(_Types));
1486 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1487 return __generic_get<_Ip>(_VSTD::move(__v));
1488}
1489
1490template <size_t _Ip, class... _Types>
1491inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001492_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001493constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1494 const variant<_Types...>& __v) {
1495 static_assert(_Ip < sizeof...(_Types));
1496 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1497 return __generic_get<_Ip>(__v);
1498}
1499
1500template <size_t _Ip, class... _Types>
1501inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001502_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001503constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1504 const variant<_Types...>&& __v) {
1505 static_assert(_Ip < sizeof...(_Types));
1506 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1507 return __generic_get<_Ip>(_VSTD::move(__v));
1508}
1509
1510template <class _Tp, class... _Types>
1511inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001512_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001513constexpr _Tp& get(variant<_Types...>& __v) {
1514 static_assert(!is_void_v<_Tp>);
1515 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1516}
1517
1518template <class _Tp, class... _Types>
1519inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001520_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001521constexpr _Tp&& get(variant<_Types...>&& __v) {
1522 static_assert(!is_void_v<_Tp>);
1523 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1524 _VSTD::move(__v));
1525}
1526
1527template <class _Tp, class... _Types>
1528inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001529_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001530constexpr const _Tp& get(const variant<_Types...>& __v) {
1531 static_assert(!is_void_v<_Tp>);
1532 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1533}
1534
1535template <class _Tp, class... _Types>
1536inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001537_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001538constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1539 static_assert(!is_void_v<_Tp>);
1540 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1541 _VSTD::move(__v));
1542}
1543
1544template <size_t _Ip, class _Vp>
1545inline _LIBCPP_INLINE_VISIBILITY
1546constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1547 using __variant_detail::__access::__variant;
1548 return __v && __holds_alternative<_Ip>(*__v)
1549 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1550 : nullptr;
1551}
1552
1553template <size_t _Ip, class... _Types>
1554inline _LIBCPP_INLINE_VISIBILITY
1555constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1556get_if(variant<_Types...>* __v) noexcept {
1557 static_assert(_Ip < sizeof...(_Types));
1558 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1559 return __generic_get_if<_Ip>(__v);
1560}
1561
1562template <size_t _Ip, class... _Types>
1563inline _LIBCPP_INLINE_VISIBILITY
1564constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1565get_if(const variant<_Types...>* __v) noexcept {
1566 static_assert(_Ip < sizeof...(_Types));
1567 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1568 return __generic_get_if<_Ip>(__v);
1569}
1570
1571template <class _Tp, class... _Types>
1572inline _LIBCPP_INLINE_VISIBILITY
1573constexpr add_pointer_t<_Tp>
1574get_if(variant<_Types...>* __v) noexcept {
1575 static_assert(!is_void_v<_Tp>);
1576 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1577}
1578
1579template <class _Tp, class... _Types>
1580inline _LIBCPP_INLINE_VISIBILITY
1581constexpr add_pointer_t<const _Tp>
1582get_if(const variant<_Types...>* __v) noexcept {
1583 static_assert(!is_void_v<_Tp>);
1584 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1585}
1586
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001587template <class _Operator>
1588struct __convert_to_bool {
1589 template <class _T1, class _T2>
1590 _LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001591 static_assert(is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001592 "the relational operator does not return a type which is implicitly convertible to bool");
1593 return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
1594 }
1595};
1596
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001597template <class... _Types>
1598inline _LIBCPP_INLINE_VISIBILITY
1599constexpr bool operator==(const variant<_Types...>& __lhs,
1600 const variant<_Types...>& __rhs) {
1601 using __variant_detail::__visitation::__variant;
1602 if (__lhs.index() != __rhs.index()) return false;
1603 if (__lhs.valueless_by_exception()) return true;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001604 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001605}
1606
1607template <class... _Types>
1608inline _LIBCPP_INLINE_VISIBILITY
1609constexpr bool operator!=(const variant<_Types...>& __lhs,
1610 const variant<_Types...>& __rhs) {
1611 using __variant_detail::__visitation::__variant;
1612 if (__lhs.index() != __rhs.index()) return true;
1613 if (__lhs.valueless_by_exception()) return false;
1614 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001615 __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001616}
1617
1618template <class... _Types>
1619inline _LIBCPP_INLINE_VISIBILITY
1620constexpr bool operator<(const variant<_Types...>& __lhs,
1621 const variant<_Types...>& __rhs) {
1622 using __variant_detail::__visitation::__variant;
1623 if (__rhs.valueless_by_exception()) return false;
1624 if (__lhs.valueless_by_exception()) return true;
1625 if (__lhs.index() < __rhs.index()) return true;
1626 if (__lhs.index() > __rhs.index()) return false;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001627 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001628}
1629
1630template <class... _Types>
1631inline _LIBCPP_INLINE_VISIBILITY
1632constexpr bool operator>(const variant<_Types...>& __lhs,
1633 const variant<_Types...>& __rhs) {
1634 using __variant_detail::__visitation::__variant;
1635 if (__lhs.valueless_by_exception()) return false;
1636 if (__rhs.valueless_by_exception()) return true;
1637 if (__lhs.index() > __rhs.index()) return true;
1638 if (__lhs.index() < __rhs.index()) return false;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001639 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001640}
1641
1642template <class... _Types>
1643inline _LIBCPP_INLINE_VISIBILITY
1644constexpr bool operator<=(const variant<_Types...>& __lhs,
1645 const variant<_Types...>& __rhs) {
1646 using __variant_detail::__visitation::__variant;
1647 if (__lhs.valueless_by_exception()) return true;
1648 if (__rhs.valueless_by_exception()) return false;
1649 if (__lhs.index() < __rhs.index()) return true;
1650 if (__lhs.index() > __rhs.index()) return false;
1651 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001652 __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001653}
1654
1655template <class... _Types>
1656inline _LIBCPP_INLINE_VISIBILITY
1657constexpr bool operator>=(const variant<_Types...>& __lhs,
1658 const variant<_Types...>& __rhs) {
1659 using __variant_detail::__visitation::__variant;
1660 if (__rhs.valueless_by_exception()) return true;
1661 if (__lhs.valueless_by_exception()) return false;
1662 if (__lhs.index() > __rhs.index()) return true;
1663 if (__lhs.index() < __rhs.index()) return false;
1664 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001665 __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001666}
1667
Ruslan Arutyunyan24d0eea2021-01-25 13:34:03 -05001668template <class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001669inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdej2f5ec002021-03-25 18:09:11 +01001670 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void
1671 __throw_if_valueless(_Vs&&... __vs) {
1672 const bool __valueless =
1673 (... || _VSTD::__as_variant(__vs).valueless_by_exception());
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -05001674 if (__valueless) {
Marek Kurdej2f5ec002021-03-25 18:09:11 +01001675 __throw_bad_variant_access();
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -05001676 }
1677}
1678
Marek Kurdej2f5ec002021-03-25 18:09:11 +01001679template <
1680 class _Visitor, class... _Vs,
1681 typename = void_t<decltype(_VSTD::__as_variant(_VSTD::declval<_Vs>()))...> >
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -05001682inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdej2f5ec002021-03-25 18:09:11 +01001683 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr
1684 decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001685 using __variant_detail::__visitation::__variant;
Ruslan Arutyunyan24d0eea2021-01-25 13:34:03 -05001686 _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
Eric Fiselier87994112020-11-17 17:34:23 -05001687 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001688 _VSTD::forward<_Vs>(__vs)...);
1689}
1690
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -05001691#if _LIBCPP_STD_VER > 17
Marek Kurdej2f5ec002021-03-25 18:09:11 +01001692template <
1693 class _Rp, class _Visitor, class... _Vs,
1694 typename = void_t<decltype(_VSTD::__as_variant(_VSTD::declval<_Vs>()))...> >
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -05001695inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdej2f5ec002021-03-25 18:09:11 +01001696 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
1697 visit(_Visitor&& __visitor, _Vs&&... __vs) {
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -05001698 using __variant_detail::__visitation::__variant;
Ruslan Arutyunyan24d0eea2021-01-25 13:34:03 -05001699 _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
Ruslan Arutyunyan33f34732021-01-25 11:12:25 -05001700 return __variant::__visit_value<_Rp>(_VSTD::forward<_Visitor>(__visitor),
1701 _VSTD::forward<_Vs>(__vs)...);
1702}
1703#endif
1704
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001705struct _LIBCPP_TEMPLATE_VIS monostate {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001706
1707inline _LIBCPP_INLINE_VISIBILITY
1708constexpr bool operator<(monostate, monostate) noexcept { return false; }
1709
1710inline _LIBCPP_INLINE_VISIBILITY
1711constexpr bool operator>(monostate, monostate) noexcept { return false; }
1712
1713inline _LIBCPP_INLINE_VISIBILITY
1714constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1715
1716inline _LIBCPP_INLINE_VISIBILITY
1717constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1718
1719inline _LIBCPP_INLINE_VISIBILITY
1720constexpr bool operator==(monostate, monostate) noexcept { return true; }
1721
1722inline _LIBCPP_INLINE_VISIBILITY
1723constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1724
1725template <class... _Types>
1726inline _LIBCPP_INLINE_VISIBILITY
1727auto swap(variant<_Types...>& __lhs,
1728 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1729 -> decltype(__lhs.swap(__rhs)) {
1730 __lhs.swap(__rhs);
1731}
1732
1733template <class... _Types>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001734struct _LIBCPP_TEMPLATE_VIS hash<
1735 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001736 using argument_type = variant<_Types...>;
1737 using result_type = size_t;
1738
1739 inline _LIBCPP_INLINE_VISIBILITY
1740 result_type operator()(const argument_type& __v) const {
1741 using __variant_detail::__visitation::__variant;
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001742 size_t __res =
1743 __v.valueless_by_exception()
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001744 ? 299792458 // Random value chosen by the universe upon creation
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001745 : __variant::__visit_alt(
1746 [](const auto& __alt) {
Marshall Clowe61ba952018-02-12 15:41:25 +00001747 using __alt_type = __uncvref_t<decltype(__alt)>;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001748 using __value_type = remove_const_t<
1749 typename __alt_type::__value_type>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001750 return hash<__value_type>{}(__alt.__value);
1751 },
1752 __v);
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001753 return __hash_combine(__res, hash<size_t>{}(__v.index()));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001754 }
1755};
1756
1757template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001758struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001759 using argument_type = monostate;
1760 using result_type = size_t;
1761
1762 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001763 result_type operator()(const argument_type&) const _NOEXCEPT {
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001764 return 66740831; // return a fundamentally attractive random value.
1765 }
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001766};
1767
1768#endif // _LIBCPP_STD_VER > 14
1769
1770_LIBCPP_END_NAMESPACE_STD
1771
Eric Fiselier8625d332017-11-19 04:57:22 +00001772_LIBCPP_POP_MACROS
1773
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001774#endif // _LIBCPP_VARIANT