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