1// <variant> -*- C++ -*-
2
3// Copyright (C) 2016-2018 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 <type_traits>
37#include <utility>
38#include <bits/enable_special_members.h>
39#include <bits/functexcept.h>
40#include <bits/move.h>
41#include <bits/functional_hash.h>
42#include <bits/invoke.h>
43#include <ext/aligned_buffer.h>
44#include <bits/parse_numbers.h>
45#include <bits/stl_iterator_base_types.h>
46#include <bits/stl_iterator_base_funcs.h>
47#include <bits/stl_construct.h>
48
49namespace std _GLIBCXX_VISIBILITY(default)
50{
51_GLIBCXX_BEGIN_NAMESPACE_VERSION
52
53namespace __detail
54{
55namespace __variant
56{
57  template<size_t _Np, typename... _Types>
58    struct _Nth_type;
59
60  template<size_t _Np, typename _First, typename... _Rest>
61    struct _Nth_type<_Np, _First, _Rest...>
62    : _Nth_type<_Np-1, _Rest...> { };
63
64  template<typename _First, typename... _Rest>
65    struct _Nth_type<0, _First, _Rest...>
66    { using type = _First; };
67
68} // namespace __variant
69} // namespace __detail
70
71#define __cpp_lib_variant 201606L
72
73  template<typename... _Types> class tuple;
74  template<typename... _Types> class variant;
75  template <typename> struct hash;
76
77  template<typename _Variant>
78    struct variant_size;
79
80  template<typename _Variant>
81    struct variant_size<const _Variant> : variant_size<_Variant> {};
82
83  template<typename _Variant>
84    struct variant_size<volatile _Variant> : variant_size<_Variant> {};
85
86  template<typename _Variant>
87    struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
88
89  template<typename... _Types>
90    struct variant_size<variant<_Types...>>
91    : std::integral_constant<size_t, sizeof...(_Types)> {};
92
93  template<typename _Variant>
94    inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
95
96  template<size_t _Np, typename _Variant>
97    struct variant_alternative;
98
99  template<size_t _Np, typename _First, typename... _Rest>
100    struct variant_alternative<_Np, variant<_First, _Rest...>>
101    : variant_alternative<_Np-1, variant<_Rest...>> {};
102
103  template<typename _First, typename... _Rest>
104    struct variant_alternative<0, variant<_First, _Rest...>>
105    { using type = _First; };
106
107  template<size_t _Np, typename _Variant>
108    using variant_alternative_t =
109      typename variant_alternative<_Np, _Variant>::type;
110
111  template<size_t _Np, typename _Variant>
112    struct variant_alternative<_Np, const _Variant>
113    { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
114
115  template<size_t _Np, typename _Variant>
116    struct variant_alternative<_Np, volatile _Variant>
117    { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
118
119  template<size_t _Np, typename _Variant>
120    struct variant_alternative<_Np, const volatile _Variant>
121    { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
122
123  inline constexpr size_t variant_npos = -1;
124
125  template<size_t _Np, typename... _Types>
126    constexpr variant_alternative_t<_Np, variant<_Types...>>&
127    get(variant<_Types...>&);
128
129  template<size_t _Np, typename... _Types>
130    constexpr variant_alternative_t<_Np, variant<_Types...>>&&
131    get(variant<_Types...>&&);
132
133  template<size_t _Np, typename... _Types>
134    constexpr variant_alternative_t<_Np, variant<_Types...>> const&
135    get(const variant<_Types...>&);
136
137  template<size_t _Np, typename... _Types>
138    constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
139    get(const variant<_Types...>&&);
140
141namespace __detail
142{
143namespace __variant
144{
145  // Returns the first apparence of _Tp in _Types.
146  // Returns sizeof...(_Types) if _Tp is not in _Types.
147  template<typename _Tp, typename... _Types>
148    struct __index_of : std::integral_constant<size_t, 0> {};
149
150  template<typename _Tp, typename... _Types>
151    inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value;
152
153  template<typename _Tp, typename _First, typename... _Rest>
154    struct __index_of<_Tp, _First, _Rest...> :
155      std::integral_constant<size_t, is_same_v<_Tp, _First>
156	? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
157
158  // _Uninitialized<T> is guaranteed to be a literal type, even if T is not.
159  // We have to do this, because [basic.types]p10.5.3 (n4606) is not implemented
160  // yet. When it's implemented, _Uninitialized<T> can be changed to the alias
161  // to T, therefore equivalent to being removed entirely.
162  //
163  // Another reason we may not want to remove _Uninitialzied<T> may be that, we
164  // want _Uninitialized<T> to be trivially destructible, no matter whether T
165  // is; but we will see.
166  template<typename _Type, bool = std::is_literal_type_v<_Type>>
167    struct _Uninitialized;
168
169  template<typename _Type>
170    struct _Uninitialized<_Type, true>
171    {
172      template<typename... _Args>
173      constexpr _Uninitialized(in_place_index_t<0>, _Args&&... __args)
174      : _M_storage(std::forward<_Args>(__args)...)
175      { }
176
177      constexpr const _Type& _M_get() const &
178      { return _M_storage; }
179
180      constexpr _Type& _M_get() &
181      { return _M_storage; }
182
183      constexpr const _Type&& _M_get() const &&
184      { return std::move(_M_storage); }
185
186      constexpr _Type&& _M_get() &&
187      { return std::move(_M_storage); }
188
189      _Type _M_storage;
190    };
191
192  template<typename _Type>
193    struct _Uninitialized<_Type, false>
194    {
195      template<typename... _Args>
196      constexpr _Uninitialized(in_place_index_t<0>, _Args&&... __args)
197      { ::new (&_M_storage) _Type(std::forward<_Args>(__args)...); }
198
199      const _Type& _M_get() const &
200      { return *_M_storage._M_ptr(); }
201
202      _Type& _M_get() &
203      { return *_M_storage._M_ptr(); }
204
205      const _Type&& _M_get() const &&
206      { return std::move(*_M_storage._M_ptr()); }
207
208      _Type&& _M_get() &&
209      { return std::move(*_M_storage._M_ptr()); }
210
211      __gnu_cxx::__aligned_membuf<_Type> _M_storage;
212    };
213
214  template<typename _Ref>
215    _Ref __ref_cast(void* __ptr)
216    {
217      return static_cast<_Ref>(*static_cast<remove_reference_t<_Ref>*>(__ptr));
218    }
219
220  template<typename _Union>
221    constexpr decltype(auto) __get(in_place_index_t<0>, _Union&& __u)
222    { return std::forward<_Union>(__u)._M_first._M_get(); }
223
224  template<size_t _Np, typename _Union>
225    constexpr decltype(auto) __get(in_place_index_t<_Np>, _Union&& __u)
226    {
227      return __variant::__get(in_place_index<_Np-1>,
228			      std::forward<_Union>(__u)._M_rest);
229    }
230
231  // Returns the typed storage for __v.
232  template<size_t _Np, typename _Variant>
233    constexpr decltype(auto) __get(_Variant&& __v)
234    {
235      return __variant::__get(std::in_place_index<_Np>,
236			      std::forward<_Variant>(__v)._M_u);
237    }
238
239  // Various functions as "vtable" entries, where those vtables are used by
240  // polymorphic operations.
241  template<typename _Lhs, typename _Rhs>
242    void
243    __erased_ctor(void* __lhs, void* __rhs)
244    {
245      using _Type = remove_reference_t<_Lhs>;
246      ::new (__lhs) _Type(__variant::__ref_cast<_Rhs>(__rhs));
247    }
248
249  template<typename _Variant, size_t _Np>
250    void
251    __erased_dtor(_Variant&& __v)
252    { std::_Destroy(std::__addressof(__variant::__get<_Np>(__v))); }
253
254  template<typename _Lhs, typename _Rhs>
255    void
256    __erased_assign(void* __lhs, void* __rhs)
257    {
258      __variant::__ref_cast<_Lhs>(__lhs) = __variant::__ref_cast<_Rhs>(__rhs);
259    }
260
261  template<typename _Lhs, typename _Rhs>
262    void
263    __erased_swap(void* __lhs, void* __rhs)
264    {
265      using std::swap;
266      swap(__variant::__ref_cast<_Lhs>(__lhs),
267	   __variant::__ref_cast<_Rhs>(__rhs));
268    }
269
270#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
271  template<typename _Variant, size_t _Np> \
272    constexpr bool \
273    __erased_##__NAME(const _Variant& __lhs, const _Variant& __rhs) \
274    { \
275      return __variant::__get<_Np>(std::forward<_Variant>(__lhs)) \
276	  __OP __variant::__get<_Np>(std::forward<_Variant>(__rhs)); \
277    }
278
279  _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
280  _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
281  _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
282  _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
283  _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
284  _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
285
286#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
287
288  template<typename _Tp>
289    size_t
290    __erased_hash(void* __t)
291    {
292      return std::hash<remove_cv_t<remove_reference_t<_Tp>>>{}(
293	  __variant::__ref_cast<_Tp>(__t));
294    }
295
296  template<typename... _Types>
297    struct _Traits
298    {
299      static constexpr bool _S_default_ctor =
300	  is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
301      static constexpr bool _S_copy_ctor =
302	  (is_copy_constructible_v<_Types> && ...);
303      static constexpr bool _S_move_ctor =
304	  (is_move_constructible_v<_Types> && ...);
305      static constexpr bool _S_copy_assign =
306	  _S_copy_ctor && _S_move_ctor
307	  && (is_copy_assignable_v<_Types> && ...);
308      static constexpr bool _S_move_assign =
309	  _S_move_ctor
310	  && (is_move_assignable_v<_Types> && ...);
311
312      static constexpr bool _S_trivial_dtor =
313	  (is_trivially_destructible_v<_Types> && ...);
314      static constexpr bool _S_trivial_copy_ctor =
315	  (is_trivially_copy_constructible_v<_Types> && ...);
316      static constexpr bool _S_trivial_move_ctor =
317	  (is_trivially_move_constructible_v<_Types> && ...);
318      static constexpr bool _S_trivial_copy_assign =
319	  _S_trivial_dtor && (is_trivially_copy_assignable_v<_Types> && ...);
320      static constexpr bool _S_trivial_move_assign =
321	  _S_trivial_dtor && (is_trivially_move_assignable_v<_Types> && ...);
322
323      // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
324      // are always nothrow.
325      static constexpr bool _S_nothrow_default_ctor =
326	  is_nothrow_default_constructible_v<
327	      typename _Nth_type<0, _Types...>::type>;
328      static constexpr bool _S_nothrow_copy_ctor = false;
329      static constexpr bool _S_nothrow_move_ctor =
330	  (is_nothrow_move_constructible_v<_Types> && ...);
331      static constexpr bool _S_nothrow_copy_assign = false;
332      static constexpr bool _S_nothrow_move_assign =
333	  _S_nothrow_move_ctor && (is_nothrow_move_assignable_v<_Types> && ...);
334    };
335
336  // Defines members and ctors.
337  template<typename... _Types>
338    union _Variadic_union { };
339
340  template<typename _First, typename... _Rest>
341    union _Variadic_union<_First, _Rest...>
342    {
343      constexpr _Variadic_union() : _M_rest() { }
344
345      template<typename... _Args>
346	constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
347	: _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
348	{ }
349
350      template<size_t _Np, typename... _Args>
351	constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
352	: _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
353	{ }
354
355      _Uninitialized<_First> _M_first;
356      _Variadic_union<_Rest...> _M_rest;
357    };
358
359  // Defines index and the dtor, possibly trivial.
360  template<bool __trivially_destructible, typename... _Types>
361    struct _Variant_storage;
362
363  template <typename... _Types>
364  using __select_index =
365    typename __select_int::_Select_int_base<sizeof...(_Types) + 1,
366					    unsigned char,
367					    unsigned short>::type::value_type;
368
369  template<typename... _Types>
370    struct _Variant_storage<false, _Types...>
371    {
372      template<size_t... __indices>
373	static constexpr void (*_S_vtable[])(const _Variant_storage&) =
374	    { &__erased_dtor<const _Variant_storage&, __indices>... };
375
376      constexpr _Variant_storage() : _M_index(variant_npos) { }
377
378      template<size_t _Np, typename... _Args>
379	constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
380	: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
381	_M_index(_Np)
382	{ }
383
384      template<size_t... __indices>
385	constexpr void _M_reset_impl(std::index_sequence<__indices...>)
386	{
387	  if (_M_index != __index_type(variant_npos))
388	    _S_vtable<__indices...>[_M_index](*this);
389	}
390
391      void _M_reset()
392      {
393	_M_reset_impl(std::index_sequence_for<_Types...>{});
394	_M_index = variant_npos;
395      }
396
397      ~_Variant_storage()
398      { _M_reset(); }
399
400      void*
401      _M_storage() const
402      {
403	return const_cast<void*>(static_cast<const void*>(
404	    std::addressof(_M_u)));
405      }
406
407      constexpr bool
408      _M_valid() const noexcept
409      {
410	return this->_M_index != __index_type(variant_npos);
411      }
412
413      _Variadic_union<_Types...> _M_u;
414      using __index_type = __select_index<_Types...>;
415      __index_type _M_index;
416    };
417
418  template<typename... _Types>
419    struct _Variant_storage<true, _Types...>
420    {
421      constexpr _Variant_storage() : _M_index(variant_npos) { }
422
423      template<size_t _Np, typename... _Args>
424	constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
425	: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
426	_M_index(_Np)
427	{ }
428
429      void _M_reset()
430      { _M_index = variant_npos; }
431
432      void*
433      _M_storage() const
434      {
435	return const_cast<void*>(static_cast<const void*>(
436	    std::addressof(_M_u)));
437      }
438
439      constexpr bool
440      _M_valid() const noexcept
441      {
442	return this->_M_index != __index_type(variant_npos);
443      }
444
445      _Variadic_union<_Types...> _M_u;
446      using __index_type = __select_index<_Types...>;
447      __index_type _M_index;
448    };
449
450  template<typename... _Types>
451    using _Variant_storage_alias =
452	_Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
453
454  // The following are (Copy|Move) (ctor|assign) layers for forwarding
455  // triviality and handling non-trivial SMF behaviors.
456
457  template<bool, typename... _Types>
458    struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
459    {
460      using _Base = _Variant_storage_alias<_Types...>;
461      using _Base::_Base;
462
463      _Copy_ctor_base(const _Copy_ctor_base& __rhs)
464	  noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
465      {
466	if (__rhs._M_valid())
467	  {
468	    static constexpr void (*_S_vtable[])(void*, void*) =
469	      { &__erased_ctor<_Types&, const _Types&>... };
470	    _S_vtable[__rhs._M_index](this->_M_storage(), __rhs._M_storage());
471	    this->_M_index = __rhs._M_index;
472	  }
473      }
474
475      _Copy_ctor_base(_Copy_ctor_base&&) = default;
476      _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
477      _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
478    };
479
480  template<typename... _Types>
481    struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
482    {
483      using _Base = _Variant_storage_alias<_Types...>;
484      using _Base::_Base;
485    };
486
487  template<typename... _Types>
488    using _Copy_ctor_alias =
489	_Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
490
491  template<bool, typename... _Types>
492    struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
493    {
494      using _Base = _Copy_ctor_alias<_Types...>;
495      using _Base::_Base;
496
497      _Move_ctor_base(_Move_ctor_base&& __rhs)
498	  noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
499      {
500	if (__rhs._M_valid())
501	  {
502	    static constexpr void (*_S_vtable[])(void*, void*) =
503	      { &__erased_ctor<_Types&, _Types&&>... };
504	    _S_vtable[__rhs._M_index](this->_M_storage(), __rhs._M_storage());
505	    this->_M_index = __rhs._M_index;
506	  }
507      }
508
509      void _M_destructive_move(_Move_ctor_base&& __rhs)
510      {
511	this->~_Move_ctor_base();
512	__try
513	  {
514	    ::new (this) _Move_ctor_base(std::move(__rhs));
515	  }
516	__catch (...)
517	  {
518	    this->_M_index = variant_npos;
519	    __throw_exception_again;
520	  }
521      }
522
523      _Move_ctor_base(const _Move_ctor_base&) = default;
524      _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
525      _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
526    };
527
528  template<typename... _Types>
529    struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
530    {
531      using _Base = _Copy_ctor_alias<_Types...>;
532      using _Base::_Base;
533
534      void _M_destructive_move(_Move_ctor_base&& __rhs)
535      {
536	this->~_Move_ctor_base();
537	::new (this) _Move_ctor_base(std::move(__rhs));
538      }
539    };
540
541  template<typename... _Types>
542    using _Move_ctor_alias =
543	_Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
544
545  template<bool, typename... _Types>
546    struct _Copy_assign_base : _Move_ctor_alias<_Types...>
547    {
548      using _Base = _Move_ctor_alias<_Types...>;
549      using _Base::_Base;
550
551      _Copy_assign_base&
552      operator=(const _Copy_assign_base& __rhs)
553	  noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
554      {
555	if (this->_M_index == __rhs._M_index)
556	  {
557	    if (__rhs._M_valid())
558	      {
559		static constexpr void (*_S_vtable[])(void*, void*) =
560		  { &__erased_assign<_Types&, const _Types&>... };
561		_S_vtable[__rhs._M_index](this->_M_storage(),
562					  __rhs._M_storage());
563	      }
564	  }
565	else
566	  {
567	    _Copy_assign_base __tmp(__rhs);
568	    this->_M_destructive_move(std::move(__tmp));
569	  }
570	__glibcxx_assert(this->_M_index == __rhs._M_index);
571	return *this;
572      }
573
574      _Copy_assign_base(const _Copy_assign_base&) = default;
575      _Copy_assign_base(_Copy_assign_base&&) = default;
576      _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
577    };
578
579  template<typename... _Types>
580    struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
581    {
582      using _Base = _Move_ctor_alias<_Types...>;
583      using _Base::_Base;
584    };
585
586  template<typename... _Types>
587    using _Copy_assign_alias =
588	_Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign,
589			  _Types...>;
590
591  template<bool, typename... _Types>
592    struct _Move_assign_base : _Copy_assign_alias<_Types...>
593    {
594      using _Base = _Copy_assign_alias<_Types...>;
595      using _Base::_Base;
596
597      _Move_assign_base&
598      operator=(_Move_assign_base&& __rhs)
599	  noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
600      {
601	if (this->_M_index == __rhs._M_index)
602	  {
603	    if (__rhs._M_valid())
604	      {
605		static constexpr void (*_S_vtable[])(void*, void*) =
606		  { &__erased_assign<_Types&, _Types&&>... };
607		_S_vtable[__rhs._M_index]
608		  (this->_M_storage(), __rhs._M_storage());
609	      }
610	  }
611	else
612	  {
613	    _Move_assign_base __tmp(std::move(__rhs));
614	    this->_M_destructive_move(std::move(__tmp));
615	  }
616	__glibcxx_assert(this->_M_index == __rhs._M_index);
617	return *this;
618      }
619
620      _Move_assign_base(const _Move_assign_base&) = default;
621      _Move_assign_base(_Move_assign_base&&) = default;
622      _Move_assign_base& operator=(const _Move_assign_base&) = default;
623    };
624
625  template<typename... _Types>
626    struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
627    {
628      using _Base = _Copy_assign_alias<_Types...>;
629      using _Base::_Base;
630    };
631
632  template<typename... _Types>
633    using _Move_assign_alias =
634	_Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign,
635			  _Types...>;
636
637  template<typename... _Types>
638    struct _Variant_base : _Move_assign_alias<_Types...>
639    {
640      using _Base = _Move_assign_alias<_Types...>;
641
642      constexpr
643      _Variant_base()
644	  noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
645      : _Variant_base(in_place_index<0>) { }
646
647      template<size_t _Np, typename... _Args>
648	constexpr explicit
649	_Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
650	: _Base(__i, std::forward<_Args>(__args)...)
651	{ }
652
653      _Variant_base(const _Variant_base&) = default;
654      _Variant_base(_Variant_base&&) = default;
655      _Variant_base& operator=(const _Variant_base&) = default;
656      _Variant_base& operator=(_Variant_base&&) = default;
657    };
658
659  // For how many times does _Tp appear in _Tuple?
660  template<typename _Tp, typename _Tuple>
661    struct __tuple_count;
662
663  template<typename _Tp, typename _Tuple>
664    inline constexpr size_t __tuple_count_v =
665      __tuple_count<_Tp, _Tuple>::value;
666
667  template<typename _Tp, typename... _Types>
668    struct __tuple_count<_Tp, tuple<_Types...>>
669    : integral_constant<size_t, 0> { };
670
671  template<typename _Tp, typename _First, typename... _Rest>
672    struct __tuple_count<_Tp, tuple<_First, _Rest...>>
673    : integral_constant<
674	size_t,
675	__tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
676
677  // TODO: Reuse this in <tuple> ?
678  template<typename _Tp, typename... _Types>
679    inline constexpr bool __exactly_once =
680      __tuple_count_v<_Tp, tuple<_Types...>> == 1;
681
682  // Takes _Types and create an overloaded _S_fun for each type.
683  // If a type appears more than once in _Types, create only one overload.
684  template<typename... _Types>
685    struct __overload_set
686    { static void _S_fun(); };
687
688  template<typename _First, typename... _Rest>
689    struct __overload_set<_First, _Rest...> : __overload_set<_Rest...>
690    {
691      using __overload_set<_Rest...>::_S_fun;
692      static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
693    };
694
695  template<typename... _Rest>
696    struct __overload_set<void, _Rest...> : __overload_set<_Rest...>
697    {
698      using __overload_set<_Rest...>::_S_fun;
699    };
700
701  // Helper for variant(_Tp&&) and variant::operator=(_Tp&&).
702  // __accepted_index maps the arbitrary _Tp to an alternative type in _Variant.
703  template<typename _Tp, typename _Variant, typename = void>
704    struct __accepted_index
705    { static constexpr size_t value = variant_npos; };
706
707  template<typename _Tp, typename... _Types>
708    struct __accepted_index<
709      _Tp, variant<_Types...>,
710      decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()),
711	       std::declval<void>())>
712    {
713      static constexpr size_t value = sizeof...(_Types) - 1
714	- decltype(__overload_set<_Types...>::
715		   _S_fun(std::declval<_Tp>()))::value;
716    };
717
718  // Returns the raw storage for __v.
719  template<typename _Variant>
720    void* __get_storage(_Variant&& __v)
721    { return __v._M_storage(); }
722
723  // Used for storing multi-dimensional vtable.
724  template<typename _Tp, size_t... _Dimensions>
725    struct _Multi_array
726    {
727      constexpr const _Tp&
728      _M_access() const
729      { return _M_data; }
730
731      _Tp _M_data;
732    };
733
734  template<typename _Tp, size_t __first, size_t... __rest>
735    struct _Multi_array<_Tp, __first, __rest...>
736    {
737      template<typename... _Args>
738	constexpr const _Tp&
739	_M_access(size_t __first_index, _Args... __rest_indices) const
740	{ return _M_arr[__first_index]._M_access(__rest_indices...); }
741
742      _Multi_array<_Tp, __rest...> _M_arr[__first];
743    };
744
745  // Creates a multi-dimensional vtable recursively.
746  //
747  // For example,
748  // visit([](auto, auto){},
749  //       variant<int, char>(),  // typedef'ed as V1
750  //       variant<float, double, long double>())  // typedef'ed as V2
751  // will trigger instantiations of:
752  // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
753  //                   tuple<V1&&, V2&&>, std::index_sequence<>>
754  //   __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
755  //                     tuple<V1&&, V2&&>, std::index_sequence<0>>
756  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
757  //                       tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
758  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
759  //                       tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
760  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
761  //                       tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
762  //   __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
763  //                     tuple<V1&&, V2&&>, std::index_sequence<1>>
764  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
765  //                       tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
766  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
767  //                       tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
768  //     __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
769  //                       tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
770  // The returned multi-dimensional vtable can be fast accessed by the visitor
771  // using index calculation.
772  template<typename _Array_type, typename _Variant_tuple, typename _Index_seq>
773    struct __gen_vtable_impl;
774
775  template<typename _Result_type, typename _Visitor, size_t... __dimensions,
776	   typename... _Variants, size_t... __indices>
777    struct __gen_vtable_impl<
778	_Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
779	tuple<_Variants...>, std::index_sequence<__indices...>>
780    {
781      using _Next =
782	  remove_reference_t<typename _Nth_type<sizeof...(__indices),
783			     _Variants...>::type>;
784      using _Array_type =
785	  _Multi_array<_Result_type (*)(_Visitor, _Variants...),
786		       __dimensions...>;
787
788      static constexpr _Array_type
789      _S_apply()
790      {
791	_Array_type __vtable{};
792	_S_apply_all_alts(
793	  __vtable, make_index_sequence<variant_size_v<_Next>>());
794	return __vtable;
795      }
796
797      template<size_t... __var_indices>
798	static constexpr void
799	_S_apply_all_alts(_Array_type& __vtable,
800			  std::index_sequence<__var_indices...>)
801	{
802	  (_S_apply_single_alt<__var_indices>(
803	     __vtable._M_arr[__var_indices]), ...);
804	}
805
806      template<size_t __index, typename _Tp>
807	static constexpr void
808	_S_apply_single_alt(_Tp& __element)
809	{
810	  using _Alternative = variant_alternative_t<__index, _Next>;
811	  __element = __gen_vtable_impl<
812	    remove_reference_t<decltype(__element)>, tuple<_Variants...>,
813	    std::index_sequence<__indices..., __index>>::_S_apply();
814	}
815    };
816
817  template<typename _Result_type, typename _Visitor, typename... _Variants,
818	   size_t... __indices>
819    struct __gen_vtable_impl<
820      _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
821		   tuple<_Variants...>, std::index_sequence<__indices...>>
822    {
823      using _Array_type =
824	  _Multi_array<_Result_type (*)(_Visitor&&, _Variants...)>;
825
826      static constexpr decltype(auto)
827      __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
828      {
829	return std::__invoke(std::forward<_Visitor>(__visitor),
830	    __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
831      }
832
833      static constexpr auto
834      _S_apply()
835      { return _Array_type{&__visit_invoke}; }
836    };
837
838  template<typename _Result_type, typename _Visitor, typename... _Variants>
839    struct __gen_vtable
840    {
841      using _Func_ptr = _Result_type (*)(_Visitor&&, _Variants...);
842      using _Array_type =
843	  _Multi_array<_Func_ptr,
844		       variant_size_v<remove_reference_t<_Variants>>...>;
845
846      static constexpr _Array_type
847      _S_apply()
848      {
849	return __gen_vtable_impl<_Array_type, tuple<_Variants...>,
850				 std::index_sequence<>>::_S_apply();
851      }
852
853      static constexpr auto _S_vtable = _S_apply();
854    };
855
856  template<size_t _Np, typename _Tp>
857    struct _Base_dedup : public _Tp { };
858
859  template<typename _Variant, typename __indices>
860    struct _Variant_hash_base;
861
862  template<typename... _Types, size_t... __indices>
863    struct _Variant_hash_base<variant<_Types...>,
864			      std::index_sequence<__indices...>>
865    : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
866
867} // namespace __variant
868} // namespace __detail
869
870  template<typename _Tp, typename... _Types>
871    constexpr bool
872    holds_alternative(const variant<_Types...>& __v) noexcept
873    {
874      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
875		    "T should occur for exactly once in alternatives");
876      return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
877    }
878
879  template<typename _Tp, typename... _Types>
880    constexpr _Tp& get(variant<_Types...>& __v)
881    {
882      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
883		    "T should occur for exactly once in alternatives");
884      static_assert(!is_void_v<_Tp>, "_Tp should not be void");
885      return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
886    }
887
888  template<typename _Tp, typename... _Types>
889    constexpr _Tp&& get(variant<_Types...>&& __v)
890    {
891      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
892		    "T should occur for exactly once in alternatives");
893      static_assert(!is_void_v<_Tp>, "_Tp should not be void");
894      return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
895	std::move(__v));
896    }
897
898  template<typename _Tp, typename... _Types>
899    constexpr const _Tp& get(const variant<_Types...>& __v)
900    {
901      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
902		    "T should occur for exactly once in alternatives");
903      static_assert(!is_void_v<_Tp>, "_Tp should not be void");
904      return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
905    }
906
907  template<typename _Tp, typename... _Types>
908    constexpr const _Tp&& get(const variant<_Types...>&& __v)
909    {
910      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
911		    "T should occur for exactly once in alternatives");
912      static_assert(!is_void_v<_Tp>, "_Tp should not be void");
913      return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
914	std::move(__v));
915    }
916
917  template<size_t _Np, typename... _Types>
918    constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
919    get_if(variant<_Types...>* __ptr) noexcept
920    {
921      using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
922      static_assert(_Np < sizeof...(_Types),
923		    "The index should be in [0, number of alternatives)");
924      static_assert(!is_void_v<_Alternative_type>, "_Tp should not be void");
925      if (__ptr && __ptr->index() == _Np)
926	return &__detail::__variant::__get<_Np>(*__ptr);
927      return nullptr;
928    }
929
930  template<size_t _Np, typename... _Types>
931    constexpr
932    add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
933    get_if(const variant<_Types...>* __ptr) noexcept
934    {
935      using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
936      static_assert(_Np < sizeof...(_Types),
937		    "The index should be in [0, number of alternatives)");
938      static_assert(!is_void_v<_Alternative_type>, "_Tp should not be void");
939      if (__ptr && __ptr->index() == _Np)
940	return &__detail::__variant::__get<_Np>(*__ptr);
941      return nullptr;
942    }
943
944  template<typename _Tp, typename... _Types>
945    constexpr add_pointer_t<_Tp>
946    get_if(variant<_Types...>* __ptr) noexcept
947    {
948      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
949		    "T should occur for exactly once in alternatives");
950      static_assert(!is_void_v<_Tp>, "_Tp should not be void");
951      return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
952	  __ptr);
953    }
954
955  template<typename _Tp, typename... _Types>
956    constexpr add_pointer_t<const _Tp>
957    get_if(const variant<_Types...>* __ptr)
958    noexcept
959    {
960      static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
961		    "T should occur for exactly once in alternatives");
962      static_assert(!is_void_v<_Tp>, "_Tp should not be void");
963      return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
964	  __ptr);
965    }
966
967  struct monostate { };
968
969#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
970  template<typename... _Types> \
971    constexpr bool operator __OP(const variant<_Types...>& __lhs, \
972				 const variant<_Types...>& __rhs) \
973    { \
974      return __lhs._M_##__NAME(__rhs, std::index_sequence_for<_Types...>{}); \
975    } \
976\
977  constexpr bool operator __OP(monostate, monostate) noexcept \
978  { return 0 __OP 0; }
979
980  _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
981  _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
982  _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
983  _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
984  _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
985  _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
986
987#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
988
989  template<typename _Visitor, typename... _Variants>
990    constexpr decltype(auto) visit(_Visitor&&, _Variants&&...);
991
992  template<typename... _Types>
993    inline enable_if_t<(is_move_constructible_v<_Types> && ...)
994			&& (is_swappable_v<_Types> && ...)>
995    swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
996    noexcept(noexcept(__lhs.swap(__rhs)))
997    { __lhs.swap(__rhs); }
998
999  template<typename... _Types>
1000    enable_if_t<!((is_move_constructible_v<_Types> && ...)
1001		   && (is_swappable_v<_Types> && ...))>
1002    swap(variant<_Types...>&, variant<_Types...>&) = delete;
1003
1004  class bad_variant_access : public exception
1005  {
1006  public:
1007    bad_variant_access() noexcept : _M_reason("Unknown reason") { }
1008    const char* what() const noexcept override
1009    { return _M_reason; }
1010
1011  private:
1012    bad_variant_access(const char* __reason) : _M_reason(__reason) { }
1013
1014    const char* _M_reason;
1015
1016    friend void __throw_bad_variant_access(const char* __what);
1017  };
1018
1019  inline void
1020  __throw_bad_variant_access(const char* __what)
1021  { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1022
1023  template<typename... _Types>
1024    class variant
1025    : private __detail::__variant::_Variant_base<_Types...>,
1026      private _Enable_default_constructor<
1027	__detail::__variant::_Traits<_Types...>::_S_default_ctor,
1028	  variant<_Types...>>,
1029      private _Enable_copy_move<
1030	__detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1031	__detail::__variant::_Traits<_Types...>::_S_copy_assign,
1032	__detail::__variant::_Traits<_Types...>::_S_move_ctor,
1033	__detail::__variant::_Traits<_Types...>::_S_move_assign,
1034	variant<_Types...>>
1035    {
1036    private:
1037      static_assert(sizeof...(_Types) > 0,
1038		    "variant must have at least one alternative");
1039      static_assert(!(std::is_reference_v<_Types> || ...),
1040		    "variant must have no reference alternative");
1041      static_assert(!(std::is_void_v<_Types> || ...),
1042		    "variant must have no void alternative");
1043
1044      using _Base = __detail::__variant::_Variant_base<_Types...>;
1045      using _Default_ctor_enabler =
1046	_Enable_default_constructor<
1047	  __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1048	    variant<_Types...>>;
1049
1050      template<typename _Tp>
1051	static constexpr bool
1052	__exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1053
1054      template<typename _Tp>
1055	static constexpr size_t __accepted_index =
1056	  __detail::__variant::__accepted_index<_Tp&&, variant>::value;
1057
1058      template<size_t _Np, bool = _Np < sizeof...(_Types)>
1059	struct __to_type_impl;
1060
1061      template<size_t _Np>
1062	struct __to_type_impl<_Np, true>
1063	{ using type = variant_alternative_t<_Np, variant>; };
1064
1065      template<size_t _Np>
1066	using __to_type = typename __to_type_impl<_Np>::type;
1067
1068      template<typename _Tp>
1069	using __accepted_type = __to_type<__accepted_index<_Tp>>;
1070
1071      template<typename _Tp>
1072	static constexpr size_t __index_of =
1073	  __detail::__variant::__index_of_v<_Tp, _Types...>;
1074
1075      using _Traits = __detail::__variant::_Traits<_Types...>;
1076
1077      template<typename _Tp>
1078	struct __is_in_place_tag : false_type { };
1079      template<typename _Tp>
1080	struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1081      template<size_t _Np>
1082	struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1083
1084      template<typename _Tp>
1085	static constexpr bool __not_in_place_tag
1086	  = !__is_in_place_tag<decay_t<_Tp>>::value;
1087
1088    public:
1089      variant() = default;
1090      variant(const variant& __rhs) = default;
1091      variant(variant&&) = default;
1092      variant& operator=(const variant&) = default;
1093      variant& operator=(variant&&) = default;
1094      ~variant() = default;
1095
1096      template<typename _Tp,
1097	       typename = enable_if_t<!is_same_v<decay_t<_Tp>, variant>>,
1098	       typename = enable_if_t<(sizeof...(_Types)>0)>,
1099	       typename = enable_if_t<__not_in_place_tag<_Tp>>,
1100	       typename = enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1101			  && is_constructible_v<__accepted_type<_Tp&&>, _Tp&&>>>
1102	constexpr
1103	variant(_Tp&& __t)
1104	noexcept(is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>)
1105	: variant(in_place_index<__accepted_index<_Tp&&>>,
1106		  std::forward<_Tp>(__t))
1107	{ __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this)); }
1108
1109      template<typename _Tp, typename... _Args,
1110	       typename = enable_if_t<__exactly_once<_Tp>
1111			  && is_constructible_v<_Tp, _Args&&...>>>
1112	constexpr explicit
1113	variant(in_place_type_t<_Tp>, _Args&&... __args)
1114	: variant(in_place_index<__index_of<_Tp>>,
1115		  std::forward<_Args>(__args)...)
1116	{ __glibcxx_assert(holds_alternative<_Tp>(*this)); }
1117
1118      template<typename _Tp, typename _Up, typename... _Args,
1119	       typename = enable_if_t<__exactly_once<_Tp>
1120			  && is_constructible_v<
1121			    _Tp, initializer_list<_Up>&, _Args&&...>>>
1122	constexpr explicit
1123	variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1124		_Args&&... __args)
1125	: variant(in_place_index<__index_of<_Tp>>, __il,
1126		  std::forward<_Args>(__args)...)
1127	{ __glibcxx_assert(holds_alternative<_Tp>(*this)); }
1128
1129      template<size_t _Np, typename... _Args,
1130	       typename = enable_if_t<
1131		 is_constructible_v<__to_type<_Np>, _Args&&...>>>
1132	constexpr explicit
1133	variant(in_place_index_t<_Np>, _Args&&... __args)
1134	: _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1135	_Default_ctor_enabler(_Enable_default_constructor_tag{})
1136	{ __glibcxx_assert(index() == _Np); }
1137
1138      template<size_t _Np, typename _Up, typename... _Args,
1139	       typename = enable_if_t<is_constructible_v<__to_type<_Np>,
1140				      initializer_list<_Up>&, _Args&&...>>>
1141	constexpr explicit
1142	variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1143		_Args&&... __args)
1144	: _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1145	_Default_ctor_enabler(_Enable_default_constructor_tag{})
1146	{ __glibcxx_assert(index() == _Np); }
1147
1148      template<typename _Tp>
1149	enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1150		    && is_constructible_v<__accepted_type<_Tp&&>, _Tp&&>
1151		    && is_assignable_v<__accepted_type<_Tp&&>&, _Tp&&>
1152		    && !is_same_v<decay_t<_Tp>, variant>, variant&>
1153	operator=(_Tp&& __rhs)
1154	noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp&&>
1155		 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>)
1156	{
1157	  constexpr auto __index = __accepted_index<_Tp&&>;
1158	  if (index() == __index)
1159	    std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1160	  else
1161	    this->emplace<__index>(std::forward<_Tp>(__rhs));
1162	  __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this));
1163	  return *this;
1164	}
1165
1166      template<typename _Tp, typename... _Args>
1167	enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1168		    _Tp&>
1169	emplace(_Args&&... __args)
1170	{
1171	  auto& ret =
1172	    this->emplace<__index_of<_Tp>>(std::forward<_Args>(__args)...);
1173	  __glibcxx_assert(holds_alternative<_Tp>(*this));
1174	  return ret;
1175	}
1176
1177      template<typename _Tp, typename _Up, typename... _Args>
1178	enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1179		    && __exactly_once<_Tp>,
1180		    _Tp&>
1181	emplace(initializer_list<_Up> __il, _Args&&... __args)
1182	{
1183	  auto& ret =
1184	    this->emplace<__index_of<_Tp>>(__il,
1185					   std::forward<_Args>(__args)...);
1186	  __glibcxx_assert(holds_alternative<_Tp>(*this));
1187	  return ret;
1188	}
1189
1190      template<size_t _Np, typename... _Args>
1191	enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1192				       _Args...>,
1193		    variant_alternative_t<_Np, variant>&>
1194	emplace(_Args&&... __args)
1195	{
1196	  static_assert(_Np < sizeof...(_Types),
1197			"The index should be in [0, number of alternatives)");
1198	  this->~variant();
1199	  __try
1200	    {
1201	      ::new (this) variant(in_place_index<_Np>,
1202				   std::forward<_Args>(__args)...);
1203	    }
1204	  __catch (...)
1205	    {
1206	      this->_M_index = variant_npos;
1207	      __throw_exception_again;
1208	    }
1209	  __glibcxx_assert(index() == _Np);
1210	  return std::get<_Np>(*this);
1211	}
1212
1213      template<size_t _Np, typename _Up, typename... _Args>
1214	enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1215				       initializer_list<_Up>&, _Args...>,
1216		    variant_alternative_t<_Np, variant>&>
1217	emplace(initializer_list<_Up> __il, _Args&&... __args)
1218	{
1219	  static_assert(_Np < sizeof...(_Types),
1220			"The index should be in [0, number of alternatives)");
1221	  this->~variant();
1222	  __try
1223	    {
1224	      ::new (this) variant(in_place_index<_Np>, __il,
1225				   std::forward<_Args>(__args)...);
1226	    }
1227	  __catch (...)
1228	    {
1229	      this->_M_index = variant_npos;
1230	      __throw_exception_again;
1231	    }
1232	  __glibcxx_assert(index() == _Np);
1233	  return std::get<_Np>(*this);
1234	}
1235
1236      constexpr bool valueless_by_exception() const noexcept
1237      { return !this->_M_valid(); }
1238
1239      constexpr size_t index() const noexcept
1240      {
1241	if (this->_M_index ==
1242	    typename _Base::__index_type(variant_npos))
1243	  return variant_npos;
1244	return this->_M_index;
1245      }
1246
1247      void
1248      swap(variant& __rhs)
1249      noexcept((__is_nothrow_swappable<_Types>::value && ...)
1250	       && is_nothrow_move_constructible_v<variant>)
1251      {
1252	if (this->index() == __rhs.index())
1253	  {
1254	    if (this->_M_valid())
1255	      {
1256		static constexpr void (*_S_vtable[])(void*, void*) =
1257		  { &__detail::__variant::__erased_swap<_Types&, _Types&>... };
1258		_S_vtable[__rhs._M_index](this->_M_storage(),
1259					  __rhs._M_storage());
1260	      }
1261	  }
1262	else if (!this->_M_valid())
1263	  {
1264	    this->_M_destructive_move(std::move(__rhs));
1265	    __rhs._M_reset();
1266	  }
1267	else if (!__rhs._M_valid())
1268	  {
1269	    __rhs._M_destructive_move(std::move(*this));
1270	    this->_M_reset();
1271	  }
1272	else
1273	  {
1274	    auto __tmp = std::move(__rhs);
1275	    __rhs._M_destructive_move(std::move(*this));
1276	    this->_M_destructive_move(std::move(__tmp));
1277	  }
1278      }
1279
1280    private:
1281#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1282      template<size_t... __indices> \
1283	static constexpr bool \
1284	(*_S_erased_##__NAME[])(const variant&, const variant&) = \
1285	  { &__detail::__variant::__erased_##__NAME< \
1286		const variant&, __indices>... }; \
1287      template<size_t... __indices> \
1288	constexpr bool \
1289	_M_##__NAME(const variant& __rhs, \
1290		    std::index_sequence<__indices...>) const \
1291	{ \
1292	  auto __lhs_index = this->index(); \
1293	  auto __rhs_index = __rhs.index(); \
1294	  if (__lhs_index != __rhs_index || valueless_by_exception()) \
1295	    /* Modulo addition. */ \
1296	    return __lhs_index + 1 __OP __rhs_index + 1; \
1297	  return _S_erased_##__NAME<__indices...>[__lhs_index](*this, __rhs); \
1298	}
1299
1300      _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1301      _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1302      _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1303      _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1304      _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1305      _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1306
1307#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1308
1309#ifdef __clang__
1310    public:
1311      using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1312    private:
1313#endif
1314
1315      template<size_t _Np, typename _Vp>
1316	friend constexpr decltype(auto) __detail::__variant::__get(_Vp&& __v);
1317
1318      template<typename _Vp>
1319	friend void* __detail::__variant::__get_storage(_Vp&& __v);
1320
1321#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1322      template<typename... _Tp> \
1323	friend constexpr bool \
1324	operator __OP(const variant<_Tp...>& __lhs, \
1325		      const variant<_Tp...>& __rhs);
1326
1327      _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1328      _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1329      _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1330      _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1331      _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1332      _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1333
1334#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1335    };
1336
1337  template<size_t _Np, typename... _Types>
1338    constexpr variant_alternative_t<_Np, variant<_Types...>>&
1339    get(variant<_Types...>& __v)
1340    {
1341      static_assert(_Np < sizeof...(_Types),
1342		    "The index should be in [0, number of alternatives)");
1343      if (__v.index() != _Np)
1344	__throw_bad_variant_access("Unexpected index");
1345      return __detail::__variant::__get<_Np>(__v);
1346    }
1347
1348  template<size_t _Np, typename... _Types>
1349    constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1350    get(variant<_Types...>&& __v)
1351    {
1352      static_assert(_Np < sizeof...(_Types),
1353		    "The index should be in [0, number of alternatives)");
1354      if (__v.index() != _Np)
1355	__throw_bad_variant_access("Unexpected index");
1356      return __detail::__variant::__get<_Np>(std::move(__v));
1357    }
1358
1359  template<size_t _Np, typename... _Types>
1360    constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1361    get(const variant<_Types...>& __v)
1362    {
1363      static_assert(_Np < sizeof...(_Types),
1364		    "The index should be in [0, number of alternatives)");
1365      if (__v.index() != _Np)
1366	__throw_bad_variant_access("Unexpected index");
1367      return __detail::__variant::__get<_Np>(__v);
1368    }
1369
1370  template<size_t _Np, typename... _Types>
1371    constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1372    get(const variant<_Types...>&& __v)
1373    {
1374      static_assert(_Np < sizeof...(_Types),
1375		    "The index should be in [0, number of alternatives)");
1376      if (__v.index() != _Np)
1377	__throw_bad_variant_access("Unexpected index");
1378      return __detail::__variant::__get<_Np>(std::move(__v));
1379    }
1380
1381  template<typename _Visitor, typename... _Variants>
1382    constexpr decltype(auto)
1383    visit(_Visitor&& __visitor, _Variants&&... __variants)
1384    {
1385      if ((__variants.valueless_by_exception() || ...))
1386	__throw_bad_variant_access("Unexpected index");
1387
1388      using _Result_type =
1389	decltype(std::forward<_Visitor>(__visitor)(
1390	    std::get<0>(std::forward<_Variants>(__variants))...));
1391
1392      constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1393	_Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1394
1395      auto __func_ptr = __vtable._M_access(__variants.index()...);
1396      return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1397			   std::forward<_Variants>(__variants)...);
1398    }
1399
1400  template<bool, typename... _Types>
1401    struct __variant_hash_call_base_impl
1402    {
1403      size_t
1404      operator()(const variant<_Types...>& __t) const
1405      noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1406      {
1407	if (!__t.valueless_by_exception())
1408	  {
1409	    namespace __edv = __detail::__variant;
1410	    static constexpr size_t (*_S_vtable[])(void*) =
1411	      { &__edv::__erased_hash<const _Types&>... };
1412	    return hash<size_t>{}(__t.index())
1413	      + _S_vtable[__t.index()](__edv::__get_storage(__t));
1414	  }
1415	return hash<size_t>{}(__t.index());
1416      }
1417    };
1418
1419  template<typename... _Types>
1420    struct __variant_hash_call_base_impl<false, _Types...> {};
1421
1422  template<typename... _Types>
1423    using __variant_hash_call_base =
1424    __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1425				   __enable_hash_call &&...), _Types...>;
1426
1427  template<typename... _Types>
1428    struct hash<variant<_Types...>>
1429    : private __detail::__variant::_Variant_hash_base<
1430	variant<_Types...>, std::index_sequence_for<_Types...>>,
1431      public __variant_hash_call_base<_Types...>
1432    {
1433      using result_type [[__deprecated__]] = size_t;
1434      using argument_type [[__deprecated__]] = variant<_Types...>;
1435    };
1436
1437  template<>
1438    struct hash<monostate>
1439    {
1440      using result_type [[__deprecated__]] = size_t;
1441      using argument_type [[__deprecated__]] = monostate;
1442
1443      size_t
1444      operator()(const monostate& __t) const noexcept
1445      {
1446	constexpr size_t __magic_monostate_hash = -7777;
1447	return __magic_monostate_hash;
1448      }
1449    };
1450
1451  template<typename... _Types>
1452    struct __is_fast_hash<hash<variant<_Types...>>>
1453    : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1454    { };
1455
1456_GLIBCXX_END_NAMESPACE_VERSION
1457} // namespace std
1458
1459#endif // C++17
1460
1461#endif // _GLIBCXX_VARIANT
1462