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