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