Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------------ variant -------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_VARIANT |
| 11 | #define _LIBCPP_VARIANT |
| 12 | |
| 13 | /* |
| 14 | variant synopsis |
| 15 | |
| 16 | namespace std { |
| 17 | |
| 18 | // 20.7.2, class template variant |
| 19 | template <class... Types> |
| 20 | class variant { |
| 21 | public: |
| 22 | |
| 23 | // 20.7.2.1, constructors |
| 24 | constexpr variant() noexcept(see below); |
Louis Dionne | cee5901 | 2019-01-10 20:06:11 +0000 | [diff] [blame] | 25 | variant(const variant&); // constexpr in C++20 |
| 26 | variant(variant&&) noexcept(see below); // constexpr in C++20 |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 27 | |
| 28 | template <class T> constexpr variant(T&&) noexcept(see below); |
| 29 | |
| 30 | template <class T, class... Args> |
| 31 | constexpr explicit variant(in_place_type_t<T>, Args&&...); |
| 32 | |
| 33 | template <class T, class U, class... Args> |
| 34 | constexpr explicit variant( |
| 35 | in_place_type_t<T>, initializer_list<U>, Args&&...); |
| 36 | |
| 37 | template <size_t I, class... Args> |
| 38 | constexpr explicit variant(in_place_index_t<I>, Args&&...); |
| 39 | |
| 40 | template <size_t I, class U, class... Args> |
| 41 | constexpr explicit variant( |
| 42 | in_place_index_t<I>, initializer_list<U>, Args&&...); |
| 43 | |
| 44 | // 20.7.2.2, destructor |
| 45 | ~variant(); |
| 46 | |
| 47 | // 20.7.2.3, assignment |
Louis Dionne | cee5901 | 2019-01-10 20:06:11 +0000 | [diff] [blame] | 48 | variant& operator=(const variant&); // constexpr in C++20 |
| 49 | variant& operator=(variant&&) noexcept(see below); // constexpr in C++20 |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 50 | |
| 51 | template <class T> variant& operator=(T&&) noexcept(see below); |
| 52 | |
| 53 | // 20.7.2.4, modifiers |
| 54 | template <class T, class... Args> |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 55 | T& emplace(Args&&...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 56 | |
| 57 | template <class T, class U, class... Args> |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 58 | T& emplace(initializer_list<U>, Args&&...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 59 | |
| 60 | template <size_t I, class... Args> |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 61 | variant_alternative_t<I, variant>& emplace(Args&&...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 62 | |
| 63 | template <size_t I, class U, class... Args> |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 64 | variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 65 | |
| 66 | // 20.7.2.5, value status |
| 67 | constexpr bool valueless_by_exception() const noexcept; |
| 68 | constexpr size_t index() const noexcept; |
| 69 | |
| 70 | // 20.7.2.6, swap |
| 71 | void swap(variant&) noexcept(see below); |
| 72 | }; |
| 73 | |
| 74 | // 20.7.3, variant helper classes |
| 75 | template <class T> struct variant_size; // undefined |
| 76 | |
| 77 | template <class T> |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 78 | inline constexpr size_t variant_size_v = variant_size<T>::value; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 79 | |
| 80 | template <class T> struct variant_size<const T>; |
| 81 | template <class T> struct variant_size<volatile T>; |
| 82 | template <class T> struct variant_size<const volatile T>; |
| 83 | |
| 84 | template <class... Types> |
| 85 | struct variant_size<variant<Types...>>; |
| 86 | |
| 87 | template <size_t I, class T> struct variant_alternative; // undefined |
| 88 | |
| 89 | template <size_t I, class T> |
| 90 | using variant_alternative_t = typename variant_alternative<I, T>::type; |
| 91 | |
| 92 | template <size_t I, class T> struct variant_alternative<I, const T>; |
| 93 | template <size_t I, class T> struct variant_alternative<I, volatile T>; |
| 94 | template <size_t I, class T> struct variant_alternative<I, const volatile T>; |
| 95 | |
| 96 | template <size_t I, class... Types> |
| 97 | struct variant_alternative<I, variant<Types...>>; |
| 98 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 99 | inline constexpr size_t variant_npos = -1; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 100 | |
| 101 | // 20.7.4, value access |
| 102 | template <class T, class... Types> |
| 103 | constexpr bool holds_alternative(const variant<Types...>&) noexcept; |
| 104 | |
| 105 | template <size_t I, class... Types> |
| 106 | constexpr variant_alternative_t<I, variant<Types...>>& |
| 107 | get(variant<Types...>&); |
| 108 | |
| 109 | template <size_t I, class... Types> |
| 110 | constexpr variant_alternative_t<I, variant<Types...>>&& |
| 111 | get(variant<Types...>&&); |
| 112 | |
| 113 | template <size_t I, class... Types> |
| 114 | constexpr variant_alternative_t<I, variant<Types...>> const& |
| 115 | get(const variant<Types...>&); |
| 116 | |
| 117 | template <size_t I, class... Types> |
| 118 | constexpr variant_alternative_t<I, variant<Types...>> const&& |
| 119 | get(const variant<Types...>&&); |
| 120 | |
| 121 | template <class T, class... Types> |
| 122 | constexpr T& get(variant<Types...>&); |
| 123 | |
| 124 | template <class T, class... Types> |
| 125 | constexpr T&& get(variant<Types...>&&); |
| 126 | |
| 127 | template <class T, class... Types> |
| 128 | constexpr const T& get(const variant<Types...>&); |
| 129 | |
| 130 | template <class T, class... Types> |
| 131 | constexpr const T&& get(const variant<Types...>&&); |
| 132 | |
| 133 | template <size_t I, class... Types> |
| 134 | constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>> |
| 135 | get_if(variant<Types...>*) noexcept; |
| 136 | |
| 137 | template <size_t I, class... Types> |
| 138 | constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>> |
| 139 | get_if(const variant<Types...>*) noexcept; |
| 140 | |
| 141 | template <class T, class... Types> |
| 142 | constexpr add_pointer_t<T> |
| 143 | get_if(variant<Types...>*) noexcept; |
| 144 | |
| 145 | template <class T, class... Types> |
| 146 | constexpr add_pointer_t<const T> |
| 147 | get_if(const variant<Types...>*) noexcept; |
| 148 | |
| 149 | // 20.7.5, relational operators |
| 150 | template <class... Types> |
| 151 | constexpr bool operator==(const variant<Types...>&, const variant<Types...>&); |
| 152 | |
| 153 | template <class... Types> |
| 154 | constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&); |
| 155 | |
| 156 | template <class... Types> |
| 157 | constexpr bool operator<(const variant<Types...>&, const variant<Types...>&); |
| 158 | |
| 159 | template <class... Types> |
| 160 | constexpr bool operator>(const variant<Types...>&, const variant<Types...>&); |
| 161 | |
| 162 | template <class... Types> |
| 163 | constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&); |
| 164 | |
| 165 | template <class... Types> |
| 166 | constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&); |
| 167 | |
| 168 | // 20.7.6, visitation |
| 169 | template <class Visitor, class... Variants> |
| 170 | constexpr see below visit(Visitor&&, Variants&&...); |
| 171 | |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 172 | template <class R, class Visitor, class... Variants> |
| 173 | constexpr R visit(Visitor&&, Variants&&...); // since C++20 |
| 174 | |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 175 | // 20.7.7, class monostate |
| 176 | struct monostate; |
| 177 | |
| 178 | // 20.7.8, monostate relational operators |
| 179 | constexpr bool operator<(monostate, monostate) noexcept; |
| 180 | constexpr bool operator>(monostate, monostate) noexcept; |
| 181 | constexpr bool operator<=(monostate, monostate) noexcept; |
| 182 | constexpr bool operator>=(monostate, monostate) noexcept; |
| 183 | constexpr bool operator==(monostate, monostate) noexcept; |
| 184 | constexpr bool operator!=(monostate, monostate) noexcept; |
| 185 | |
| 186 | // 20.7.9, specialized algorithms |
| 187 | template <class... Types> |
| 188 | void swap(variant<Types...>&, variant<Types...>&) noexcept(see below); |
| 189 | |
| 190 | // 20.7.10, class bad_variant_access |
| 191 | class bad_variant_access; |
| 192 | |
| 193 | // 20.7.11, hash support |
| 194 | template <class T> struct hash; |
| 195 | template <class... Types> struct hash<variant<Types...>>; |
| 196 | template <> struct hash<monostate>; |
| 197 | |
| 198 | } // namespace std |
| 199 | |
| 200 | */ |
| 201 | |
Louis Dionne | 73912b2 | 2020-11-04 15:01:25 -0500 | [diff] [blame] | 202 | #include <__availability> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame^] | 203 | #include <__config> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 204 | #include <__tuple> |
| 205 | #include <array> |
Arthur O'Dwyer | 7deec12 | 2021-03-24 18:19:12 -0400 | [diff] [blame] | 206 | #include <compare> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 207 | #include <exception> |
| 208 | #include <functional> |
| 209 | #include <initializer_list> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame^] | 210 | #include <limits> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 211 | #include <new> |
| 212 | #include <tuple> |
| 213 | #include <type_traits> |
| 214 | #include <utility> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 215 | #include <version> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 216 | |
| 217 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 218 | #pragma GCC system_header |
| 219 | #endif |
| 220 | |
Eric Fiselier | 8625d33 | 2017-11-19 04:57:22 +0000 | [diff] [blame] | 221 | _LIBCPP_PUSH_MACROS |
| 222 | #include <__undef_macros> |
| 223 | |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 224 | namespace std { // explicitly not using versioning namespace |
| 225 | |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 226 | class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 227 | public: |
Saleem Abdulrasool | 5ee8338 | 2016-12-31 17:34:26 +0000 | [diff] [blame] | 228 | virtual const char* what() const _NOEXCEPT; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | } // namespace std |
| 232 | |
| 233 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 234 | |
Louis Dionne | 7202cd6 | 2020-07-22 15:24:16 -0400 | [diff] [blame] | 235 | // TODO: GCC 5 lies about its support for C++17 (it says it supports it but it |
| 236 | // really doesn't). That breaks variant, which uses some C++17 features. |
| 237 | // Remove this once we drop support for GCC 5. |
Louis Dionne | 129391d | 2020-07-22 15:59:09 -0400 | [diff] [blame] | 238 | #if _LIBCPP_STD_VER > 14 && !(defined(_LIBCPP_COMPILER_GCC) && _GNUC_VER_NEW < 6000) |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 239 | |
Eric Fiselier | 10642ea | 2016-12-03 01:58:07 +0000 | [diff] [blame] | 240 | _LIBCPP_NORETURN |
| 241 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 242 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 10642ea | 2016-12-03 01:58:07 +0000 | [diff] [blame] | 243 | void __throw_bad_variant_access() { |
| 244 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 245 | throw bad_variant_access(); |
| 246 | #else |
| 247 | _VSTD::abort(); |
| 248 | #endif |
| 249 | } |
| 250 | |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 251 | template <class... _Types> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 252 | class _LIBCPP_TEMPLATE_VIS variant; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 253 | |
| 254 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 255 | struct _LIBCPP_TEMPLATE_VIS variant_size; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 256 | |
| 257 | template <class _Tp> |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 258 | _LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 259 | |
| 260 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 261 | struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {}; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 262 | |
| 263 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 264 | struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {}; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 265 | |
| 266 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 267 | struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 268 | : variant_size<_Tp> {}; |
| 269 | |
| 270 | template <class... _Types> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 271 | struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 272 | : integral_constant<size_t, sizeof...(_Types)> {}; |
| 273 | |
| 274 | template <size_t _Ip, class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 275 | struct _LIBCPP_TEMPLATE_VIS variant_alternative; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 276 | |
| 277 | template <size_t _Ip, class _Tp> |
| 278 | using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type; |
| 279 | |
| 280 | template <size_t _Ip, class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 281 | struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 282 | : add_const<variant_alternative_t<_Ip, _Tp>> {}; |
| 283 | |
| 284 | template <size_t _Ip, class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 285 | struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 286 | : add_volatile<variant_alternative_t<_Ip, _Tp>> {}; |
| 287 | |
| 288 | template <size_t _Ip, class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 289 | struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 290 | : add_cv<variant_alternative_t<_Ip, _Tp>> {}; |
| 291 | |
| 292 | template <size_t _Ip, class... _Types> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 293 | struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> { |
Marshall Clow | 185577f | 2017-06-12 16:13:17 +0000 | [diff] [blame] | 294 | static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>"); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 295 | using type = __type_pack_element<_Ip, _Types...>; |
| 296 | }; |
| 297 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 298 | _LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1); |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 299 | |
| 300 | constexpr int __choose_index_type(unsigned int __num_elem) { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 301 | if (__num_elem < numeric_limits<unsigned char>::max()) |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 302 | return 0; |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 303 | if (__num_elem < numeric_limits<unsigned short>::max()) |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 304 | return 1; |
| 305 | return 2; |
| 306 | } |
| 307 | |
| 308 | template <size_t _NumAlts> |
| 309 | using __variant_index_t = |
| 310 | #ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION |
| 311 | unsigned int; |
| 312 | #else |
| 313 | std::tuple_element_t< |
| 314 | __choose_index_type(_NumAlts), |
| 315 | std::tuple<unsigned char, unsigned short, unsigned int> |
| 316 | >; |
| 317 | #endif |
| 318 | |
| 319 | template <class _IndexType> |
| 320 | constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 321 | |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 322 | template <class... _Types> |
| 323 | class _LIBCPP_TEMPLATE_VIS variant; |
| 324 | |
| 325 | template <class... _Types> |
| 326 | _LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>& |
| 327 | __as_variant(variant<_Types...>& __vs) noexcept { |
| 328 | return __vs; |
| 329 | } |
| 330 | |
| 331 | template <class... _Types> |
| 332 | _LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>& |
| 333 | __as_variant(const variant<_Types...>& __vs) noexcept { |
| 334 | return __vs; |
| 335 | } |
| 336 | |
| 337 | template <class... _Types> |
| 338 | _LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&& |
| 339 | __as_variant(variant<_Types...>&& __vs) noexcept { |
| 340 | return _VSTD::move(__vs); |
| 341 | } |
| 342 | |
| 343 | template <class... _Types> |
| 344 | _LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&& |
| 345 | __as_variant(const variant<_Types...>&& __vs) noexcept { |
| 346 | return _VSTD::move(__vs); |
| 347 | } |
| 348 | |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 349 | namespace __find_detail { |
| 350 | |
| 351 | template <class _Tp, class... _Types> |
| 352 | inline _LIBCPP_INLINE_VISIBILITY |
| 353 | constexpr size_t __find_index() { |
| 354 | constexpr bool __matches[] = {is_same_v<_Tp, _Types>...}; |
| 355 | size_t __result = __not_found; |
| 356 | for (size_t __i = 0; __i < sizeof...(_Types); ++__i) { |
| 357 | if (__matches[__i]) { |
| 358 | if (__result != __not_found) { |
| 359 | return __ambiguous; |
| 360 | } |
| 361 | __result = __i; |
| 362 | } |
| 363 | } |
| 364 | return __result; |
| 365 | } |
| 366 | |
| 367 | template <size_t _Index> |
| 368 | struct __find_unambiguous_index_sfinae_impl |
| 369 | : integral_constant<size_t, _Index> {}; |
| 370 | |
| 371 | template <> |
| 372 | struct __find_unambiguous_index_sfinae_impl<__not_found> {}; |
| 373 | |
| 374 | template <> |
| 375 | struct __find_unambiguous_index_sfinae_impl<__ambiguous> {}; |
| 376 | |
| 377 | template <class _Tp, class... _Types> |
| 378 | struct __find_unambiguous_index_sfinae |
| 379 | : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {}; |
| 380 | |
| 381 | } // namespace __find_detail |
| 382 | |
| 383 | namespace __variant_detail { |
| 384 | |
| 385 | struct __valueless_t {}; |
| 386 | |
| 387 | enum class _Trait { _TriviallyAvailable, _Available, _Unavailable }; |
| 388 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 389 | template <typename _Tp, |
| 390 | template <typename> class _IsTriviallyAvailable, |
| 391 | template <typename> class _IsAvailable> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 392 | constexpr _Trait __trait = |
| 393 | _IsTriviallyAvailable<_Tp>::value |
| 394 | ? _Trait::_TriviallyAvailable |
| 395 | : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable; |
| 396 | |
| 397 | inline _LIBCPP_INLINE_VISIBILITY |
| 398 | constexpr _Trait __common_trait(initializer_list<_Trait> __traits) { |
| 399 | _Trait __result = _Trait::_TriviallyAvailable; |
| 400 | for (_Trait __t : __traits) { |
| 401 | if (static_cast<int>(__t) > static_cast<int>(__result)) { |
| 402 | __result = __t; |
| 403 | } |
| 404 | } |
| 405 | return __result; |
| 406 | } |
| 407 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 408 | template <typename... _Types> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 409 | struct __traits { |
| 410 | static constexpr _Trait __copy_constructible_trait = |
| 411 | __common_trait({__trait<_Types, |
| 412 | is_trivially_copy_constructible, |
| 413 | is_copy_constructible>...}); |
| 414 | |
| 415 | static constexpr _Trait __move_constructible_trait = |
| 416 | __common_trait({__trait<_Types, |
| 417 | is_trivially_move_constructible, |
| 418 | is_move_constructible>...}); |
| 419 | |
| 420 | static constexpr _Trait __copy_assignable_trait = __common_trait( |
| 421 | {__copy_constructible_trait, |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 422 | __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...}); |
| 423 | |
| 424 | static constexpr _Trait __move_assignable_trait = __common_trait( |
| 425 | {__move_constructible_trait, |
| 426 | __trait<_Types, is_trivially_move_assignable, is_move_assignable>...}); |
| 427 | |
| 428 | static constexpr _Trait __destructible_trait = __common_trait( |
| 429 | {__trait<_Types, is_trivially_destructible, is_destructible>...}); |
| 430 | }; |
| 431 | |
| 432 | namespace __access { |
| 433 | |
| 434 | struct __union { |
| 435 | template <class _Vp> |
| 436 | inline _LIBCPP_INLINE_VISIBILITY |
| 437 | static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) { |
| 438 | return _VSTD::forward<_Vp>(__v).__head; |
| 439 | } |
| 440 | |
| 441 | template <class _Vp, size_t _Ip> |
| 442 | inline _LIBCPP_INLINE_VISIBILITY |
| 443 | static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) { |
| 444 | return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>); |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | struct __base { |
| 449 | template <size_t _Ip, class _Vp> |
| 450 | inline _LIBCPP_INLINE_VISIBILITY |
| 451 | static constexpr auto&& __get_alt(_Vp&& __v) { |
| 452 | return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data, |
| 453 | in_place_index<_Ip>); |
| 454 | } |
| 455 | }; |
| 456 | |
| 457 | struct __variant { |
| 458 | template <size_t _Ip, class _Vp> |
| 459 | inline _LIBCPP_INLINE_VISIBILITY |
| 460 | static constexpr auto&& __get_alt(_Vp&& __v) { |
| 461 | return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl); |
| 462 | } |
| 463 | }; |
| 464 | |
| 465 | } // namespace __access |
| 466 | |
| 467 | namespace __visitation { |
| 468 | |
| 469 | struct __base { |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 470 | template <class _Visitor, class... _Vs> |
| 471 | inline _LIBCPP_INLINE_VISIBILITY |
| 472 | static constexpr decltype(auto) |
| 473 | __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { |
| 474 | constexpr auto __fdiagonal = |
| 475 | __make_fdiagonal<_Visitor&&, |
| 476 | decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>(); |
| 477 | return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor), |
| 478 | _VSTD::forward<_Vs>(__vs).__as_base()...); |
| 479 | } |
| 480 | |
| 481 | template <class _Visitor, class... _Vs> |
| 482 | inline _LIBCPP_INLINE_VISIBILITY |
| 483 | static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, |
| 484 | _Vs&&... __vs) { |
| 485 | constexpr auto __fmatrix = |
| 486 | __make_fmatrix<_Visitor&&, |
| 487 | decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>(); |
| 488 | return __at(__fmatrix, __vs.index()...)( |
| 489 | _VSTD::forward<_Visitor>(__visitor), |
| 490 | _VSTD::forward<_Vs>(__vs).__as_base()...); |
| 491 | } |
| 492 | |
| 493 | private: |
| 494 | template <class _Tp> |
| 495 | inline _LIBCPP_INLINE_VISIBILITY |
| 496 | static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; } |
| 497 | |
| 498 | template <class _Tp, size_t _Np, typename... _Indices> |
| 499 | inline _LIBCPP_INLINE_VISIBILITY |
| 500 | static constexpr auto&& __at(const array<_Tp, _Np>& __elems, |
| 501 | size_t __index, _Indices... __indices) { |
| 502 | return __at(__elems[__index], __indices...); |
| 503 | } |
| 504 | |
| 505 | template <class _Fp, class... _Fs> |
| 506 | static constexpr void __std_visit_visitor_return_type_check() { |
| 507 | static_assert( |
| 508 | __all<is_same_v<_Fp, _Fs>...>::value, |
| 509 | "`std::visit` requires the visitor to have a single return type."); |
| 510 | } |
| 511 | |
| 512 | template <class... _Fs> |
| 513 | inline _LIBCPP_INLINE_VISIBILITY |
| 514 | static constexpr auto __make_farray(_Fs&&... __fs) { |
| 515 | __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>(); |
| 516 | using __result = array<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>; |
| 517 | return __result{{_VSTD::forward<_Fs>(__fs)...}}; |
| 518 | } |
| 519 | |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 520 | template <size_t... _Is> |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 521 | struct __dispatcher { |
| 522 | template <class _Fp, class... _Vs> |
Michael Park | 32334da | 2020-08-30 12:42:35 -0400 | [diff] [blame] | 523 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 524 | static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) { |
Arthur O'Dwyer | f0bff15 | 2020-11-24 09:59:26 -0500 | [diff] [blame] | 525 | return _VSTD::__invoke_constexpr( |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 526 | static_cast<_Fp>(__f), |
| 527 | __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...); |
Michael Park | 32334da | 2020-08-30 12:42:35 -0400 | [diff] [blame] | 528 | } |
| 529 | }; |
| 530 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 531 | template <class _Fp, class... _Vs, size_t... _Is> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 532 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 533 | static constexpr auto __make_dispatch(index_sequence<_Is...>) { |
| 534 | return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 537 | template <size_t _Ip, class _Fp, class... _Vs> |
| 538 | inline _LIBCPP_INLINE_VISIBILITY |
| 539 | static constexpr auto __make_fdiagonal_impl() { |
| 540 | return __make_dispatch<_Fp, _Vs...>( |
| 541 | index_sequence<(__identity<_Vs>{}, _Ip)...>{}); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 542 | } |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 543 | |
| 544 | template <class _Fp, class... _Vs, size_t... _Is> |
| 545 | inline _LIBCPP_INLINE_VISIBILITY |
| 546 | static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) { |
| 547 | return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...); |
| 548 | } |
| 549 | |
| 550 | template <class _Fp, class _Vp, class... _Vs> |
| 551 | inline _LIBCPP_INLINE_VISIBILITY |
| 552 | static constexpr auto __make_fdiagonal() { |
| 553 | constexpr size_t _Np = __uncvref_t<_Vp>::__size(); |
| 554 | static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value); |
| 555 | return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{}); |
| 556 | } |
| 557 | |
| 558 | template <class _Fp, class... _Vs, size_t... _Is> |
| 559 | inline _LIBCPP_INLINE_VISIBILITY |
| 560 | static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) { |
| 561 | return __make_dispatch<_Fp, _Vs...>(__is); |
| 562 | } |
| 563 | |
| 564 | template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls> |
| 565 | inline _LIBCPP_INLINE_VISIBILITY |
| 566 | static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>, |
| 567 | index_sequence<_Js...>, |
| 568 | _Ls... __ls) { |
| 569 | return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>( |
| 570 | index_sequence<_Is..., _Js>{}, __ls...)...); |
| 571 | } |
| 572 | |
| 573 | template <class _Fp, class... _Vs> |
| 574 | inline _LIBCPP_INLINE_VISIBILITY |
| 575 | static constexpr auto __make_fmatrix() { |
| 576 | return __make_fmatrix_impl<_Fp, _Vs...>( |
| 577 | index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 578 | } |
| 579 | }; |
| 580 | |
| 581 | struct __variant { |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 582 | template <class _Visitor, class... _Vs> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 583 | inline _LIBCPP_INLINE_VISIBILITY |
| 584 | static constexpr decltype(auto) |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 585 | __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 586 | return __base::__visit_alt_at(__index, |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 587 | _VSTD::forward<_Visitor>(__visitor), |
| 588 | _VSTD::forward<_Vs>(__vs).__impl...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 591 | template <class _Visitor, class... _Vs> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 592 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 593 | static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, |
| 594 | _Vs&&... __vs) { |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 595 | return __base::__visit_alt( |
| 596 | _VSTD::forward<_Visitor>(__visitor), |
| 597 | _VSTD::__as_variant(_VSTD::forward<_Vs>(__vs)).__impl...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 600 | template <class _Visitor, class... _Vs> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 601 | inline _LIBCPP_INLINE_VISIBILITY |
| 602 | static constexpr decltype(auto) |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 603 | __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { |
| 604 | return __visit_alt_at( |
| 605 | __index, |
| 606 | __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)), |
| 607 | _VSTD::forward<_Vs>(__vs)...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 610 | template <class _Visitor, class... _Vs> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 611 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 612 | static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, |
| 613 | _Vs&&... __vs) { |
| 614 | return __visit_alt( |
| 615 | __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)), |
| 616 | _VSTD::forward<_Vs>(__vs)...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 617 | } |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 618 | |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 619 | #if _LIBCPP_STD_VER > 17 |
| 620 | template <class _Rp, class _Visitor, class... _Vs> |
| 621 | inline _LIBCPP_INLINE_VISIBILITY |
| 622 | static constexpr _Rp __visit_value(_Visitor&& __visitor, |
| 623 | _Vs&&... __vs) { |
| 624 | return __visit_alt( |
| 625 | __make_value_visitor<_Rp>(_VSTD::forward<_Visitor>(__visitor)), |
| 626 | _VSTD::forward<_Vs>(__vs)...); |
| 627 | } |
| 628 | #endif |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 629 | |
| 630 | private: |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 631 | template <class _Visitor, class... _Values> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 632 | static constexpr void __std_visit_exhaustive_visitor_check() { |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 633 | static_assert(is_invocable_v<_Visitor, _Values...>, |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 634 | "`std::visit` requires the visitor to be exhaustive."); |
| 635 | } |
| 636 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 637 | template <class _Visitor> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 638 | struct __value_visitor { |
| 639 | template <class... _Alts> |
| 640 | inline _LIBCPP_INLINE_VISIBILITY |
| 641 | constexpr decltype(auto) operator()(_Alts&&... __alts) const { |
| 642 | __std_visit_exhaustive_visitor_check< |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 643 | _Visitor, |
| 644 | decltype((_VSTD::forward<_Alts>(__alts).__value))...>(); |
Arthur O'Dwyer | f0bff15 | 2020-11-24 09:59:26 -0500 | [diff] [blame] | 645 | return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor), |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 646 | _VSTD::forward<_Alts>(__alts).__value...); |
| 647 | } |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 648 | _Visitor&& __visitor; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 649 | }; |
| 650 | |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 651 | #if _LIBCPP_STD_VER > 17 |
| 652 | template <class _Rp, class _Visitor> |
| 653 | struct __value_visitor_return_type { |
| 654 | template <class... _Alts> |
| 655 | inline _LIBCPP_INLINE_VISIBILITY |
| 656 | constexpr _Rp operator()(_Alts&&... __alts) const { |
| 657 | __std_visit_exhaustive_visitor_check< |
| 658 | _Visitor, |
| 659 | decltype((_VSTD::forward<_Alts>(__alts).__value))...>(); |
| 660 | if constexpr (is_void_v<_Rp>) { |
| 661 | _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor), |
| 662 | _VSTD::forward<_Alts>(__alts).__value...); |
| 663 | } |
| 664 | else { |
| 665 | return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor), |
| 666 | _VSTD::forward<_Alts>(__alts).__value...); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | _Visitor&& __visitor; |
| 671 | }; |
| 672 | #endif |
| 673 | |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 674 | template <class _Visitor> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 675 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 676 | static constexpr auto __make_value_visitor(_Visitor&& __visitor) { |
| 677 | return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)}; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 678 | } |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 679 | |
| 680 | #if _LIBCPP_STD_VER > 17 |
| 681 | template <class _Rp, class _Visitor> |
| 682 | inline _LIBCPP_INLINE_VISIBILITY |
| 683 | static constexpr auto __make_value_visitor(_Visitor&& __visitor) { |
| 684 | return __value_visitor_return_type<_Rp, _Visitor>{_VSTD::forward<_Visitor>(__visitor)}; |
| 685 | } |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 686 | #endif |
Nico Weber | 8aa3661 | 2021-01-25 15:10:41 -0500 | [diff] [blame] | 687 | }; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 688 | |
| 689 | } // namespace __visitation |
| 690 | |
| 691 | template <size_t _Index, class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 692 | struct _LIBCPP_TEMPLATE_VIS __alt { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 693 | using __value_type = _Tp; |
| 694 | |
| 695 | template <class... _Args> |
| 696 | inline _LIBCPP_INLINE_VISIBILITY |
| 697 | explicit constexpr __alt(in_place_t, _Args&&... __args) |
| 698 | : __value(_VSTD::forward<_Args>(__args)...) {} |
| 699 | |
| 700 | __value_type __value; |
| 701 | }; |
| 702 | |
| 703 | template <_Trait _DestructibleTrait, size_t _Index, class... _Types> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 704 | union _LIBCPP_TEMPLATE_VIS __union; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 705 | |
| 706 | template <_Trait _DestructibleTrait, size_t _Index> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 707 | union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {}; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 708 | |
| 709 | #define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \ |
| 710 | template <size_t _Index, class _Tp, class... _Types> \ |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 711 | union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 712 | _Index, \ |
| 713 | _Tp, \ |
| 714 | _Types...> { \ |
| 715 | public: \ |
| 716 | inline _LIBCPP_INLINE_VISIBILITY \ |
| 717 | explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \ |
| 718 | \ |
| 719 | template <class... _Args> \ |
| 720 | inline _LIBCPP_INLINE_VISIBILITY \ |
| 721 | explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \ |
| 722 | : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \ |
| 723 | \ |
| 724 | template <size_t _Ip, class... _Args> \ |
| 725 | inline _LIBCPP_INLINE_VISIBILITY \ |
| 726 | explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \ |
| 727 | : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \ |
| 728 | \ |
| 729 | __union(const __union&) = default; \ |
| 730 | __union(__union&&) = default; \ |
| 731 | \ |
| 732 | destructor \ |
| 733 | \ |
| 734 | __union& operator=(const __union&) = default; \ |
| 735 | __union& operator=(__union&&) = default; \ |
| 736 | \ |
| 737 | private: \ |
| 738 | char __dummy; \ |
| 739 | __alt<_Index, _Tp> __head; \ |
| 740 | __union<destructible_trait, _Index + 1, _Types...> __tail; \ |
| 741 | \ |
| 742 | friend struct __access::__union; \ |
| 743 | } |
| 744 | |
| 745 | _LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;); |
| 746 | _LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {}); |
| 747 | _LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;); |
| 748 | |
| 749 | #undef _LIBCPP_VARIANT_UNION |
| 750 | |
| 751 | template <_Trait _DestructibleTrait, class... _Types> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 752 | class _LIBCPP_TEMPLATE_VIS __base { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 753 | public: |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 754 | using __index_t = __variant_index_t<sizeof...(_Types)>; |
| 755 | |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 756 | inline _LIBCPP_INLINE_VISIBILITY |
| 757 | explicit constexpr __base(__valueless_t tag) noexcept |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 758 | : __data(tag), __index(__variant_npos<__index_t>) {} |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 759 | |
| 760 | template <size_t _Ip, class... _Args> |
| 761 | inline _LIBCPP_INLINE_VISIBILITY |
| 762 | explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args) |
Eric Fiselier | 2adf087 | 2016-12-02 23:17:33 +0000 | [diff] [blame] | 763 | : |
| 764 | __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...), |
| 765 | __index(_Ip) {} |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 766 | |
| 767 | inline _LIBCPP_INLINE_VISIBILITY |
| 768 | constexpr bool valueless_by_exception() const noexcept { |
| 769 | return index() == variant_npos; |
| 770 | } |
| 771 | |
| 772 | inline _LIBCPP_INLINE_VISIBILITY |
| 773 | constexpr size_t index() const noexcept { |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 774 | return __index == __variant_npos<__index_t> ? variant_npos : __index; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | protected: |
| 778 | inline _LIBCPP_INLINE_VISIBILITY |
| 779 | constexpr auto&& __as_base() & { return *this; } |
| 780 | |
| 781 | inline _LIBCPP_INLINE_VISIBILITY |
| 782 | constexpr auto&& __as_base() && { return _VSTD::move(*this); } |
| 783 | |
| 784 | inline _LIBCPP_INLINE_VISIBILITY |
| 785 | constexpr auto&& __as_base() const & { return *this; } |
| 786 | |
| 787 | inline _LIBCPP_INLINE_VISIBILITY |
| 788 | constexpr auto&& __as_base() const && { return _VSTD::move(*this); } |
| 789 | |
| 790 | inline _LIBCPP_INLINE_VISIBILITY |
| 791 | static constexpr size_t __size() { return sizeof...(_Types); } |
| 792 | |
| 793 | __union<_DestructibleTrait, 0, _Types...> __data; |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 794 | __index_t __index; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 795 | |
| 796 | friend struct __access::__base; |
| 797 | friend struct __visitation::__base; |
| 798 | }; |
| 799 | |
| 800 | template <class _Traits, _Trait = _Traits::__destructible_trait> |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 801 | class _LIBCPP_TEMPLATE_VIS __dtor; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 802 | |
| 803 | #define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \ |
| 804 | template <class... _Types> \ |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 805 | class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, \ |
| 806 | destructible_trait> \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 807 | : public __base<destructible_trait, _Types...> { \ |
| 808 | using __base_type = __base<destructible_trait, _Types...>; \ |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 809 | using __index_t = typename __base_type::__index_t; \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 810 | \ |
| 811 | public: \ |
| 812 | using __base_type::__base_type; \ |
| 813 | using __base_type::operator=; \ |
| 814 | \ |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 815 | __dtor(const __dtor&) = default; \ |
| 816 | __dtor(__dtor&&) = default; \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 817 | destructor \ |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 818 | __dtor& operator=(const __dtor&) = default; \ |
| 819 | __dtor& operator=(__dtor&&) = default; \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 820 | \ |
| 821 | protected: \ |
| 822 | inline _LIBCPP_INLINE_VISIBILITY \ |
| 823 | destroy \ |
| 824 | } |
| 825 | |
| 826 | _LIBCPP_VARIANT_DESTRUCTOR( |
| 827 | _Trait::_TriviallyAvailable, |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 828 | ~__dtor() = default;, |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 829 | void __destroy() noexcept { this->__index = __variant_npos<__index_t>; }); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 830 | |
| 831 | _LIBCPP_VARIANT_DESTRUCTOR( |
| 832 | _Trait::_Available, |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 833 | ~__dtor() { __destroy(); }, |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 834 | void __destroy() noexcept { |
| 835 | if (!this->valueless_by_exception()) { |
| 836 | __visitation::__base::__visit_alt( |
| 837 | [](auto& __alt) noexcept { |
Marshall Clow | e61ba95 | 2018-02-12 15:41:25 +0000 | [diff] [blame] | 838 | using __alt_type = __uncvref_t<decltype(__alt)>; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 839 | __alt.~__alt_type(); |
| 840 | }, |
| 841 | *this); |
| 842 | } |
Eric Fiselier | caccc7b | 2017-11-19 04:19:44 +0000 | [diff] [blame] | 843 | this->__index = __variant_npos<__index_t>; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 844 | }); |
| 845 | |
| 846 | _LIBCPP_VARIANT_DESTRUCTOR( |
| 847 | _Trait::_Unavailable, |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 848 | ~__dtor() = delete;, |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 849 | void __destroy() noexcept = delete;); |
| 850 | |
| 851 | #undef _LIBCPP_VARIANT_DESTRUCTOR |
| 852 | |
| 853 | template <class _Traits> |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 854 | class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> { |
| 855 | using __base_type = __dtor<_Traits>; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 856 | |
| 857 | public: |
| 858 | using __base_type::__base_type; |
| 859 | using __base_type::operator=; |
| 860 | |
| 861 | protected: |
| 862 | template <size_t _Ip, class _Tp, class... _Args> |
| 863 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 864 | static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) { |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 865 | ::new ((void*)_VSTD::addressof(__a)) |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 866 | __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...); |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 867 | return __a.__value; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | template <class _Rhs> |
| 871 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 872 | static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 873 | __lhs.__destroy(); |
| 874 | if (!__rhs.valueless_by_exception()) { |
| 875 | __visitation::__base::__visit_alt_at( |
| 876 | __rhs.index(), |
| 877 | [](auto& __lhs_alt, auto&& __rhs_alt) { |
| 878 | __construct_alt( |
| 879 | __lhs_alt, |
| 880 | _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value); |
| 881 | }, |
| 882 | __lhs, _VSTD::forward<_Rhs>(__rhs)); |
| 883 | __lhs.__index = __rhs.index(); |
| 884 | } |
| 885 | } |
| 886 | }; |
| 887 | |
| 888 | template <class _Traits, _Trait = _Traits::__move_constructible_trait> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 889 | class _LIBCPP_TEMPLATE_VIS __move_constructor; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 890 | |
| 891 | #define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \ |
| 892 | move_constructor) \ |
| 893 | template <class... _Types> \ |
Louis Dionne | ff05add | 2020-10-05 16:30:23 -0400 | [diff] [blame] | 894 | class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \ |
| 895 | move_constructible_trait> \ |
| 896 | : public __ctor<__traits<_Types...>> { \ |
| 897 | using __base_type = __ctor<__traits<_Types...>>; \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 898 | \ |
| 899 | public: \ |
| 900 | using __base_type::__base_type; \ |
| 901 | using __base_type::operator=; \ |
| 902 | \ |
| 903 | __move_constructor(const __move_constructor&) = default; \ |
| 904 | move_constructor \ |
| 905 | ~__move_constructor() = default; \ |
| 906 | __move_constructor& operator=(const __move_constructor&) = default; \ |
| 907 | __move_constructor& operator=(__move_constructor&&) = default; \ |
| 908 | } |
| 909 | |
| 910 | _LIBCPP_VARIANT_MOVE_CONSTRUCTOR( |
| 911 | _Trait::_TriviallyAvailable, |
| 912 | __move_constructor(__move_constructor&& __that) = default;); |
| 913 | |
| 914 | _LIBCPP_VARIANT_MOVE_CONSTRUCTOR( |
| 915 | _Trait::_Available, |
| 916 | __move_constructor(__move_constructor&& __that) noexcept( |
| 917 | __all<is_nothrow_move_constructible_v<_Types>...>::value) |
| 918 | : __move_constructor(__valueless_t{}) { |
| 919 | this->__generic_construct(*this, _VSTD::move(__that)); |
| 920 | }); |
| 921 | |
| 922 | _LIBCPP_VARIANT_MOVE_CONSTRUCTOR( |
| 923 | _Trait::_Unavailable, |
| 924 | __move_constructor(__move_constructor&&) = delete;); |
| 925 | |
| 926 | #undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR |
| 927 | |
| 928 | template <class _Traits, _Trait = _Traits::__copy_constructible_trait> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 929 | class _LIBCPP_TEMPLATE_VIS __copy_constructor; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 930 | |
| 931 | #define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \ |
| 932 | copy_constructor) \ |
| 933 | template <class... _Types> \ |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 934 | class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 935 | copy_constructible_trait> \ |
| 936 | : public __move_constructor<__traits<_Types...>> { \ |
| 937 | using __base_type = __move_constructor<__traits<_Types...>>; \ |
| 938 | \ |
| 939 | public: \ |
| 940 | using __base_type::__base_type; \ |
| 941 | using __base_type::operator=; \ |
| 942 | \ |
| 943 | copy_constructor \ |
| 944 | __copy_constructor(__copy_constructor&&) = default; \ |
| 945 | ~__copy_constructor() = default; \ |
| 946 | __copy_constructor& operator=(const __copy_constructor&) = default; \ |
| 947 | __copy_constructor& operator=(__copy_constructor&&) = default; \ |
| 948 | } |
| 949 | |
| 950 | _LIBCPP_VARIANT_COPY_CONSTRUCTOR( |
| 951 | _Trait::_TriviallyAvailable, |
| 952 | __copy_constructor(const __copy_constructor& __that) = default;); |
| 953 | |
| 954 | _LIBCPP_VARIANT_COPY_CONSTRUCTOR( |
| 955 | _Trait::_Available, |
| 956 | __copy_constructor(const __copy_constructor& __that) |
| 957 | : __copy_constructor(__valueless_t{}) { |
| 958 | this->__generic_construct(*this, __that); |
| 959 | }); |
| 960 | |
| 961 | _LIBCPP_VARIANT_COPY_CONSTRUCTOR( |
| 962 | _Trait::_Unavailable, |
| 963 | __copy_constructor(const __copy_constructor&) = delete;); |
| 964 | |
| 965 | #undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR |
| 966 | |
| 967 | template <class _Traits> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 968 | class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 969 | using __base_type = __copy_constructor<_Traits>; |
| 970 | |
| 971 | public: |
| 972 | using __base_type::__base_type; |
| 973 | using __base_type::operator=; |
| 974 | |
| 975 | template <size_t _Ip, class... _Args> |
| 976 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 977 | auto& __emplace(_Args&&... __args) { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 978 | this->__destroy(); |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 979 | auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this), |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 980 | _VSTD::forward<_Args>(__args)...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 981 | this->__index = _Ip; |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 982 | return __res; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | protected: |
Michael Park | 6211835 | 2017-06-07 10:22:43 +0000 | [diff] [blame] | 986 | template <size_t _Ip, class _Tp, class _Arg> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 987 | inline _LIBCPP_INLINE_VISIBILITY |
Michael Park | 6211835 | 2017-06-07 10:22:43 +0000 | [diff] [blame] | 988 | void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 989 | if (this->index() == _Ip) { |
| 990 | __a.__value = _VSTD::forward<_Arg>(__arg); |
| 991 | } else { |
| 992 | struct { |
| 993 | void operator()(true_type) const { |
Michael Park | 6211835 | 2017-06-07 10:22:43 +0000 | [diff] [blame] | 994 | __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg)); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 995 | } |
| 996 | void operator()(false_type) const { |
Michael Park | 6211835 | 2017-06-07 10:22:43 +0000 | [diff] [blame] | 997 | __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg))); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 998 | } |
| 999 | __assignment* __this; |
| 1000 | _Arg&& __arg; |
| 1001 | } __impl{this, _VSTD::forward<_Arg>(__arg)}; |
Michael Park | 6211835 | 2017-06-07 10:22:43 +0000 | [diff] [blame] | 1002 | __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> || |
| 1003 | !is_nothrow_move_constructible_v<_Tp>>{}); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | template <class _That> |
| 1008 | inline _LIBCPP_INLINE_VISIBILITY |
| 1009 | void __generic_assign(_That&& __that) { |
| 1010 | if (this->valueless_by_exception() && __that.valueless_by_exception()) { |
| 1011 | // do nothing. |
| 1012 | } else if (__that.valueless_by_exception()) { |
| 1013 | this->__destroy(); |
| 1014 | } else { |
| 1015 | __visitation::__base::__visit_alt_at( |
| 1016 | __that.index(), |
| 1017 | [this](auto& __this_alt, auto&& __that_alt) { |
| 1018 | this->__assign_alt( |
| 1019 | __this_alt, |
Michael Park | 6211835 | 2017-06-07 10:22:43 +0000 | [diff] [blame] | 1020 | _VSTD::forward<decltype(__that_alt)>(__that_alt).__value); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1021 | }, |
| 1022 | *this, _VSTD::forward<_That>(__that)); |
| 1023 | } |
| 1024 | } |
| 1025 | }; |
| 1026 | |
| 1027 | template <class _Traits, _Trait = _Traits::__move_assignable_trait> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1028 | class _LIBCPP_TEMPLATE_VIS __move_assignment; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1029 | |
| 1030 | #define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \ |
| 1031 | move_assignment) \ |
| 1032 | template <class... _Types> \ |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 1033 | class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1034 | move_assignable_trait> \ |
| 1035 | : public __assignment<__traits<_Types...>> { \ |
| 1036 | using __base_type = __assignment<__traits<_Types...>>; \ |
| 1037 | \ |
| 1038 | public: \ |
| 1039 | using __base_type::__base_type; \ |
| 1040 | using __base_type::operator=; \ |
| 1041 | \ |
| 1042 | __move_assignment(const __move_assignment&) = default; \ |
| 1043 | __move_assignment(__move_assignment&&) = default; \ |
| 1044 | ~__move_assignment() = default; \ |
| 1045 | __move_assignment& operator=(const __move_assignment&) = default; \ |
| 1046 | move_assignment \ |
| 1047 | } |
| 1048 | |
| 1049 | _LIBCPP_VARIANT_MOVE_ASSIGNMENT( |
| 1050 | _Trait::_TriviallyAvailable, |
| 1051 | __move_assignment& operator=(__move_assignment&& __that) = default;); |
| 1052 | |
| 1053 | _LIBCPP_VARIANT_MOVE_ASSIGNMENT( |
| 1054 | _Trait::_Available, |
| 1055 | __move_assignment& operator=(__move_assignment&& __that) noexcept( |
| 1056 | __all<(is_nothrow_move_constructible_v<_Types> && |
| 1057 | is_nothrow_move_assignable_v<_Types>)...>::value) { |
| 1058 | this->__generic_assign(_VSTD::move(__that)); |
| 1059 | return *this; |
| 1060 | }); |
| 1061 | |
| 1062 | _LIBCPP_VARIANT_MOVE_ASSIGNMENT( |
| 1063 | _Trait::_Unavailable, |
| 1064 | __move_assignment& operator=(__move_assignment&&) = delete;); |
| 1065 | |
| 1066 | #undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT |
| 1067 | |
| 1068 | template <class _Traits, _Trait = _Traits::__copy_assignable_trait> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1069 | class _LIBCPP_TEMPLATE_VIS __copy_assignment; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1070 | |
| 1071 | #define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \ |
| 1072 | copy_assignment) \ |
| 1073 | template <class... _Types> \ |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 1074 | class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \ |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1075 | copy_assignable_trait> \ |
| 1076 | : public __move_assignment<__traits<_Types...>> { \ |
| 1077 | using __base_type = __move_assignment<__traits<_Types...>>; \ |
| 1078 | \ |
| 1079 | public: \ |
| 1080 | using __base_type::__base_type; \ |
| 1081 | using __base_type::operator=; \ |
| 1082 | \ |
| 1083 | __copy_assignment(const __copy_assignment&) = default; \ |
| 1084 | __copy_assignment(__copy_assignment&&) = default; \ |
| 1085 | ~__copy_assignment() = default; \ |
| 1086 | copy_assignment \ |
| 1087 | __copy_assignment& operator=(__copy_assignment&&) = default; \ |
| 1088 | } |
| 1089 | |
| 1090 | _LIBCPP_VARIANT_COPY_ASSIGNMENT( |
| 1091 | _Trait::_TriviallyAvailable, |
| 1092 | __copy_assignment& operator=(const __copy_assignment& __that) = default;); |
| 1093 | |
| 1094 | _LIBCPP_VARIANT_COPY_ASSIGNMENT( |
| 1095 | _Trait::_Available, |
| 1096 | __copy_assignment& operator=(const __copy_assignment& __that) { |
| 1097 | this->__generic_assign(__that); |
| 1098 | return *this; |
| 1099 | }); |
| 1100 | |
| 1101 | _LIBCPP_VARIANT_COPY_ASSIGNMENT( |
| 1102 | _Trait::_Unavailable, |
| 1103 | __copy_assignment& operator=(const __copy_assignment&) = delete;); |
| 1104 | |
| 1105 | #undef _LIBCPP_VARIANT_COPY_ASSIGNMENT |
| 1106 | |
| 1107 | template <class... _Types> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1108 | class _LIBCPP_TEMPLATE_VIS __impl |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1109 | : public __copy_assignment<__traits<_Types...>> { |
| 1110 | using __base_type = __copy_assignment<__traits<_Types...>>; |
| 1111 | |
| 1112 | public: |
| 1113 | using __base_type::__base_type; |
| 1114 | using __base_type::operator=; |
| 1115 | |
| 1116 | template <size_t _Ip, class _Arg> |
| 1117 | inline _LIBCPP_INLINE_VISIBILITY |
| 1118 | void __assign(_Arg&& __arg) { |
| 1119 | this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), |
Michael Park | 6211835 | 2017-06-07 10:22:43 +0000 | [diff] [blame] | 1120 | _VSTD::forward<_Arg>(__arg)); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | inline _LIBCPP_INLINE_VISIBILITY |
| 1124 | void __swap(__impl& __that) { |
| 1125 | if (this->valueless_by_exception() && __that.valueless_by_exception()) { |
| 1126 | // do nothing. |
| 1127 | } else if (this->index() == __that.index()) { |
| 1128 | __visitation::__base::__visit_alt_at( |
| 1129 | this->index(), |
| 1130 | [](auto& __this_alt, auto& __that_alt) { |
| 1131 | using _VSTD::swap; |
| 1132 | swap(__this_alt.__value, __that_alt.__value); |
| 1133 | }, |
| 1134 | *this, |
| 1135 | __that); |
| 1136 | } else { |
| 1137 | __impl* __lhs = this; |
| 1138 | __impl* __rhs = _VSTD::addressof(__that); |
| 1139 | if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) { |
| 1140 | _VSTD::swap(__lhs, __rhs); |
| 1141 | } |
| 1142 | __impl __tmp(_VSTD::move(*__rhs)); |
Michael Park | 5dee734 | 2020-06-16 15:15:10 -0700 | [diff] [blame] | 1143 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Michael Park | 6a9ef64 | 2020-07-08 10:46:02 -0700 | [diff] [blame] | 1144 | if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) { |
Michael Park | 31967aa | 2020-06-16 13:29:23 -0700 | [diff] [blame] | 1145 | this->__generic_construct(*__rhs, _VSTD::move(*__lhs)); |
| 1146 | } else { |
| 1147 | // EXTENSION: When the move construction of `__lhs` into `__rhs` throws |
| 1148 | // and `__tmp` is nothrow move constructible then we move `__tmp` back |
| 1149 | // into `__rhs` and provide the strong exception safety guarantee. |
| 1150 | try { |
| 1151 | this->__generic_construct(*__rhs, _VSTD::move(*__lhs)); |
| 1152 | } catch (...) { |
| 1153 | if (__tmp.__move_nothrow()) { |
| 1154 | this->__generic_construct(*__rhs, _VSTD::move(__tmp)); |
| 1155 | } |
| 1156 | throw; |
| 1157 | } |
| 1158 | } |
Michael Park | 5dee734 | 2020-06-16 15:15:10 -0700 | [diff] [blame] | 1159 | #else |
| 1160 | // this isn't consolidated with the `if constexpr` branch above due to |
| 1161 | // `throw` being ill-formed with exceptions disabled even when discarded. |
| 1162 | this->__generic_construct(*__rhs, _VSTD::move(*__lhs)); |
| 1163 | #endif |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1164 | this->__generic_construct(*__lhs, _VSTD::move(__tmp)); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | private: |
| 1169 | inline _LIBCPP_INLINE_VISIBILITY |
| 1170 | bool __move_nothrow() const { |
| 1171 | constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...}; |
| 1172 | return this->valueless_by_exception() || __results[this->index()]; |
| 1173 | } |
| 1174 | }; |
| 1175 | |
Eric Fiselier | 3aaf8a3 | 2019-07-14 18:21:15 +0000 | [diff] [blame] | 1176 | struct __no_narrowing_check { |
| 1177 | template <class _Dest, class _Source> |
| 1178 | using _Apply = __identity<_Dest>; |
| 1179 | }; |
| 1180 | |
| 1181 | struct __narrowing_check { |
| 1182 | template <class _Dest> |
| 1183 | static auto __test_impl(_Dest (&&)[1]) -> __identity<_Dest>; |
| 1184 | template <class _Dest, class _Source> |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1185 | using _Apply _LIBCPP_NODEBUG_TYPE = decltype(__test_impl<_Dest>({declval<_Source>()})); |
Eric Fiselier | 3aaf8a3 | 2019-07-14 18:21:15 +0000 | [diff] [blame] | 1186 | }; |
| 1187 | |
| 1188 | template <class _Dest, class _Source> |
Eric Fiselier | aa7cdca | 2019-07-14 21:29:39 +0000 | [diff] [blame] | 1189 | using __check_for_narrowing _LIBCPP_NODEBUG_TYPE = |
| 1190 | typename _If< |
Eric Fiselier | 3aaf8a3 | 2019-07-14 18:21:15 +0000 | [diff] [blame] | 1191 | #ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT |
| 1192 | false && |
| 1193 | #endif |
| 1194 | is_arithmetic<_Dest>::value, |
| 1195 | __narrowing_check, |
| 1196 | __no_narrowing_check |
Eric Fiselier | aa7cdca | 2019-07-14 21:29:39 +0000 | [diff] [blame] | 1197 | >::template _Apply<_Dest, _Source>; |
Eric Fiselier | 3aaf8a3 | 2019-07-14 18:21:15 +0000 | [diff] [blame] | 1198 | |
Eric Fiselier | aa7cdca | 2019-07-14 21:29:39 +0000 | [diff] [blame] | 1199 | template <class _Tp, size_t _Idx> |
| 1200 | struct __overload { |
Zhihao Yuan | 821efb8 | 2019-06-20 22:09:40 +0000 | [diff] [blame] | 1201 | template <class _Up> |
Eric Fiselier | 80f5dfb | 2019-07-14 18:31:55 +0000 | [diff] [blame] | 1202 | auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1203 | }; |
| 1204 | |
Eric Fiselier | aa7cdca | 2019-07-14 21:29:39 +0000 | [diff] [blame] | 1205 | template <class _Tp, size_t> |
| 1206 | struct __overload_bool { |
Zhihao Yuan | 821efb8 | 2019-06-20 22:09:40 +0000 | [diff] [blame] | 1207 | template <class _Up, class _Ap = __uncvref_t<_Up>> |
| 1208 | auto operator()(bool, _Up&&) const |
| 1209 | -> enable_if_t<is_same_v<_Ap, bool>, __identity<_Tp>>; |
| 1210 | }; |
| 1211 | |
Eric Fiselier | aa7cdca | 2019-07-14 21:29:39 +0000 | [diff] [blame] | 1212 | template <size_t _Idx> |
| 1213 | struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {}; |
| 1214 | template <size_t _Idx> |
| 1215 | struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {}; |
| 1216 | template <size_t _Idx> |
| 1217 | struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {}; |
| 1218 | template <size_t _Idx> |
| 1219 | struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {}; |
| 1220 | |
| 1221 | template <class ..._Bases> |
| 1222 | struct __all_overloads : _Bases... { |
| 1223 | void operator()() const; |
| 1224 | using _Bases::operator()...; |
| 1225 | }; |
| 1226 | |
| 1227 | template <class IdxSeq> |
| 1228 | struct __make_overloads_imp; |
| 1229 | |
| 1230 | template <size_t ..._Idx> |
| 1231 | struct __make_overloads_imp<__tuple_indices<_Idx...> > { |
| 1232 | template <class ..._Types> |
| 1233 | using _Apply _LIBCPP_NODEBUG_TYPE = __all_overloads<__overload<_Types, _Idx>...>; |
| 1234 | }; |
| 1235 | |
| 1236 | template <class ..._Types> |
| 1237 | using _MakeOverloads _LIBCPP_NODEBUG_TYPE = typename __make_overloads_imp< |
| 1238 | __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>; |
Zhihao Yuan | 821efb8 | 2019-06-20 22:09:40 +0000 | [diff] [blame] | 1239 | |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1240 | template <class _Tp, class... _Types> |
Zhihao Yuan | 821efb8 | 2019-06-20 22:09:40 +0000 | [diff] [blame] | 1241 | using __best_match_t = |
Eric Fiselier | aa7cdca | 2019-07-14 21:29:39 +0000 | [diff] [blame] | 1242 | typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1243 | |
| 1244 | } // __variant_detail |
| 1245 | |
| 1246 | template <class... _Types> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1247 | class _LIBCPP_TEMPLATE_VIS variant |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1248 | : private __sfinae_ctor_base< |
| 1249 | __all<is_copy_constructible_v<_Types>...>::value, |
| 1250 | __all<is_move_constructible_v<_Types>...>::value>, |
| 1251 | private __sfinae_assign_base< |
| 1252 | __all<(is_copy_constructible_v<_Types> && |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1253 | is_copy_assignable_v<_Types>)...>::value, |
| 1254 | __all<(is_move_constructible_v<_Types> && |
| 1255 | is_move_assignable_v<_Types>)...>::value> { |
| 1256 | static_assert(0 < sizeof...(_Types), |
| 1257 | "variant must consist of at least one alternative."); |
| 1258 | |
| 1259 | static_assert(__all<!is_array_v<_Types>...>::value, |
| 1260 | "variant can not have an array type as an alternative."); |
| 1261 | |
| 1262 | static_assert(__all<!is_reference_v<_Types>...>::value, |
| 1263 | "variant can not have a reference type as an alternative."); |
| 1264 | |
| 1265 | static_assert(__all<!is_void_v<_Types>...>::value, |
| 1266 | "variant can not have a void type as an alternative."); |
| 1267 | |
| 1268 | using __first_type = variant_alternative_t<0, variant>; |
| 1269 | |
| 1270 | public: |
| 1271 | template <bool _Dummy = true, |
| 1272 | enable_if_t<__dependent_type<is_default_constructible<__first_type>, |
| 1273 | _Dummy>::value, |
| 1274 | int> = 0> |
| 1275 | inline _LIBCPP_INLINE_VISIBILITY |
| 1276 | constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>) |
| 1277 | : __impl(in_place_index<0>) {} |
| 1278 | |
| 1279 | variant(const variant&) = default; |
| 1280 | variant(variant&&) = default; |
| 1281 | |
| 1282 | template < |
| 1283 | class _Arg, |
Marshall Clow | d5bbc24 | 2018-02-06 20:56:55 +0000 | [diff] [blame] | 1284 | enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0, |
| 1285 | enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0, |
| 1286 | enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0, |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1287 | class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>, |
| 1288 | size_t _Ip = |
| 1289 | __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, |
| 1290 | enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0> |
| 1291 | inline _LIBCPP_INLINE_VISIBILITY |
| 1292 | constexpr variant(_Arg&& __arg) noexcept( |
| 1293 | is_nothrow_constructible_v<_Tp, _Arg>) |
| 1294 | : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {} |
| 1295 | |
| 1296 | template <size_t _Ip, class... _Args, |
Eric Fiselier | 5d2df75 | 2018-03-23 23:42:30 +0000 | [diff] [blame] | 1297 | class = enable_if_t<(_Ip < sizeof...(_Types)), int>, |
| 1298 | class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, |
| 1299 | enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1300 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 5d2df75 | 2018-03-23 23:42:30 +0000 | [diff] [blame] | 1301 | explicit constexpr variant( |
| 1302 | in_place_index_t<_Ip>, |
| 1303 | _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>) |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1304 | : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {} |
| 1305 | |
Eric Fiselier | 5d2df75 | 2018-03-23 23:42:30 +0000 | [diff] [blame] | 1306 | template < |
| 1307 | size_t _Ip, |
| 1308 | class _Up, |
| 1309 | class... _Args, |
| 1310 | enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, |
| 1311 | class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1312 | enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, |
| 1313 | int> = 0> |
| 1314 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 5d2df75 | 2018-03-23 23:42:30 +0000 | [diff] [blame] | 1315 | explicit constexpr variant( |
| 1316 | in_place_index_t<_Ip>, |
| 1317 | initializer_list<_Up> __il, |
| 1318 | _Args&&... __args) noexcept( |
| 1319 | is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1320 | : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {} |
| 1321 | |
| 1322 | template < |
| 1323 | class _Tp, |
| 1324 | class... _Args, |
| 1325 | size_t _Ip = |
| 1326 | __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, |
| 1327 | enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> |
| 1328 | inline _LIBCPP_INLINE_VISIBILITY |
| 1329 | explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept( |
| 1330 | is_nothrow_constructible_v<_Tp, _Args...>) |
| 1331 | : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {} |
| 1332 | |
| 1333 | template < |
| 1334 | class _Tp, |
| 1335 | class _Up, |
| 1336 | class... _Args, |
| 1337 | size_t _Ip = |
| 1338 | __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, |
| 1339 | enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, |
| 1340 | int> = 0> |
| 1341 | inline _LIBCPP_INLINE_VISIBILITY |
| 1342 | explicit constexpr variant( |
| 1343 | in_place_type_t<_Tp>, |
| 1344 | initializer_list<_Up> __il, |
| 1345 | _Args&&... __args) noexcept( |
| 1346 | is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>) |
| 1347 | : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {} |
| 1348 | |
| 1349 | ~variant() = default; |
| 1350 | |
| 1351 | variant& operator=(const variant&) = default; |
| 1352 | variant& operator=(variant&&) = default; |
| 1353 | |
| 1354 | template < |
| 1355 | class _Arg, |
Marshall Clow | d5bbc24 | 2018-02-06 20:56:55 +0000 | [diff] [blame] | 1356 | enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0, |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1357 | class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>, |
| 1358 | size_t _Ip = |
| 1359 | __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, |
| 1360 | enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, |
| 1361 | int> = 0> |
| 1362 | inline _LIBCPP_INLINE_VISIBILITY |
| 1363 | variant& operator=(_Arg&& __arg) noexcept( |
| 1364 | is_nothrow_assignable_v<_Tp&, _Arg> && |
| 1365 | is_nothrow_constructible_v<_Tp, _Arg>) { |
| 1366 | __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg)); |
| 1367 | return *this; |
| 1368 | } |
| 1369 | |
| 1370 | template < |
| 1371 | size_t _Ip, |
| 1372 | class... _Args, |
| 1373 | enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, |
| 1374 | class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, |
| 1375 | enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> |
| 1376 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 1377 | _Tp& emplace(_Args&&... __args) { |
| 1378 | return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | template < |
| 1382 | size_t _Ip, |
| 1383 | class _Up, |
| 1384 | class... _Args, |
| 1385 | enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, |
| 1386 | class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, |
| 1387 | enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, |
| 1388 | int> = 0> |
| 1389 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 1390 | _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { |
| 1391 | return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | template < |
| 1395 | class _Tp, |
| 1396 | class... _Args, |
| 1397 | size_t _Ip = |
| 1398 | __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, |
| 1399 | enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> |
| 1400 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 1401 | _Tp& emplace(_Args&&... __args) { |
| 1402 | return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | template < |
| 1406 | class _Tp, |
| 1407 | class _Up, |
| 1408 | class... _Args, |
| 1409 | size_t _Ip = |
| 1410 | __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, |
| 1411 | enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, |
| 1412 | int> = 0> |
| 1413 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 6292844 | 2017-04-15 19:32:02 +0000 | [diff] [blame] | 1414 | _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { |
| 1415 | return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | inline _LIBCPP_INLINE_VISIBILITY |
| 1419 | constexpr bool valueless_by_exception() const noexcept { |
| 1420 | return __impl.valueless_by_exception(); |
| 1421 | } |
| 1422 | |
| 1423 | inline _LIBCPP_INLINE_VISIBILITY |
| 1424 | constexpr size_t index() const noexcept { return __impl.index(); } |
| 1425 | |
| 1426 | template < |
| 1427 | bool _Dummy = true, |
| 1428 | enable_if_t< |
| 1429 | __all<( |
| 1430 | __dependent_type<is_move_constructible<_Types>, _Dummy>::value && |
| 1431 | __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value, |
| 1432 | int> = 0> |
| 1433 | inline _LIBCPP_INLINE_VISIBILITY |
| 1434 | void swap(variant& __that) noexcept( |
| 1435 | __all<(is_nothrow_move_constructible_v<_Types> && |
| 1436 | is_nothrow_swappable_v<_Types>)...>::value) { |
| 1437 | __impl.__swap(__that.__impl); |
| 1438 | } |
| 1439 | |
| 1440 | private: |
| 1441 | __variant_detail::__impl<_Types...> __impl; |
| 1442 | |
| 1443 | friend struct __variant_detail::__access::__variant; |
| 1444 | friend struct __variant_detail::__visitation::__variant; |
| 1445 | }; |
| 1446 | |
| 1447 | template <size_t _Ip, class... _Types> |
| 1448 | inline _LIBCPP_INLINE_VISIBILITY |
| 1449 | constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept { |
| 1450 | return __v.index() == _Ip; |
| 1451 | } |
| 1452 | |
| 1453 | template <class _Tp, class... _Types> |
| 1454 | inline _LIBCPP_INLINE_VISIBILITY |
| 1455 | constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept { |
| 1456 | return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v); |
| 1457 | } |
| 1458 | |
| 1459 | template <size_t _Ip, class _Vp> |
| 1460 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1461 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Richard Smith | 66d1c55 | 2018-08-27 21:41:50 +0000 | [diff] [blame] | 1462 | constexpr auto&& __generic_get(_Vp&& __v) { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1463 | using __variant_detail::__access::__variant; |
| 1464 | if (!__holds_alternative<_Ip>(__v)) { |
Eric Fiselier | 10642ea | 2016-12-03 01:58:07 +0000 | [diff] [blame] | 1465 | __throw_bad_variant_access(); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1466 | } |
| 1467 | return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value; |
| 1468 | } |
| 1469 | |
| 1470 | template <size_t _Ip, class... _Types> |
| 1471 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1472 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1473 | constexpr variant_alternative_t<_Ip, variant<_Types...>>& get( |
| 1474 | variant<_Types...>& __v) { |
| 1475 | static_assert(_Ip < sizeof...(_Types)); |
| 1476 | static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); |
| 1477 | return __generic_get<_Ip>(__v); |
| 1478 | } |
| 1479 | |
| 1480 | template <size_t _Ip, class... _Types> |
| 1481 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1482 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1483 | constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get( |
| 1484 | variant<_Types...>&& __v) { |
| 1485 | static_assert(_Ip < sizeof...(_Types)); |
| 1486 | static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); |
| 1487 | return __generic_get<_Ip>(_VSTD::move(__v)); |
| 1488 | } |
| 1489 | |
| 1490 | template <size_t _Ip, class... _Types> |
| 1491 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1492 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1493 | constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get( |
| 1494 | const variant<_Types...>& __v) { |
| 1495 | static_assert(_Ip < sizeof...(_Types)); |
| 1496 | static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); |
| 1497 | return __generic_get<_Ip>(__v); |
| 1498 | } |
| 1499 | |
| 1500 | template <size_t _Ip, class... _Types> |
| 1501 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1502 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1503 | constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get( |
| 1504 | const variant<_Types...>&& __v) { |
| 1505 | static_assert(_Ip < sizeof...(_Types)); |
| 1506 | static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); |
| 1507 | return __generic_get<_Ip>(_VSTD::move(__v)); |
| 1508 | } |
| 1509 | |
| 1510 | template <class _Tp, class... _Types> |
| 1511 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1512 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1513 | constexpr _Tp& get(variant<_Types...>& __v) { |
| 1514 | static_assert(!is_void_v<_Tp>); |
| 1515 | return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); |
| 1516 | } |
| 1517 | |
| 1518 | template <class _Tp, class... _Types> |
| 1519 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1520 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1521 | constexpr _Tp&& get(variant<_Types...>&& __v) { |
| 1522 | static_assert(!is_void_v<_Tp>); |
| 1523 | return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>( |
| 1524 | _VSTD::move(__v)); |
| 1525 | } |
| 1526 | |
| 1527 | template <class _Tp, class... _Types> |
| 1528 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1529 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1530 | constexpr const _Tp& get(const variant<_Types...>& __v) { |
| 1531 | static_assert(!is_void_v<_Tp>); |
| 1532 | return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); |
| 1533 | } |
| 1534 | |
| 1535 | template <class _Tp, class... _Types> |
| 1536 | inline _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | cef92e6 | 2018-11-19 15:37:04 +0000 | [diff] [blame] | 1537 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1538 | constexpr const _Tp&& get(const variant<_Types...>&& __v) { |
| 1539 | static_assert(!is_void_v<_Tp>); |
| 1540 | return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>( |
| 1541 | _VSTD::move(__v)); |
| 1542 | } |
| 1543 | |
| 1544 | template <size_t _Ip, class _Vp> |
| 1545 | inline _LIBCPP_INLINE_VISIBILITY |
| 1546 | constexpr auto* __generic_get_if(_Vp* __v) noexcept { |
| 1547 | using __variant_detail::__access::__variant; |
| 1548 | return __v && __holds_alternative<_Ip>(*__v) |
| 1549 | ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value) |
| 1550 | : nullptr; |
| 1551 | } |
| 1552 | |
| 1553 | template <size_t _Ip, class... _Types> |
| 1554 | inline _LIBCPP_INLINE_VISIBILITY |
| 1555 | constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>> |
| 1556 | get_if(variant<_Types...>* __v) noexcept { |
| 1557 | static_assert(_Ip < sizeof...(_Types)); |
| 1558 | static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); |
| 1559 | return __generic_get_if<_Ip>(__v); |
| 1560 | } |
| 1561 | |
| 1562 | template <size_t _Ip, class... _Types> |
| 1563 | inline _LIBCPP_INLINE_VISIBILITY |
| 1564 | constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>> |
| 1565 | get_if(const variant<_Types...>* __v) noexcept { |
| 1566 | static_assert(_Ip < sizeof...(_Types)); |
| 1567 | static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); |
| 1568 | return __generic_get_if<_Ip>(__v); |
| 1569 | } |
| 1570 | |
| 1571 | template <class _Tp, class... _Types> |
| 1572 | inline _LIBCPP_INLINE_VISIBILITY |
| 1573 | constexpr add_pointer_t<_Tp> |
| 1574 | get_if(variant<_Types...>* __v) noexcept { |
| 1575 | static_assert(!is_void_v<_Tp>); |
| 1576 | return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); |
| 1577 | } |
| 1578 | |
| 1579 | template <class _Tp, class... _Types> |
| 1580 | inline _LIBCPP_INLINE_VISIBILITY |
| 1581 | constexpr add_pointer_t<const _Tp> |
| 1582 | get_if(const variant<_Types...>* __v) noexcept { |
| 1583 | static_assert(!is_void_v<_Tp>); |
| 1584 | return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); |
| 1585 | } |
| 1586 | |
Eric Fiselier | fb6a2bc | 2018-09-19 17:53:21 +0000 | [diff] [blame] | 1587 | template <class _Operator> |
| 1588 | struct __convert_to_bool { |
| 1589 | template <class _T1, class _T2> |
| 1590 | _LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 1591 | static_assert(is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value, |
Eric Fiselier | fb6a2bc | 2018-09-19 17:53:21 +0000 | [diff] [blame] | 1592 | "the relational operator does not return a type which is implicitly convertible to bool"); |
| 1593 | return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2)); |
| 1594 | } |
| 1595 | }; |
| 1596 | |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1597 | template <class... _Types> |
| 1598 | inline _LIBCPP_INLINE_VISIBILITY |
| 1599 | constexpr bool operator==(const variant<_Types...>& __lhs, |
| 1600 | const variant<_Types...>& __rhs) { |
| 1601 | using __variant_detail::__visitation::__variant; |
| 1602 | if (__lhs.index() != __rhs.index()) return false; |
| 1603 | if (__lhs.valueless_by_exception()) return true; |
Eric Fiselier | fb6a2bc | 2018-09-19 17:53:21 +0000 | [diff] [blame] | 1604 | return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | template <class... _Types> |
| 1608 | inline _LIBCPP_INLINE_VISIBILITY |
| 1609 | constexpr bool operator!=(const variant<_Types...>& __lhs, |
| 1610 | const variant<_Types...>& __rhs) { |
| 1611 | using __variant_detail::__visitation::__variant; |
| 1612 | if (__lhs.index() != __rhs.index()) return true; |
| 1613 | if (__lhs.valueless_by_exception()) return false; |
| 1614 | return __variant::__visit_value_at( |
Eric Fiselier | fb6a2bc | 2018-09-19 17:53:21 +0000 | [diff] [blame] | 1615 | __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | template <class... _Types> |
| 1619 | inline _LIBCPP_INLINE_VISIBILITY |
| 1620 | constexpr bool operator<(const variant<_Types...>& __lhs, |
| 1621 | const variant<_Types...>& __rhs) { |
| 1622 | using __variant_detail::__visitation::__variant; |
| 1623 | if (__rhs.valueless_by_exception()) return false; |
| 1624 | if (__lhs.valueless_by_exception()) return true; |
| 1625 | if (__lhs.index() < __rhs.index()) return true; |
| 1626 | if (__lhs.index() > __rhs.index()) return false; |
Eric Fiselier | fb6a2bc | 2018-09-19 17:53:21 +0000 | [diff] [blame] | 1627 | return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | template <class... _Types> |
| 1631 | inline _LIBCPP_INLINE_VISIBILITY |
| 1632 | constexpr bool operator>(const variant<_Types...>& __lhs, |
| 1633 | const variant<_Types...>& __rhs) { |
| 1634 | using __variant_detail::__visitation::__variant; |
| 1635 | if (__lhs.valueless_by_exception()) return false; |
| 1636 | if (__rhs.valueless_by_exception()) return true; |
| 1637 | if (__lhs.index() > __rhs.index()) return true; |
| 1638 | if (__lhs.index() < __rhs.index()) return false; |
Eric Fiselier | fb6a2bc | 2018-09-19 17:53:21 +0000 | [diff] [blame] | 1639 | return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | template <class... _Types> |
| 1643 | inline _LIBCPP_INLINE_VISIBILITY |
| 1644 | constexpr bool operator<=(const variant<_Types...>& __lhs, |
| 1645 | const variant<_Types...>& __rhs) { |
| 1646 | using __variant_detail::__visitation::__variant; |
| 1647 | if (__lhs.valueless_by_exception()) return true; |
| 1648 | if (__rhs.valueless_by_exception()) return false; |
| 1649 | if (__lhs.index() < __rhs.index()) return true; |
| 1650 | if (__lhs.index() > __rhs.index()) return false; |
| 1651 | return __variant::__visit_value_at( |
Eric Fiselier | fb6a2bc | 2018-09-19 17:53:21 +0000 | [diff] [blame] | 1652 | __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | template <class... _Types> |
| 1656 | inline _LIBCPP_INLINE_VISIBILITY |
| 1657 | constexpr bool operator>=(const variant<_Types...>& __lhs, |
| 1658 | const variant<_Types...>& __rhs) { |
| 1659 | using __variant_detail::__visitation::__variant; |
| 1660 | if (__rhs.valueless_by_exception()) return true; |
| 1661 | if (__lhs.valueless_by_exception()) return false; |
| 1662 | if (__lhs.index() > __rhs.index()) return true; |
| 1663 | if (__lhs.index() < __rhs.index()) return false; |
| 1664 | return __variant::__visit_value_at( |
Eric Fiselier | fb6a2bc | 2018-09-19 17:53:21 +0000 | [diff] [blame] | 1665 | __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Ruslan Arutyunyan | 24d0eea | 2021-01-25 13:34:03 -0500 | [diff] [blame] | 1668 | template <class... _Vs> |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1669 | inline _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 1670 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void |
| 1671 | __throw_if_valueless(_Vs&&... __vs) { |
| 1672 | const bool __valueless = |
| 1673 | (... || _VSTD::__as_variant(__vs).valueless_by_exception()); |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 1674 | if (__valueless) { |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 1675 | __throw_bad_variant_access(); |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 1679 | template < |
| 1680 | class _Visitor, class... _Vs, |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1681 | typename = void_t<decltype(_VSTD::__as_variant(declval<_Vs>()))...> > |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 1682 | inline _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 1683 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr |
| 1684 | decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1685 | using __variant_detail::__visitation::__variant; |
Ruslan Arutyunyan | 24d0eea | 2021-01-25 13:34:03 -0500 | [diff] [blame] | 1686 | _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...); |
Eric Fiselier | 8799411 | 2020-11-17 17:34:23 -0500 | [diff] [blame] | 1687 | return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor), |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1688 | _VSTD::forward<_Vs>(__vs)...); |
| 1689 | } |
| 1690 | |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 1691 | #if _LIBCPP_STD_VER > 17 |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 1692 | template < |
| 1693 | class _Rp, class _Visitor, class... _Vs, |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 1694 | typename = void_t<decltype(_VSTD::__as_variant(declval<_Vs>()))...> > |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 1695 | inline _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | 2f5ec00 | 2021-03-25 18:09:11 +0100 | [diff] [blame] | 1696 | _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp |
| 1697 | visit(_Visitor&& __visitor, _Vs&&... __vs) { |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 1698 | using __variant_detail::__visitation::__variant; |
Ruslan Arutyunyan | 24d0eea | 2021-01-25 13:34:03 -0500 | [diff] [blame] | 1699 | _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...); |
Ruslan Arutyunyan | 33f3473 | 2021-01-25 11:12:25 -0500 | [diff] [blame] | 1700 | return __variant::__visit_value<_Rp>(_VSTD::forward<_Visitor>(__visitor), |
| 1701 | _VSTD::forward<_Vs>(__vs)...); |
| 1702 | } |
| 1703 | #endif |
| 1704 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1705 | struct _LIBCPP_TEMPLATE_VIS monostate {}; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1706 | |
| 1707 | inline _LIBCPP_INLINE_VISIBILITY |
| 1708 | constexpr bool operator<(monostate, monostate) noexcept { return false; } |
| 1709 | |
| 1710 | inline _LIBCPP_INLINE_VISIBILITY |
| 1711 | constexpr bool operator>(monostate, monostate) noexcept { return false; } |
| 1712 | |
| 1713 | inline _LIBCPP_INLINE_VISIBILITY |
| 1714 | constexpr bool operator<=(monostate, monostate) noexcept { return true; } |
| 1715 | |
| 1716 | inline _LIBCPP_INLINE_VISIBILITY |
| 1717 | constexpr bool operator>=(monostate, monostate) noexcept { return true; } |
| 1718 | |
| 1719 | inline _LIBCPP_INLINE_VISIBILITY |
| 1720 | constexpr bool operator==(monostate, monostate) noexcept { return true; } |
| 1721 | |
| 1722 | inline _LIBCPP_INLINE_VISIBILITY |
| 1723 | constexpr bool operator!=(monostate, monostate) noexcept { return false; } |
| 1724 | |
| 1725 | template <class... _Types> |
| 1726 | inline _LIBCPP_INLINE_VISIBILITY |
| 1727 | auto swap(variant<_Types...>& __lhs, |
| 1728 | variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs))) |
| 1729 | -> decltype(__lhs.swap(__rhs)) { |
| 1730 | __lhs.swap(__rhs); |
| 1731 | } |
| 1732 | |
| 1733 | template <class... _Types> |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 1734 | struct _LIBCPP_TEMPLATE_VIS hash< |
| 1735 | __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1736 | using argument_type = variant<_Types...>; |
| 1737 | using result_type = size_t; |
| 1738 | |
| 1739 | inline _LIBCPP_INLINE_VISIBILITY |
| 1740 | result_type operator()(const argument_type& __v) const { |
| 1741 | using __variant_detail::__visitation::__variant; |
Eric Fiselier | 9a4e350 | 2016-12-02 23:38:31 +0000 | [diff] [blame] | 1742 | size_t __res = |
| 1743 | __v.valueless_by_exception() |
Eric Fiselier | 6b1683c | 2016-12-04 21:37:37 +0000 | [diff] [blame] | 1744 | ? 299792458 // Random value chosen by the universe upon creation |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1745 | : __variant::__visit_alt( |
| 1746 | [](const auto& __alt) { |
Marshall Clow | e61ba95 | 2018-02-12 15:41:25 +0000 | [diff] [blame] | 1747 | using __alt_type = __uncvref_t<decltype(__alt)>; |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 1748 | using __value_type = remove_const_t< |
| 1749 | typename __alt_type::__value_type>; |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1750 | return hash<__value_type>{}(__alt.__value); |
| 1751 | }, |
| 1752 | __v); |
Eric Fiselier | 9a4e350 | 2016-12-02 23:38:31 +0000 | [diff] [blame] | 1753 | return __hash_combine(__res, hash<size_t>{}(__v.index())); |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1754 | } |
| 1755 | }; |
| 1756 | |
| 1757 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1758 | struct _LIBCPP_TEMPLATE_VIS hash<monostate> { |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1759 | using argument_type = monostate; |
| 1760 | using result_type = size_t; |
| 1761 | |
| 1762 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4903689 | 2017-03-23 02:40:28 +0000 | [diff] [blame] | 1763 | result_type operator()(const argument_type&) const _NOEXCEPT { |
Eric Fiselier | 6b1683c | 2016-12-04 21:37:37 +0000 | [diff] [blame] | 1764 | return 66740831; // return a fundamentally attractive random value. |
| 1765 | } |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1766 | }; |
| 1767 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1768 | #endif // _LIBCPP_STD_VER > 14 |
Eric Fiselier | 94cdacc | 2016-12-02 23:00:05 +0000 | [diff] [blame] | 1769 | |
| 1770 | _LIBCPP_END_NAMESPACE_STD |
| 1771 | |
Eric Fiselier | 8625d33 | 2017-11-19 04:57:22 +0000 | [diff] [blame] | 1772 | _LIBCPP_POP_MACROS |
| 1773 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1774 | #endif // _LIBCPP_VARIANT |