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