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