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