xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/std/variant (revision 0a3071956a3a9fdebdbf7f338cf2d439b45fc728)
1// <variant> -*- C++ -*-
2
3// Copyright (C) 2016-2022 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file variant
26 *  This is the `<variant>` C++ Library header.
27 */
28
29#ifndef _GLIBCXX_VARIANT
30#define _GLIBCXX_VARIANT 1
31
32#pragma GCC system_header
33
34#if __cplusplus >= 201703L
35
36#include <initializer_list>
37#include <type_traits>
38#include <bits/enable_special_members.h>
39#include <bits/exception_defines.h>
40#include <bits/functional_hash.h>
41#include <bits/invoke.h>
42#include <bits/parse_numbers.h>
43#include <bits/stl_iterator_base_types.h>
44#include <bits/stl_iterator_base_funcs.h>
45#include <bits/stl_construct.h>
46#include <bits/utility.h> // in_place_index_t
47#if __cplusplus >= 202002L
48# include <compare>
49#endif
50
51#if __cpp_concepts >= 202002L && __cpp_constexpr >= 201811L
52// P2231R1 constexpr needs constexpr unions and constrained destructors.
53# define __cpp_lib_variant 202106L
54#else
55# include <ext/aligned_buffer.h> // Use __aligned_membuf instead of union.
56# define __cpp_lib_variant 202102L
57#endif
58
59namespace std _GLIBCXX_VISIBILITY(default)
60{
61_GLIBCXX_BEGIN_NAMESPACE_VERSION
62
63  template<typename... _Types> class tuple;
64  template<typename... _Types> class variant;
65  template <typename> struct hash;
66
67  template<typename _Variant>
68    struct variant_size;
69
70  template<typename _Variant>
71    struct variant_size<const _Variant> : variant_size<_Variant> {};
72
73  template<typename _Variant>
74    struct variant_size<volatile _Variant> : variant_size<_Variant> {};
75
76  template<typename _Variant>
77    struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
78
79  template<typename... _Types>
80    struct variant_size<variant<_Types...>>
81    : std::integral_constant<size_t, sizeof...(_Types)> {};
82
83  template<typename _Variant>
84    inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
85
86  template<typename... _Types>
87    inline constexpr size_t
88    variant_size_v<variant<_Types...>> = sizeof...(_Types);
89
90  template<typename... _Types>
91    inline constexpr size_t
92    variant_size_v<const variant<_Types...>> = sizeof...(_Types);
93
94  template<size_t _Np, typename _Variant>
95    struct variant_alternative;
96
97  template<size_t _Np, typename... _Types>
98    struct variant_alternative<_Np, variant<_Types...>>
99    {
100      static_assert(_Np < sizeof...(_Types));
101
102      using type = typename _Nth_type<_Np, _Types...>::type;
103    };
104
105  template<size_t _Np, typename _Variant>
106    using variant_alternative_t =
107      typename variant_alternative<_Np, _Variant>::type;
108
109  template<size_t _Np, typename _Variant>
110    struct variant_alternative<_Np, const _Variant>
111    { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
112
113  template<size_t _Np, typename _Variant>
114    struct variant_alternative<_Np, volatile _Variant>
115    { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
116
117  template<size_t _Np, typename _Variant>
118    struct variant_alternative<_Np, const volatile _Variant>
119    { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
120
121  inline constexpr size_t variant_npos = -1;
122
123  template<size_t _Np, typename... _Types>
124    constexpr variant_alternative_t<_Np, variant<_Types...>>&
125    get(variant<_Types...>&);
126
127  template<size_t _Np, typename... _Types>
128    constexpr variant_alternative_t<_Np, variant<_Types...>>&&
129    get(variant<_Types...>&&);
130
131  template<size_t _Np, typename... _Types>
132    constexpr variant_alternative_t<_Np, variant<_Types...>> const&
133    get(const variant<_Types...>&);
134
135  template<size_t _Np, typename... _Types>
136    constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
137    get(const variant<_Types...>&&);
138
139  template<typename _Result_type, typename _Visitor, typename... _Variants>
140    constexpr decltype(auto)
141    __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
142
143  template <typename... _Types, typename _Tp>
144    _GLIBCXX20_CONSTEXPR
145    decltype(auto)
146    __variant_cast(_Tp&& __rhs)
147    {
148      if constexpr (is_lvalue_reference_v<_Tp>)
149	{
150	  if constexpr (is_const_v<remove_reference_t<_Tp>>)
151	    return static_cast<const variant<_Types...>&>(__rhs);
152	  else
153	    return static_cast<variant<_Types...>&>(__rhs);
154	}
155      else
156        return static_cast<variant<_Types...>&&>(__rhs);
157    }
158
159namespace __detail
160{
161namespace __variant
162{
163  // used for raw visitation
164  struct __variant_cookie {};
165  // used for raw visitation with indices passed in
166  struct __variant_idx_cookie { using type = __variant_idx_cookie; };
167  // Used to enable deduction (and same-type checking) for std::visit:
168  template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
169
170  // Visit variants that might be valueless.
171  template<typename _Visitor, typename... _Variants>
172    constexpr void
173    __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
174    {
175      std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
176				        std::forward<_Variants>(__variants)...);
177    }
178
179  // Visit variants that might be valueless, passing indices to the visitor.
180  template<typename _Visitor, typename... _Variants>
181    constexpr void
182    __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
183    {
184      std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
185	  std::forward<_Variants>(__variants)...);
186    }
187
188  // The __as function templates implement the exposition-only "as-variant"
189
190  template<typename... _Types>
191    constexpr std::variant<_Types...>&
192    __as(std::variant<_Types...>& __v) noexcept
193    { return __v; }
194
195  template<typename... _Types>
196    constexpr const std::variant<_Types...>&
197    __as(const std::variant<_Types...>& __v) noexcept
198    { return __v; }
199
200  template<typename... _Types>
201    constexpr std::variant<_Types...>&&
202    __as(std::variant<_Types...>&& __v) noexcept
203    { return std::move(__v); }
204
205  template<typename... _Types>
206    constexpr const std::variant<_Types...>&&
207    __as(const std::variant<_Types...>&& __v) noexcept
208    { return std::move(__v); }
209
210  // For C++17:
211  // _Uninitialized<T> is guaranteed to be a trivially destructible type,
212  // even if T is not.
213  // For C++20:
214  // _Uninitialized<T> is trivially destructible iff T is, so _Variant_union
215  // needs a constrained non-trivial destructor.
216  template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
217    struct _Uninitialized;
218
219  template<typename _Type>
220    struct _Uninitialized<_Type, true>
221    {
222      template<typename... _Args>
223	constexpr
224	_Uninitialized(in_place_index_t<0>, _Args&&... __args)
225	: _M_storage(std::forward<_Args>(__args)...)
226	{ }
227
228      constexpr const _Type& _M_get() const & noexcept
229      { return _M_storage; }
230
231      constexpr _Type& _M_get() & noexcept
232      { return _M_storage; }
233
234      constexpr const _Type&& _M_get() const && noexcept
235      { return std::move(_M_storage); }
236
237      constexpr _Type&& _M_get() && noexcept
238      { return std::move(_M_storage); }
239
240      _Type _M_storage;
241    };
242
243  template<typename _Type>
244    struct _Uninitialized<_Type, false>
245    {
246#if __cpp_lib_variant >= 202106L
247      template<typename... _Args>
248	constexpr
249	_Uninitialized(in_place_index_t<0>, _Args&&... __args)
250	: _M_storage(std::forward<_Args>(__args)...)
251	{ }
252
253      constexpr ~_Uninitialized() { }
254
255      _Uninitialized(const _Uninitialized&) = default;
256      _Uninitialized(_Uninitialized&&) = default;
257      _Uninitialized& operator=(const _Uninitialized&) = default;
258      _Uninitialized& operator=(_Uninitialized&&) = default;
259
260      constexpr const _Type& _M_get() const & noexcept
261      { return _M_storage; }
262
263      constexpr _Type& _M_get() & noexcept
264      { return _M_storage; }
265
266      constexpr const _Type&& _M_get() const && noexcept
267      { return std::move(_M_storage); }
268
269      constexpr _Type&& _M_get() && noexcept
270      { return std::move(_M_storage); }
271
272      struct _Empty_byte { };
273
274      union {
275	_Empty_byte _M_empty;
276	_Type _M_storage;
277      };
278#else
279      template<typename... _Args>
280	constexpr
281	_Uninitialized(in_place_index_t<0>, _Args&&... __args)
282	{
283	  ::new ((void*)std::addressof(_M_storage))
284	    _Type(std::forward<_Args>(__args)...);
285	}
286
287      const _Type& _M_get() const & noexcept
288      { return *_M_storage._M_ptr(); }
289
290      _Type& _M_get() & noexcept
291      { return *_M_storage._M_ptr(); }
292
293      const _Type&& _M_get() const && noexcept
294      { return std::move(*_M_storage._M_ptr()); }
295
296      _Type&& _M_get() && noexcept
297      { return std::move(*_M_storage._M_ptr()); }
298
299      __gnu_cxx::__aligned_membuf<_Type> _M_storage;
300#endif
301    };
302
303  template<size_t _Np, typename _Union>
304    constexpr decltype(auto)
305    __get_n(_Union&& __u) noexcept
306    {
307      if constexpr (_Np == 0)
308	return std::forward<_Union>(__u)._M_first._M_get();
309      else if constexpr (_Np == 1)
310	return std::forward<_Union>(__u)._M_rest._M_first._M_get();
311      else if constexpr (_Np == 2)
312	return std::forward<_Union>(__u)._M_rest._M_rest._M_first._M_get();
313      else
314	return __variant::__get_n<_Np - 3>(
315		 std::forward<_Union>(__u)._M_rest._M_rest._M_rest);
316    }
317
318  // Returns the typed storage for __v.
319  template<size_t _Np, typename _Variant>
320    constexpr decltype(auto)
321    __get(_Variant&& __v) noexcept
322    { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
323
324  // Gets the _Uninitialized to construct into for __u.
325  template<size_t _Np, typename _Union>
326    constexpr decltype(auto)
327    __construct_n(_Union& __u) noexcept
328    {
329      if constexpr (_Np == 0)
330	return &__u._M_first;
331      else if constexpr (_Np == 1)
332	{
333	  std::_Construct(&__u._M_rest);
334	  return &__u._M_rest._M_first;
335	}
336      else if constexpr (_Np == 2)
337	{
338	  std::_Construct(&__u._M_rest);
339	  std::_Construct(&__u._M_rest._M_rest);
340	  return &__u._M_rest._M_rest._M_first;
341	}
342      else
343	{
344	  std::_Construct(&__u._M_rest);
345	  std::_Construct(&__u._M_rest._M_rest);
346	  std::_Construct(&__u._M_rest._M_rest._M_rest);
347	  return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
348	}
349    }
350
351  template<typename... _Types>
352    struct _Traits
353    {
354      static constexpr bool _S_default_ctor =
355	  is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
356      static constexpr bool _S_copy_ctor =
357	  (is_copy_constructible_v<_Types> && ...);
358      static constexpr bool _S_move_ctor =
359	  (is_move_constructible_v<_Types> && ...);
360      static constexpr bool _S_copy_assign =
361	  _S_copy_ctor
362	  && (is_copy_assignable_v<_Types> && ...);
363      static constexpr bool _S_move_assign =
364	  _S_move_ctor
365	  && (is_move_assignable_v<_Types> && ...);
366
367      static constexpr bool _S_trivial_dtor =
368	  (is_trivially_destructible_v<_Types> && ...);
369      static constexpr bool _S_trivial_copy_ctor =
370	  (is_trivially_copy_constructible_v<_Types> && ...);
371      static constexpr bool _S_trivial_move_ctor =
372	  (is_trivially_move_constructible_v<_Types> && ...);
373      static constexpr bool _S_trivial_copy_assign =
374	  _S_trivial_dtor && _S_trivial_copy_ctor
375	  && (is_trivially_copy_assignable_v<_Types> && ...);
376      static constexpr bool _S_trivial_move_assign =
377	  _S_trivial_dtor && _S_trivial_move_ctor
378	  && (is_trivially_move_assignable_v<_Types> && ...);
379
380      // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
381      // are always nothrow.
382      static constexpr bool _S_nothrow_default_ctor =
383	  is_nothrow_default_constructible_v<
384	      typename _Nth_type<0, _Types...>::type>;
385      static constexpr bool _S_nothrow_copy_ctor = false;
386      static constexpr bool _S_nothrow_move_ctor =
387	  (is_nothrow_move_constructible_v<_Types> && ...);
388      static constexpr bool _S_nothrow_copy_assign = false;
389      static constexpr bool _S_nothrow_move_assign =
390	  _S_nothrow_move_ctor
391	  && (is_nothrow_move_assignable_v<_Types> && ...);
392    };
393
394  // Defines members and ctors.
395  template<typename... _Types>
396    union _Variadic_union
397    {
398      _Variadic_union() = default;
399
400      template<size_t _Np, typename... _Args>
401	_Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete;
402    };
403
404  template<typename _First, typename... _Rest>
405    union _Variadic_union<_First, _Rest...>
406    {
407      constexpr _Variadic_union() : _M_rest() { }
408
409      template<typename... _Args>
410	constexpr
411	_Variadic_union(in_place_index_t<0>, _Args&&... __args)
412	: _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
413	{ }
414
415      template<size_t _Np, typename... _Args>
416	constexpr
417	_Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
418	: _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
419	{ }
420
421#if __cpp_lib_variant >= 202106L
422      _Variadic_union(const _Variadic_union&) = default;
423      _Variadic_union(_Variadic_union&&) = default;
424      _Variadic_union& operator=(const _Variadic_union&) = default;
425      _Variadic_union& operator=(_Variadic_union&&) = default;
426
427      ~_Variadic_union() = default;
428
429      constexpr ~_Variadic_union()
430	requires (!__has_trivial_destructor(_First))
431	      || (!__has_trivial_destructor(_Variadic_union<_Rest...>))
432      { }
433#endif
434
435      _Uninitialized<_First> _M_first;
436      _Variadic_union<_Rest...> _M_rest;
437    };
438
439  // _Never_valueless_alt is true for variant alternatives that can
440  // always be placed in a variant without it becoming valueless.
441
442  // For suitably-small, trivially copyable types we can create temporaries
443  // on the stack and then memcpy them into place.
444  template<typename _Tp>
445    struct _Never_valueless_alt
446    : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
447    { };
448
449  // Specialize _Never_valueless_alt for other types which have a
450  // non-throwing and cheap move construction and move assignment operator,
451  // so that emplacing the type will provide the strong exception-safety
452  // guarantee, by creating and moving a temporary.
453  // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
454  // variant using that alternative, so we can't change the value later!
455
456  // True if every alternative in _Types... can be emplaced in a variant
457  // without it becoming valueless. If this is true, variant<_Types...>
458  // can never be valueless, which enables some minor optimizations.
459  template <typename... _Types>
460    constexpr bool __never_valueless()
461    {
462      return _Traits<_Types...>::_S_move_assign
463	&& (_Never_valueless_alt<_Types>::value && ...);
464    }
465
466  // Defines index and the dtor, possibly trivial.
467  template<bool __trivially_destructible, typename... _Types>
468    struct _Variant_storage;
469
470  template <typename... _Types>
471    using __select_index =
472      typename __select_int::_Select_int_base<sizeof...(_Types),
473					      unsigned char,
474					      unsigned short>::type::value_type;
475
476  template<typename... _Types>
477    struct _Variant_storage<false, _Types...>
478    {
479      constexpr
480      _Variant_storage()
481      : _M_index(static_cast<__index_type>(variant_npos))
482      { }
483
484      template<size_t _Np, typename... _Args>
485	constexpr
486	_Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
487	: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
488	_M_index{_Np}
489	{ }
490
491      constexpr void
492      _M_reset()
493      {
494	if (!_M_valid()) [[unlikely]]
495	  return;
496
497	std::__do_visit<void>([](auto&& __this_mem) mutable
498	  {
499	    std::_Destroy(std::__addressof(__this_mem));
500	  }, __variant_cast<_Types...>(*this));
501
502	_M_index = static_cast<__index_type>(variant_npos);
503      }
504
505      _GLIBCXX20_CONSTEXPR
506      ~_Variant_storage()
507      { _M_reset(); }
508
509      constexpr bool
510      _M_valid() const noexcept
511      {
512	if constexpr (__variant::__never_valueless<_Types...>())
513	  return true;
514	return this->_M_index != __index_type(variant_npos);
515      }
516
517      _Variadic_union<_Types...> _M_u;
518      using __index_type = __select_index<_Types...>;
519      __index_type _M_index;
520    };
521
522  template<typename... _Types>
523    struct _Variant_storage<true, _Types...>
524    {
525      constexpr
526      _Variant_storage()
527      : _M_index(static_cast<__index_type>(variant_npos))
528      { }
529
530      template<size_t _Np, typename... _Args>
531	constexpr
532	_Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
533	: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
534	_M_index{_Np}
535	{ }
536
537      constexpr void
538      _M_reset() noexcept
539      { _M_index = static_cast<__index_type>(variant_npos); }
540
541      constexpr bool
542      _M_valid() const noexcept
543      {
544	if constexpr (__variant::__never_valueless<_Types...>())
545	  return true;
546	// It would be nice if we could just return true for -fno-exceptions.
547	// It's possible (but inadvisable) that a std::variant could become
548	// valueless in a translation unit compiled with -fexceptions and then
549	// be passed to functions compiled with -fno-exceptions. We would need
550	// some #ifdef _GLIBCXX_NO_EXCEPTIONS_GLOBALLY property to elide all
551	// checks for valueless_by_exception().
552	return this->_M_index != static_cast<__index_type>(variant_npos);
553      }
554
555      _Variadic_union<_Types...> _M_u;
556      using __index_type = __select_index<_Types...>;
557      __index_type _M_index;
558    };
559
560  // Implementation of v.emplace<N>(args...).
561  template<size_t _Np, bool _Triv, typename... _Types, typename... _Args>
562    _GLIBCXX20_CONSTEXPR
563    inline void
564    __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
565    {
566      __v._M_reset();
567      auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
568      std::_Construct(__addr, in_place_index<0>,
569		      std::forward<_Args>(__args)...);
570      // Construction didn't throw, so can set the new index now:
571      __v._M_index = _Np;
572    }
573
574  template<typename... _Types>
575    using _Variant_storage_alias =
576	_Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
577
578  // The following are (Copy|Move) (ctor|assign) layers for forwarding
579  // triviality and handling non-trivial SMF behaviors.
580
581  template<bool, typename... _Types>
582    struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
583    {
584      using _Base = _Variant_storage_alias<_Types...>;
585      using _Base::_Base;
586
587      _GLIBCXX20_CONSTEXPR
588      _Copy_ctor_base(const _Copy_ctor_base& __rhs)
589	  noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
590      {
591	__variant::__raw_idx_visit(
592	  [this](auto&& __rhs_mem, auto __rhs_index) mutable
593	  {
594	    constexpr size_t __j = __rhs_index;
595	    if constexpr (__j != variant_npos)
596	      std::_Construct(std::__addressof(this->_M_u),
597			      in_place_index<__j>, __rhs_mem);
598	  }, __variant_cast<_Types...>(__rhs));
599	this->_M_index = __rhs._M_index;
600      }
601
602      _Copy_ctor_base(_Copy_ctor_base&&) = default;
603      _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
604      _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
605    };
606
607  template<typename... _Types>
608    struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
609    {
610      using _Base = _Variant_storage_alias<_Types...>;
611      using _Base::_Base;
612    };
613
614  template<typename... _Types>
615    using _Copy_ctor_alias =
616	_Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
617
618  template<bool, typename... _Types>
619    struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
620    {
621      using _Base = _Copy_ctor_alias<_Types...>;
622      using _Base::_Base;
623
624      _GLIBCXX20_CONSTEXPR
625      _Move_ctor_base(_Move_ctor_base&& __rhs)
626	  noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
627      {
628	__variant::__raw_idx_visit(
629	  [this](auto&& __rhs_mem, auto __rhs_index) mutable
630	  {
631	    constexpr size_t __j = __rhs_index;
632	    if constexpr (__j != variant_npos)
633	      std::_Construct(std::__addressof(this->_M_u),
634			      in_place_index<__j>,
635			      std::forward<decltype(__rhs_mem)>(__rhs_mem));
636	  }, __variant_cast<_Types...>(std::move(__rhs)));
637	this->_M_index = __rhs._M_index;
638      }
639
640      _Move_ctor_base(const _Move_ctor_base&) = default;
641      _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
642      _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
643    };
644
645  template<typename... _Types>
646    struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
647    {
648      using _Base = _Copy_ctor_alias<_Types...>;
649      using _Base::_Base;
650    };
651
652  template<typename... _Types>
653    using _Move_ctor_alias =
654	_Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
655
656  template<bool, typename... _Types>
657    struct _Copy_assign_base : _Move_ctor_alias<_Types...>
658    {
659      using _Base = _Move_ctor_alias<_Types...>;
660      using _Base::_Base;
661
662      _GLIBCXX20_CONSTEXPR
663      _Copy_assign_base&
664      operator=(const _Copy_assign_base& __rhs)
665	  noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
666      {
667	__variant::__raw_idx_visit(
668	  [this](auto&& __rhs_mem, auto __rhs_index) mutable
669	  {
670	    constexpr size_t __j = __rhs_index;
671	    if constexpr (__j == variant_npos)
672	      this->_M_reset(); // Make *this valueless.
673	    else if (this->_M_index == __j)
674	      __variant::__get<__j>(*this) = __rhs_mem;
675	    else
676	      {
677		using _Tj = typename _Nth_type<__j, _Types...>::type;
678		if constexpr (is_nothrow_copy_constructible_v<_Tj>
679			      || !is_nothrow_move_constructible_v<_Tj>)
680		  __variant::__emplace<__j>(*this, __rhs_mem);
681		else
682		  {
683		    using _Variant = variant<_Types...>;
684		    _Variant& __self = __variant_cast<_Types...>(*this);
685		    __self = _Variant(in_place_index<__j>, __rhs_mem);
686		  }
687	      }
688	  }, __variant_cast<_Types...>(__rhs));
689	return *this;
690      }
691
692      _Copy_assign_base(const _Copy_assign_base&) = default;
693      _Copy_assign_base(_Copy_assign_base&&) = default;
694      _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
695    };
696
697  template<typename... _Types>
698    struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
699    {
700      using _Base = _Move_ctor_alias<_Types...>;
701      using _Base::_Base;
702    };
703
704  template<typename... _Types>
705    using _Copy_assign_alias =
706      _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
707
708  template<bool, typename... _Types>
709    struct _Move_assign_base : _Copy_assign_alias<_Types...>
710    {
711      using _Base = _Copy_assign_alias<_Types...>;
712      using _Base::_Base;
713
714      _GLIBCXX20_CONSTEXPR
715      _Move_assign_base&
716      operator=(_Move_assign_base&& __rhs)
717	  noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
718      {
719	__variant::__raw_idx_visit(
720	  [this](auto&& __rhs_mem, auto __rhs_index) mutable
721	  {
722	    constexpr size_t __j = __rhs_index;
723	    if constexpr (__j != variant_npos)
724	      {
725		if (this->_M_index == __j)
726		  __variant::__get<__j>(*this) = std::move(__rhs_mem);
727		else
728		  {
729		    using _Tj = typename _Nth_type<__j, _Types...>::type;
730		    if constexpr (is_nothrow_move_constructible_v<_Tj>)
731		      __variant::__emplace<__j>(*this, std::move(__rhs_mem));
732		    else
733		      {
734			using _Variant = variant<_Types...>;
735			_Variant& __self = __variant_cast<_Types...>(*this);
736			__self.template emplace<__j>(std::move(__rhs_mem));
737		      }
738		  }
739	      }
740	    else
741	      this->_M_reset();
742	  }, __variant_cast<_Types...>(__rhs));
743	return *this;
744      }
745
746      _Move_assign_base(const _Move_assign_base&) = default;
747      _Move_assign_base(_Move_assign_base&&) = default;
748      _Move_assign_base& operator=(const _Move_assign_base&) = default;
749    };
750
751  template<typename... _Types>
752    struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
753    {
754      using _Base = _Copy_assign_alias<_Types...>;
755      using _Base::_Base;
756    };
757
758  template<typename... _Types>
759    using _Move_assign_alias =
760      _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
761
762  template<typename... _Types>
763    struct _Variant_base : _Move_assign_alias<_Types...>
764    {
765      using _Base = _Move_assign_alias<_Types...>;
766
767      constexpr
768      _Variant_base() noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
769      : _Variant_base(in_place_index<0>) { }
770
771      template<size_t _Np, typename... _Args>
772	constexpr explicit
773	_Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
774	: _Base(__i, std::forward<_Args>(__args)...)
775	{ }
776
777      _Variant_base(const _Variant_base&) = default;
778      _Variant_base(_Variant_base&&) = default;
779      _Variant_base& operator=(const _Variant_base&) = default;
780      _Variant_base& operator=(_Variant_base&&) = default;
781    };
782
783  template<typename _Tp, typename... _Types>
784    inline constexpr bool __exactly_once
785      = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types);
786
787  // Helper used to check for valid conversions that don't involve narrowing.
788  template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
789
790  // "Build an imaginary function FUN(Ti) for each alternative type Ti"
791  template<size_t _Ind, typename _Tp, typename _Ti, typename = void>
792    struct _Build_FUN
793    {
794      // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
795      // but only static functions will be considered in the call below.
796      void _S_fun();
797    };
798
799  // "... for which Ti x[] = {std::forward<T>(t)}; is well-formed."
800  template<size_t _Ind, typename _Tp, typename _Ti>
801    struct _Build_FUN<_Ind, _Tp, _Ti,
802		      void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
803    {
804      // This is the FUN function for type _Ti, with index _Ind
805      static integral_constant<size_t, _Ind> _S_fun(_Ti);
806    };
807
808  template<typename _Tp, typename _Variant,
809	   typename = make_index_sequence<variant_size_v<_Variant>>>
810    struct _Build_FUNs;
811
812  template<typename _Tp, typename... _Ti, size_t... _Ind>
813    struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
814    : _Build_FUN<_Ind, _Tp, _Ti>...
815    {
816      using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
817    };
818
819  // The index j of the overload FUN(Tj) selected by overload resolution
820  // for FUN(std::forward<_Tp>(t))
821  template<typename _Tp, typename _Variant>
822    using _FUN_type
823      = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
824
825  // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
826  template<typename _Tp, typename _Variant, typename = void>
827    struct __accepted_index
828    : integral_constant<size_t, variant_npos>
829    { };
830
831  template<typename _Tp, typename _Variant>
832    struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
833    : _FUN_type<_Tp, _Variant>
834    { };
835
836  template <typename _Maybe_variant_cookie, typename _Variant>
837    struct _Extra_visit_slot_needed
838    {
839      template <typename> struct _Variant_never_valueless;
840
841      template <typename... _Types>
842	struct _Variant_never_valueless<variant<_Types...>>
843	: bool_constant<__variant::__never_valueless<_Types...>()> {};
844
845      static constexpr bool value =
846	(is_same_v<_Maybe_variant_cookie, __variant_cookie>
847	 || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
848	&& !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
849    };
850
851  // Used for storing a multi-dimensional vtable.
852  template<typename _Tp, size_t... _Dimensions>
853    struct _Multi_array;
854
855  // Partial specialization with rank zero, stores a single _Tp element.
856  template<typename _Tp>
857    struct _Multi_array<_Tp>
858    {
859      template<typename>
860	struct __untag_result
861	: false_type
862	{ using element_type = _Tp; };
863
864      template <typename... _Args>
865	struct __untag_result<const void(*)(_Args...)>
866	: false_type
867	{ using element_type = void(*)(_Args...); };
868
869      template <typename... _Args>
870	struct __untag_result<__variant_cookie(*)(_Args...)>
871	: false_type
872	{ using element_type = void(*)(_Args...); };
873
874      template <typename... _Args>
875	struct __untag_result<__variant_idx_cookie(*)(_Args...)>
876	: false_type
877	{ using element_type = void(*)(_Args...); };
878
879      template <typename _Res, typename... _Args>
880	struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
881	: true_type
882	{ using element_type = _Res(*)(_Args...); };
883
884      using __result_is_deduced = __untag_result<_Tp>;
885
886      constexpr const typename __untag_result<_Tp>::element_type&
887      _M_access() const
888      { return _M_data; }
889
890      typename __untag_result<_Tp>::element_type _M_data;
891    };
892
893  // Partial specialization with rank >= 1.
894  template<typename _Ret,
895	   typename _Visitor,
896	   typename... _Variants,
897	   size_t __first, size_t... __rest>
898    struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
899    {
900      static constexpr size_t __index =
901	sizeof...(_Variants) - sizeof...(__rest) - 1;
902
903      using _Variant = typename _Nth_type<__index, _Variants...>::type;
904
905      static constexpr int __do_cookie =
906	_Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
907
908      using _Tp = _Ret(*)(_Visitor, _Variants...);
909
910      template<typename... _Args>
911	constexpr decltype(auto)
912	_M_access(size_t __first_index, _Args... __rest_indices) const
913        {
914	  return _M_arr[__first_index + __do_cookie]
915	    ._M_access(__rest_indices...);
916	}
917
918      _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
919    };
920
921  // Creates a multi-dimensional vtable recursively.
922  //
923  // For example,
924  // visit([](auto, auto){},
925  //       variant<int, char>(),  // typedef'ed as V1
926  //       variant<float, double, long double>())  // typedef'ed as V2
927  // will trigger instantiations of:
928  // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
929  //                   tuple<V1&&, V2&&>, std::index_sequence<>>
930  //   __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
931  //                     tuple<V1&&, V2&&>, std::index_sequence<0>>
932  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
933  //                       tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
934  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
935  //                       tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
936  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
937  //                       tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
938  //   __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
939  //                     tuple<V1&&, V2&&>, std::index_sequence<1>>
940  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
941  //                       tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
942  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
943  //                       tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
944  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
945  //                       tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
946  // The returned multi-dimensional vtable can be fast accessed by the visitor
947  // using index calculation.
948  template<typename _Array_type, typename _Index_seq>
949    struct __gen_vtable_impl;
950
951  // Defines the _S_apply() member that returns a _Multi_array populated
952  // with function pointers that perform the visitation expressions e(m)
953  // for each valid pack of indexes into the variant types _Variants.
954  //
955  // This partial specialization builds up the index sequences by recursively
956  // calling _S_apply() on the next specialization of __gen_vtable_impl.
957  // The base case of the recursion defines the actual function pointers.
958  template<typename _Result_type, typename _Visitor, size_t... __dimensions,
959	   typename... _Variants, size_t... __indices>
960    struct __gen_vtable_impl<
961	_Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
962	std::index_sequence<__indices...>>
963    {
964      using _Next =
965	  remove_reference_t<typename _Nth_type<sizeof...(__indices),
966			     _Variants...>::type>;
967      using _Array_type =
968	  _Multi_array<_Result_type (*)(_Visitor, _Variants...),
969		       __dimensions...>;
970
971      static constexpr _Array_type
972      _S_apply()
973      {
974	_Array_type __vtable{};
975	_S_apply_all_alts(
976	  __vtable, make_index_sequence<variant_size_v<_Next>>());
977	return __vtable;
978      }
979
980      template<size_t... __var_indices>
981	static constexpr void
982	_S_apply_all_alts(_Array_type& __vtable,
983			  std::index_sequence<__var_indices...>)
984	{
985	  if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
986	    (_S_apply_single_alt<true, __var_indices>(
987	      __vtable._M_arr[__var_indices + 1],
988	      &(__vtable._M_arr[0])), ...);
989	  else
990	    (_S_apply_single_alt<false, __var_indices>(
991	      __vtable._M_arr[__var_indices]), ...);
992	}
993
994      template<bool __do_cookie, size_t __index, typename _Tp>
995	static constexpr void
996	_S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
997	{
998	  if constexpr (__do_cookie)
999	    {
1000	      __element = __gen_vtable_impl<
1001		_Tp,
1002		std::index_sequence<__indices..., __index>>::_S_apply();
1003	      *__cookie_element = __gen_vtable_impl<
1004		_Tp,
1005		std::index_sequence<__indices..., variant_npos>>::_S_apply();
1006	    }
1007	  else
1008	    {
1009	      auto __tmp_element = __gen_vtable_impl<
1010		remove_reference_t<decltype(__element)>,
1011		std::index_sequence<__indices..., __index>>::_S_apply();
1012	      static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
1013			    "std::visit requires the visitor to have the same "
1014			    "return type for all alternatives of a variant");
1015	      __element = __tmp_element;
1016	    }
1017	}
1018    };
1019
1020  // This partial specialization is the base case for the recursion.
1021  // It populates a _Multi_array element with the address of a function
1022  // that invokes the visitor with the alternatives specified by __indices.
1023  template<typename _Result_type, typename _Visitor, typename... _Variants,
1024	   size_t... __indices>
1025    struct __gen_vtable_impl<
1026      _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
1027		   std::index_sequence<__indices...>>
1028    {
1029      using _Array_type =
1030	  _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
1031
1032      template<size_t __index, typename _Variant>
1033	static constexpr decltype(auto)
1034	__element_by_index_or_cookie(_Variant&& __var) noexcept
1035        {
1036	  if constexpr (__index != variant_npos)
1037	    return __variant::__get<__index>(std::forward<_Variant>(__var));
1038	  else
1039	    return __variant_cookie{};
1040	}
1041
1042      static constexpr decltype(auto)
1043      __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1044      {
1045	if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1046	  // For raw visitation using indices, pass the indices to the visitor
1047	  // and discard the return value:
1048	  std::__invoke(std::forward<_Visitor>(__visitor),
1049	      __element_by_index_or_cookie<__indices>(
1050		std::forward<_Variants>(__vars))...,
1051	      integral_constant<size_t, __indices>()...);
1052	else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1053	  // For raw visitation without indices, and discard the return value:
1054	  std::__invoke(std::forward<_Visitor>(__visitor),
1055	      __element_by_index_or_cookie<__indices>(
1056		std::forward<_Variants>(__vars))...);
1057	else if constexpr (_Array_type::__result_is_deduced::value)
1058	  // For the usual std::visit case deduce the return value:
1059	  return std::__invoke(std::forward<_Visitor>(__visitor),
1060	      __element_by_index_or_cookie<__indices>(
1061		std::forward<_Variants>(__vars))...);
1062	else // for std::visit<R> use INVOKE<R>
1063	  return std::__invoke_r<_Result_type>(
1064	      std::forward<_Visitor>(__visitor),
1065	      __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1066      }
1067
1068      static constexpr auto
1069      _S_apply()
1070      {
1071	if constexpr (_Array_type::__result_is_deduced::value)
1072	  {
1073	    constexpr bool __visit_ret_type_mismatch =
1074	      !is_same_v<typename _Result_type::type,
1075			 decltype(__visit_invoke(std::declval<_Visitor>(),
1076				    std::declval<_Variants>()...))>;
1077	    if constexpr (__visit_ret_type_mismatch)
1078	      {
1079		struct __cannot_match {};
1080		return __cannot_match{};
1081	      }
1082	    else
1083	      return _Array_type{&__visit_invoke};
1084	  }
1085	else
1086	  return _Array_type{&__visit_invoke};
1087      }
1088    };
1089
1090  template<typename _Result_type, typename _Visitor, typename... _Variants>
1091    struct __gen_vtable
1092    {
1093      using _Array_type =
1094	  _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1095		       variant_size_v<remove_reference_t<_Variants>>...>;
1096
1097      static constexpr _Array_type _S_vtable
1098	= __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1099    };
1100
1101  template<size_t _Np, typename _Tp>
1102    struct _Base_dedup : public _Tp { };
1103
1104  template<typename _Variant, typename __indices>
1105    struct _Variant_hash_base;
1106
1107  template<typename... _Types, size_t... __indices>
1108    struct _Variant_hash_base<variant<_Types...>,
1109			      std::index_sequence<__indices...>>
1110    : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1111
1112  // Equivalent to decltype(get<_Np>(as-variant(declval<_Variant>())))
1113  template<size_t _Np, typename _Variant,
1114      typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
1115      typename _Tp = variant_alternative_t<_Np, remove_reference_t<_AsV>>>
1116    using __get_t
1117      = __conditional_t<is_lvalue_reference_v<_Variant>, _Tp&, _Tp&&>;
1118
1119  // Return type of std::visit.
1120  template<typename _Visitor, typename... _Variants>
1121    using __visit_result_t
1122      = invoke_result_t<_Visitor, __get_t<0, _Variants>...>;
1123
1124  template<typename _Tp, typename... _Types>
1125    constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1126
1127  template <typename _Visitor, typename _Variant, size_t... _Idxs>
1128    constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
1129    {
1130      return __same_types<
1131	invoke_result_t<_Visitor, __get_t<_Idxs, _Variant>>...
1132	>;
1133    }
1134
1135} // namespace __variant
1136} // namespace __detail
1137
1138  template<typename _Tp, typename... _Types>
1139    constexpr bool
1140    holds_alternative(const variant<_Types...>& __v) noexcept
1141    {
1142      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1143		    "T must occur exactly once in alternatives");
1144      return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>();
1145    }
1146
1147  template<typename _Tp, typename... _Types>
1148    constexpr _Tp&
1149    get(variant<_Types...>& __v)
1150    {
1151      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1152		    "T must occur exactly once in alternatives");
1153      constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1154      return std::get<__n>(__v);
1155    }
1156
1157  template<typename _Tp, typename... _Types>
1158    constexpr _Tp&&
1159    get(variant<_Types...>&& __v)
1160    {
1161      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1162		    "T must occur exactly once in alternatives");
1163      constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1164      return std::get<__n>(std::move(__v));
1165    }
1166
1167  template<typename _Tp, typename... _Types>
1168    constexpr const _Tp&
1169    get(const variant<_Types...>& __v)
1170    {
1171      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1172		    "T must occur exactly once in alternatives");
1173      constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1174      return std::get<__n>(__v);
1175    }
1176
1177  template<typename _Tp, typename... _Types>
1178    constexpr const _Tp&&
1179    get(const variant<_Types...>&& __v)
1180    {
1181      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1182		    "T must occur exactly once in alternatives");
1183      constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1184      return std::get<__n>(std::move(__v));
1185    }
1186
1187  template<size_t _Np, typename... _Types>
1188    constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1189    get_if(variant<_Types...>* __ptr) noexcept
1190    {
1191      using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1192      static_assert(_Np < sizeof...(_Types),
1193		    "The index must be in [0, number of alternatives)");
1194      static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1195      if (__ptr && __ptr->index() == _Np)
1196	return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1197      return nullptr;
1198    }
1199
1200  template<size_t _Np, typename... _Types>
1201    constexpr
1202    add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1203    get_if(const variant<_Types...>* __ptr) noexcept
1204    {
1205      using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1206      static_assert(_Np < sizeof...(_Types),
1207		    "The index must be in [0, number of alternatives)");
1208      static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1209      if (__ptr && __ptr->index() == _Np)
1210	return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1211      return nullptr;
1212    }
1213
1214  template<typename _Tp, typename... _Types>
1215    constexpr add_pointer_t<_Tp>
1216    get_if(variant<_Types...>* __ptr) noexcept
1217    {
1218      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1219		    "T must occur exactly once in alternatives");
1220      static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1221      constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1222      return std::get_if<__n>(__ptr);
1223    }
1224
1225  template<typename _Tp, typename... _Types>
1226    constexpr add_pointer_t<const _Tp>
1227    get_if(const variant<_Types...>* __ptr) noexcept
1228    {
1229      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1230		    "T must occur exactly once in alternatives");
1231      static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1232      constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1233      return std::get_if<__n>(__ptr);
1234    }
1235
1236  struct monostate { };
1237
1238#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1239  template<typename... _Types> \
1240    constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1241				 const variant<_Types...>& __rhs) \
1242    { \
1243      bool __ret = true; \
1244      __detail::__variant::__raw_idx_visit( \
1245        [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
1246        { \
1247	  if constexpr (__rhs_index != variant_npos) \
1248	    { \
1249	      if (__lhs.index() == __rhs_index) \
1250	        { \
1251		  auto& __this_mem = std::get<__rhs_index>(__lhs);	\
1252                  __ret = __this_mem __OP __rhs_mem; \
1253                } \
1254	      else \
1255		__ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1256            } \
1257          else \
1258            __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1259	}, __rhs); \
1260      return __ret; \
1261    }
1262
1263  _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1264  _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1265  _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1266  _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1267  _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1268  _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1269
1270#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1271
1272  constexpr bool operator==(monostate, monostate) noexcept { return true; }
1273
1274#ifdef __cpp_lib_three_way_comparison
1275  template<typename... _Types>
1276    requires (three_way_comparable<_Types> && ...)
1277    constexpr
1278    common_comparison_category_t<compare_three_way_result_t<_Types>...>
1279    operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1280    {
1281      common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1282	= strong_ordering::equal;
1283
1284      __detail::__variant::__raw_idx_visit(
1285	[&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
1286	{
1287	  if constexpr (__w_index != variant_npos)
1288	    {
1289	      if (__v.index() == __w_index)
1290		{
1291		  auto& __this_mem = std::get<__w_index>(__v);
1292		  __ret = __this_mem <=> __w_mem;
1293		  return;
1294		}
1295	    }
1296	  __ret = (__v.index() + 1) <=> (__w_index + 1);
1297	}, __w);
1298      return __ret;
1299    }
1300
1301  constexpr strong_ordering
1302  operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
1303#else
1304  constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1305  constexpr bool operator<(monostate, monostate) noexcept { return false; }
1306  constexpr bool operator>(monostate, monostate) noexcept { return false; }
1307  constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1308  constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1309#endif
1310
1311  template<typename _Visitor, typename... _Variants>
1312    constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1313    visit(_Visitor&&, _Variants&&...);
1314
1315  template<typename... _Types>
1316    _GLIBCXX20_CONSTEXPR
1317    inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1318			&& (is_swappable_v<_Types> && ...)>
1319    swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1320    noexcept(noexcept(__lhs.swap(__rhs)))
1321    { __lhs.swap(__rhs); }
1322
1323  template<typename... _Types>
1324    enable_if_t<!((is_move_constructible_v<_Types> && ...)
1325		   && (is_swappable_v<_Types> && ...))>
1326    swap(variant<_Types...>&, variant<_Types...>&) = delete;
1327
1328  class bad_variant_access : public exception
1329  {
1330  public:
1331    bad_variant_access() noexcept { }
1332
1333    const char* what() const noexcept override
1334    { return _M_reason; }
1335
1336  private:
1337    bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1338
1339    // Must point to a string with static storage duration:
1340    const char* _M_reason = "bad variant access";
1341
1342    friend void __throw_bad_variant_access(const char* __what);
1343  };
1344
1345  // Must only be called with a string literal
1346  inline void
1347  __throw_bad_variant_access(const char* __what)
1348  { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1349
1350  inline void
1351  __throw_bad_variant_access(bool __valueless)
1352  {
1353    if (__valueless) [[__unlikely__]]
1354      __throw_bad_variant_access("std::get: variant is valueless");
1355    else
1356      __throw_bad_variant_access("std::get: wrong index for variant");
1357  }
1358
1359  template<typename... _Types>
1360    class variant
1361    : private __detail::__variant::_Variant_base<_Types...>,
1362      private _Enable_default_constructor<
1363	__detail::__variant::_Traits<_Types...>::_S_default_ctor,
1364	  variant<_Types...>>,
1365      private _Enable_copy_move<
1366	__detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1367	__detail::__variant::_Traits<_Types...>::_S_copy_assign,
1368	__detail::__variant::_Traits<_Types...>::_S_move_ctor,
1369	__detail::__variant::_Traits<_Types...>::_S_move_assign,
1370	variant<_Types...>>
1371    {
1372    private:
1373      template <typename... _UTypes, typename _Tp>
1374	friend _GLIBCXX20_CONSTEXPR decltype(auto)
1375	__variant_cast(_Tp&&);
1376
1377      static_assert(sizeof...(_Types) > 0,
1378		    "variant must have at least one alternative");
1379      static_assert(!(std::is_reference_v<_Types> || ...),
1380		    "variant must have no reference alternative");
1381      static_assert(!(std::is_void_v<_Types> || ...),
1382		    "variant must have no void alternative");
1383
1384      using _Base = __detail::__variant::_Variant_base<_Types...>;
1385      using _Default_ctor_enabler =
1386	_Enable_default_constructor<
1387	  __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1388	    variant<_Types...>>;
1389
1390      template<typename _Tp>
1391	static constexpr bool __not_self
1392	  = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1393
1394      template<typename _Tp>
1395	static constexpr bool
1396	__exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1397
1398      template<typename _Tp>
1399	static constexpr size_t __accepted_index
1400	  = __detail::__variant::__accepted_index<_Tp, variant>::value;
1401
1402      template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1403	using __to_type = typename _Nth_type<_Np, _Types...>::type;
1404
1405      template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1406	using __accepted_type = __to_type<__accepted_index<_Tp>>;
1407
1408      template<typename _Tp>
1409	static constexpr size_t __index_of
1410	  = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1411
1412      using _Traits = __detail::__variant::_Traits<_Types...>;
1413
1414      template<typename _Tp>
1415	struct __is_in_place_tag : false_type { };
1416      template<typename _Tp>
1417	struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1418      template<size_t _Np>
1419	struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1420
1421      template<typename _Tp>
1422	static constexpr bool __not_in_place_tag
1423	  = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1424
1425    public:
1426      variant() = default;
1427      variant(const variant& __rhs) = default;
1428      variant(variant&&) = default;
1429      variant& operator=(const variant&) = default;
1430      variant& operator=(variant&&) = default;
1431      _GLIBCXX20_CONSTEXPR ~variant() = default;
1432
1433      template<typename _Tp,
1434	       typename = enable_if_t<sizeof...(_Types) != 0>,
1435	       typename = enable_if_t<__not_in_place_tag<_Tp>>,
1436	       typename _Tj = __accepted_type<_Tp&&>,
1437	       typename = enable_if_t<__exactly_once<_Tj>
1438				      && is_constructible_v<_Tj, _Tp>>>
1439	constexpr
1440	variant(_Tp&& __t)
1441	noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1442	: variant(in_place_index<__accepted_index<_Tp>>,
1443		  std::forward<_Tp>(__t))
1444	{ }
1445
1446      template<typename _Tp, typename... _Args,
1447	       typename = enable_if_t<__exactly_once<_Tp>
1448				      && is_constructible_v<_Tp, _Args...>>>
1449	constexpr explicit
1450	variant(in_place_type_t<_Tp>, _Args&&... __args)
1451	: variant(in_place_index<__index_of<_Tp>>,
1452		  std::forward<_Args>(__args)...)
1453	{ }
1454
1455      template<typename _Tp, typename _Up, typename... _Args,
1456	       typename = enable_if_t<__exactly_once<_Tp>
1457				      && is_constructible_v<_Tp,
1458					   initializer_list<_Up>&, _Args...>>>
1459	constexpr explicit
1460	variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1461		_Args&&... __args)
1462	: variant(in_place_index<__index_of<_Tp>>, __il,
1463		  std::forward<_Args>(__args)...)
1464	{ }
1465
1466      template<size_t _Np, typename... _Args,
1467	       typename _Tp = __to_type<_Np>,
1468	       typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1469	constexpr explicit
1470	variant(in_place_index_t<_Np>, _Args&&... __args)
1471	: _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1472	_Default_ctor_enabler(_Enable_default_constructor_tag{})
1473	{ }
1474
1475      template<size_t _Np, typename _Up, typename... _Args,
1476	       typename _Tp = __to_type<_Np>,
1477	       typename = enable_if_t<is_constructible_v<_Tp,
1478							 initializer_list<_Up>&,
1479							 _Args...>>>
1480	constexpr explicit
1481	variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1482		_Args&&... __args)
1483	: _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1484	_Default_ctor_enabler(_Enable_default_constructor_tag{})
1485	{ }
1486
1487      template<typename _Tp>
1488	_GLIBCXX20_CONSTEXPR
1489	enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1490		    && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1491		    && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1492		    variant&>
1493	operator=(_Tp&& __rhs)
1494	noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1495		 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1496	{
1497	  constexpr auto __index = __accepted_index<_Tp>;
1498	  if (index() == __index)
1499	    std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1500	  else
1501	    {
1502	      using _Tj = __accepted_type<_Tp&&>;
1503	      if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1504			    || !is_nothrow_move_constructible_v<_Tj>)
1505		this->emplace<__index>(std::forward<_Tp>(__rhs));
1506	      else
1507		// _GLIBCXX_RESOLVE_LIB_DEFECTS
1508		// 3585. converting assignment with immovable alternative
1509		this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
1510	    }
1511	  return *this;
1512	}
1513
1514      template<typename _Tp, typename... _Args>
1515	_GLIBCXX20_CONSTEXPR
1516	enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1517		    _Tp&>
1518	emplace(_Args&&... __args)
1519	{
1520	  constexpr size_t __index = __index_of<_Tp>;
1521	  return this->emplace<__index>(std::forward<_Args>(__args)...);
1522	}
1523
1524      template<typename _Tp, typename _Up, typename... _Args>
1525	_GLIBCXX20_CONSTEXPR
1526	enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1527		    && __exactly_once<_Tp>,
1528		    _Tp&>
1529	emplace(initializer_list<_Up> __il, _Args&&... __args)
1530	{
1531	  constexpr size_t __index = __index_of<_Tp>;
1532	  return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1533	}
1534
1535      template<size_t _Np, typename... _Args>
1536	_GLIBCXX20_CONSTEXPR
1537	enable_if_t<is_constructible_v<__to_type<_Np>, _Args...>,
1538		    __to_type<_Np>&>
1539	emplace(_Args&&... __args)
1540	{
1541	  namespace __variant = std::__detail::__variant;
1542	  using type = typename _Nth_type<_Np, _Types...>::type;
1543	  // Provide the strong exception-safety guarantee when possible,
1544	  // to avoid becoming valueless.
1545	  if constexpr (is_nothrow_constructible_v<type, _Args...>)
1546	    {
1547	      __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1548	    }
1549	  else if constexpr (is_scalar_v<type>)
1550	    {
1551	      // This might invoke a potentially-throwing conversion operator:
1552	      const type __tmp(std::forward<_Args>(__args)...);
1553	      // But this won't throw:
1554	      __variant::__emplace<_Np>(*this, __tmp);
1555	    }
1556	  else if constexpr (__variant::_Never_valueless_alt<type>()
1557	      && _Traits::_S_move_assign)
1558	    {
1559	      // This construction might throw:
1560	      variant __tmp(in_place_index<_Np>,
1561			    std::forward<_Args>(__args)...);
1562	      // But _Never_valueless_alt<type> means this won't:
1563	      *this = std::move(__tmp);
1564	    }
1565	  else
1566	    {
1567	      // This case only provides the basic exception-safety guarantee,
1568	      // i.e. the variant can become valueless.
1569	      __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1570	    }
1571	  return std::get<_Np>(*this);
1572	}
1573
1574      template<size_t _Np, typename _Up, typename... _Args>
1575	_GLIBCXX20_CONSTEXPR
1576	enable_if_t<is_constructible_v<__to_type<_Np>,
1577				       initializer_list<_Up>&, _Args...>,
1578		    __to_type<_Np>&>
1579	emplace(initializer_list<_Up> __il, _Args&&... __args)
1580	{
1581	  namespace __variant = std::__detail::__variant;
1582	  using type = typename _Nth_type<_Np, _Types...>::type;
1583	  // Provide the strong exception-safety guarantee when possible,
1584	  // to avoid becoming valueless.
1585	  if constexpr (is_nothrow_constructible_v<type,
1586						   initializer_list<_Up>&,
1587						   _Args...>)
1588	    {
1589	      __variant::__emplace<_Np>(*this, __il,
1590					std::forward<_Args>(__args)...);
1591	    }
1592	  else if constexpr (__variant::_Never_valueless_alt<type>()
1593	      && _Traits::_S_move_assign)
1594	    {
1595	      // This construction might throw:
1596	      variant __tmp(in_place_index<_Np>, __il,
1597			    std::forward<_Args>(__args)...);
1598	      // But _Never_valueless_alt<type> means this won't:
1599	      *this = std::move(__tmp);
1600	    }
1601	  else
1602	    {
1603	      // This case only provides the basic exception-safety guarantee,
1604	      // i.e. the variant can become valueless.
1605	      __variant::__emplace<_Np>(*this, __il,
1606					std::forward<_Args>(__args)...);
1607	    }
1608	  return std::get<_Np>(*this);
1609	}
1610
1611      template<size_t _Np, typename... _Args>
1612	enable_if_t<!(_Np < sizeof...(_Types))> emplace(_Args&&...) = delete;
1613
1614      template<typename _Tp, typename... _Args>
1615	enable_if_t<!__exactly_once<_Tp>> emplace(_Args&&...) = delete;
1616
1617      constexpr bool valueless_by_exception() const noexcept
1618      { return !this->_M_valid(); }
1619
1620      constexpr size_t index() const noexcept
1621      {
1622	using __index_type = typename _Base::__index_type;
1623	if constexpr (__detail::__variant::__never_valueless<_Types...>())
1624	  return this->_M_index;
1625	else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1626	  return make_signed_t<__index_type>(this->_M_index);
1627	else
1628	  return size_t(__index_type(this->_M_index + 1)) - 1;
1629      }
1630
1631      _GLIBCXX20_CONSTEXPR
1632      void
1633      swap(variant& __rhs)
1634      noexcept((__is_nothrow_swappable<_Types>::value && ...)
1635	       && is_nothrow_move_constructible_v<variant>)
1636      {
1637	static_assert((is_move_constructible_v<_Types> && ...));
1638
1639	// Handle this here to simplify the visitation.
1640	if (__rhs.valueless_by_exception()) [[__unlikely__]]
1641	  {
1642	    if (!this->valueless_by_exception()) [[__likely__]]
1643	      __rhs.swap(*this);
1644	    return;
1645	  }
1646
1647	namespace __variant = __detail::__variant;
1648
1649	__variant::__raw_idx_visit(
1650	  [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1651	  {
1652	    constexpr size_t __j = __rhs_index;
1653	    if constexpr (__j != variant_npos)
1654	      {
1655		if (this->index() == __j)
1656		  {
1657		    using std::swap;
1658		    swap(std::get<__j>(*this), __rhs_mem);
1659		  }
1660		else
1661		  {
1662		    auto __tmp(std::move(__rhs_mem));
1663
1664		    if constexpr (_Traits::_S_trivial_move_assign)
1665		      __rhs = std::move(*this);
1666		    else
1667		      __variant::__raw_idx_visit(
1668			[&__rhs](auto&& __this_mem, auto __this_index) mutable
1669			{
1670			  constexpr size_t __k = __this_index;
1671			  if constexpr (__k != variant_npos)
1672			    __variant::__emplace<__k>(__rhs,
1673						      std::move(__this_mem));
1674			}, *this);
1675
1676		    __variant::__emplace<__j>(*this, std::move(__tmp));
1677		  }
1678	      }
1679	  }, __rhs);
1680      }
1681
1682#if defined(__clang__) && __clang_major__ <= 7
1683    public:
1684      using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1685#endif
1686
1687    private:
1688      template<size_t _Np, typename _Vp>
1689	friend constexpr decltype(auto)
1690	__detail::__variant::__get(_Vp&& __v) noexcept;
1691
1692#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1693      template<typename... _Tp> \
1694	friend constexpr bool \
1695	operator __OP(const variant<_Tp...>& __lhs, \
1696		      const variant<_Tp...>& __rhs);
1697
1698      _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1699      _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1700      _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1701      _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1702      _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1703      _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1704
1705#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1706    };
1707
1708  template<size_t _Np, typename... _Types>
1709    constexpr variant_alternative_t<_Np, variant<_Types...>>&
1710    get(variant<_Types...>& __v)
1711    {
1712      static_assert(_Np < sizeof...(_Types),
1713		    "The index must be in [0, number of alternatives)");
1714      if (__v.index() != _Np)
1715	__throw_bad_variant_access(__v.valueless_by_exception());
1716      return __detail::__variant::__get<_Np>(__v);
1717    }
1718
1719  template<size_t _Np, typename... _Types>
1720    constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1721    get(variant<_Types...>&& __v)
1722    {
1723      static_assert(_Np < sizeof...(_Types),
1724		    "The index must be in [0, number of alternatives)");
1725      if (__v.index() != _Np)
1726	__throw_bad_variant_access(__v.valueless_by_exception());
1727      return __detail::__variant::__get<_Np>(std::move(__v));
1728    }
1729
1730  template<size_t _Np, typename... _Types>
1731    constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1732    get(const variant<_Types...>& __v)
1733    {
1734      static_assert(_Np < sizeof...(_Types),
1735		    "The index must be in [0, number of alternatives)");
1736      if (__v.index() != _Np)
1737	__throw_bad_variant_access(__v.valueless_by_exception());
1738      return __detail::__variant::__get<_Np>(__v);
1739    }
1740
1741  template<size_t _Np, typename... _Types>
1742    constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1743    get(const variant<_Types...>&& __v)
1744    {
1745      static_assert(_Np < sizeof...(_Types),
1746		    "The index must be in [0, number of alternatives)");
1747      if (__v.index() != _Np)
1748	__throw_bad_variant_access(__v.valueless_by_exception());
1749      return __detail::__variant::__get<_Np>(std::move(__v));
1750    }
1751
1752  /// @cond undocumented
1753  template<typename _Result_type, typename _Visitor, typename... _Variants>
1754    constexpr decltype(auto)
1755    __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1756    {
1757      // Get the silly case of visiting no variants out of the way first.
1758      if constexpr (sizeof...(_Variants) == 0)
1759	{
1760	  if constexpr (is_void_v<_Result_type>)
1761	    return (void) std::forward<_Visitor>(__visitor)();
1762	  else
1763	    return std::forward<_Visitor>(__visitor)();
1764	}
1765      else
1766	{
1767	  constexpr size_t __max = 11; // "These go to eleven."
1768
1769	  // The type of the first variant in the pack.
1770	  using _V0 = typename _Nth_type<0, _Variants...>::type;
1771	  // The number of alternatives in that first variant.
1772	  constexpr auto __n = variant_size_v<remove_reference_t<_V0>>;
1773
1774	  if constexpr (sizeof...(_Variants) > 1 || __n > __max)
1775	    {
1776	      // Use a jump table for the general case.
1777	      constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1778		_Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1779
1780	      auto __func_ptr = __vtable._M_access(__variants.index()...);
1781	      return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1782				   std::forward<_Variants>(__variants)...);
1783	    }
1784	  else // We have a single variant with a small number of alternatives.
1785	    {
1786	      // A name for the first variant in the pack.
1787	      _V0& __v0
1788		= [](_V0& __v, ...) -> _V0& { return __v; }(__variants...);
1789
1790	      using __detail::__variant::_Multi_array;
1791	      using __detail::__variant::__gen_vtable_impl;
1792	      using _Ma = _Multi_array<_Result_type (*)(_Visitor&&, _V0&&)>;
1793
1794#ifdef _GLIBCXX_DEBUG
1795# define _GLIBCXX_VISIT_UNREACHABLE __builtin_trap
1796#else
1797# define _GLIBCXX_VISIT_UNREACHABLE __builtin_unreachable
1798#endif
1799
1800#define _GLIBCXX_VISIT_CASE(N)						\
1801  case N:								\
1802  {									\
1803    if constexpr (N < __n)						\
1804      {									\
1805	return __gen_vtable_impl<_Ma, index_sequence<N>>::		\
1806	  __visit_invoke(std::forward<_Visitor>(__visitor),		\
1807			 std::forward<_V0>(__v0));		\
1808      }									\
1809    else _GLIBCXX_VISIT_UNREACHABLE();					\
1810  }
1811
1812	      switch (__v0.index())
1813		{
1814		  _GLIBCXX_VISIT_CASE(0)
1815		  _GLIBCXX_VISIT_CASE(1)
1816		  _GLIBCXX_VISIT_CASE(2)
1817		  _GLIBCXX_VISIT_CASE(3)
1818		  _GLIBCXX_VISIT_CASE(4)
1819		  _GLIBCXX_VISIT_CASE(5)
1820		  _GLIBCXX_VISIT_CASE(6)
1821		  _GLIBCXX_VISIT_CASE(7)
1822		  _GLIBCXX_VISIT_CASE(8)
1823		  _GLIBCXX_VISIT_CASE(9)
1824		  _GLIBCXX_VISIT_CASE(10)
1825		case variant_npos:
1826		  using __detail::__variant::__variant_idx_cookie;
1827		  using __detail::__variant::__variant_cookie;
1828		  if constexpr (is_same_v<_Result_type, __variant_idx_cookie>
1829				|| is_same_v<_Result_type, __variant_cookie>)
1830		    {
1831		      using _Npos = index_sequence<variant_npos>;
1832		      return __gen_vtable_impl<_Ma, _Npos>::
1833			__visit_invoke(std::forward<_Visitor>(__visitor),
1834				       std::forward<_V0>(__v0));
1835		    }
1836		  else
1837		    _GLIBCXX_VISIT_UNREACHABLE();
1838		default:
1839		  _GLIBCXX_VISIT_UNREACHABLE();
1840		}
1841#undef _GLIBCXX_VISIT_CASE
1842#undef _GLIBCXX_VISIT_UNREACHABLE
1843	    }
1844	}
1845    }
1846  /// @endcond
1847
1848  template<typename _Visitor, typename... _Variants>
1849    constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1850    visit(_Visitor&& __visitor, _Variants&&... __variants)
1851    {
1852      namespace __variant = std::__detail::__variant;
1853
1854      if ((__variant::__as(__variants).valueless_by_exception() || ...))
1855	__throw_bad_variant_access("std::visit: variant is valueless");
1856
1857      using _Result_type
1858	= __detail::__variant::__visit_result_t<_Visitor, _Variants...>;
1859
1860      using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1861
1862      if constexpr (sizeof...(_Variants) == 1)
1863	{
1864	  using _Vp = decltype(__variant::__as(std::declval<_Variants>()...));
1865
1866	  constexpr bool __visit_rettypes_match = __detail::__variant::
1867	    __check_visitor_results<_Visitor, _Vp>(
1868	      make_index_sequence<variant_size_v<remove_reference_t<_Vp>>>());
1869	  if constexpr (!__visit_rettypes_match)
1870	    {
1871	      static_assert(__visit_rettypes_match,
1872			  "std::visit requires the visitor to have the same "
1873			  "return type for all alternatives of a variant");
1874	      return;
1875	    }
1876	  else
1877	    return std::__do_visit<_Tag>(
1878	      std::forward<_Visitor>(__visitor),
1879	      static_cast<_Vp>(__variants)...);
1880	}
1881      else
1882	return std::__do_visit<_Tag>(
1883	  std::forward<_Visitor>(__visitor),
1884	  __variant::__as(std::forward<_Variants>(__variants))...);
1885    }
1886
1887#if __cplusplus > 201703L
1888  template<typename _Res, typename _Visitor, typename... _Variants>
1889    constexpr _Res
1890    visit(_Visitor&& __visitor, _Variants&&... __variants)
1891    {
1892      namespace __variant = std::__detail::__variant;
1893
1894      if ((__variant::__as(__variants).valueless_by_exception() || ...))
1895	__throw_bad_variant_access("std::visit<R>: variant is valueless");
1896
1897      return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1898	  __variant::__as(std::forward<_Variants>(__variants))...);
1899    }
1900#endif
1901
1902  /// @cond undocumented
1903  template<bool, typename... _Types>
1904    struct __variant_hash_call_base_impl
1905    {
1906      size_t
1907      operator()(const variant<_Types...>& __t) const
1908      noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1909      {
1910	size_t __ret;
1911	__detail::__variant::__raw_visit(
1912	  [&__t, &__ret](auto&& __t_mem) mutable
1913	  {
1914	    using _Type = __remove_cvref_t<decltype(__t_mem)>;
1915	    if constexpr (!is_same_v<_Type,
1916			             __detail::__variant::__variant_cookie>)
1917	      __ret = std::hash<size_t>{}(__t.index())
1918		      + std::hash<_Type>{}(__t_mem);
1919	    else
1920	      __ret = std::hash<size_t>{}(__t.index());
1921	  }, __t);
1922	return __ret;
1923      }
1924    };
1925
1926  template<typename... _Types>
1927    struct __variant_hash_call_base_impl<false, _Types...> {};
1928
1929  template<typename... _Types>
1930    using __variant_hash_call_base =
1931    __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1932				   __enable_hash_call &&...), _Types...>;
1933  /// @endcond
1934
1935  template<typename... _Types>
1936    struct hash<variant<_Types...>>
1937    : private __detail::__variant::_Variant_hash_base<
1938	variant<_Types...>, std::index_sequence_for<_Types...>>,
1939      public __variant_hash_call_base<_Types...>
1940    {
1941      using result_type [[__deprecated__]] = size_t;
1942      using argument_type [[__deprecated__]] = variant<_Types...>;
1943    };
1944
1945  template<>
1946    struct hash<monostate>
1947    {
1948      using result_type [[__deprecated__]] = size_t;
1949      using argument_type [[__deprecated__]] = monostate;
1950
1951      size_t
1952      operator()(const monostate&) const noexcept
1953      {
1954	constexpr size_t __magic_monostate_hash = -7777;
1955	return __magic_monostate_hash;
1956      }
1957    };
1958
1959  template<typename... _Types>
1960    struct __is_fast_hash<hash<variant<_Types...>>>
1961    : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1962    { };
1963
1964_GLIBCXX_END_NAMESPACE_VERSION
1965} // namespace std
1966
1967#endif // C++17
1968
1969#endif // _GLIBCXX_VARIANT
1970