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