2016-12-03 07:00:05 +08:00
|
|
|
// -*- C++ -*-
|
2021-11-18 05:25:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2016-12-03 07:00:05 +08:00
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-12-03 07:00:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_VARIANT
|
|
|
|
#define _LIBCPP_VARIANT
|
|
|
|
|
|
|
|
/*
|
|
|
|
variant synopsis
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
// 20.7.2, class template variant
|
|
|
|
template <class... Types>
|
|
|
|
class variant {
|
|
|
|
public:
|
|
|
|
|
|
|
|
// 20.7.2.1, constructors
|
|
|
|
constexpr variant() noexcept(see below);
|
2019-01-11 04:06:11 +08:00
|
|
|
variant(const variant&); // constexpr in C++20
|
|
|
|
variant(variant&&) noexcept(see below); // constexpr in C++20
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class T> constexpr variant(T&&) noexcept(see below);
|
|
|
|
|
|
|
|
template <class T, class... Args>
|
|
|
|
constexpr explicit variant(in_place_type_t<T>, Args&&...);
|
|
|
|
|
|
|
|
template <class T, class U, class... Args>
|
|
|
|
constexpr explicit variant(
|
|
|
|
in_place_type_t<T>, initializer_list<U>, Args&&...);
|
|
|
|
|
|
|
|
template <size_t I, class... Args>
|
|
|
|
constexpr explicit variant(in_place_index_t<I>, Args&&...);
|
|
|
|
|
|
|
|
template <size_t I, class U, class... Args>
|
|
|
|
constexpr explicit variant(
|
|
|
|
in_place_index_t<I>, initializer_list<U>, Args&&...);
|
|
|
|
|
|
|
|
// 20.7.2.2, destructor
|
|
|
|
~variant();
|
|
|
|
|
|
|
|
// 20.7.2.3, assignment
|
2019-01-11 04:06:11 +08:00
|
|
|
variant& operator=(const variant&); // constexpr in C++20
|
|
|
|
variant& operator=(variant&&) noexcept(see below); // constexpr in C++20
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class T> variant& operator=(T&&) noexcept(see below);
|
|
|
|
|
|
|
|
// 20.7.2.4, modifiers
|
|
|
|
template <class T, class... Args>
|
2017-04-16 03:32:02 +08:00
|
|
|
T& emplace(Args&&...);
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class T, class U, class... Args>
|
2017-04-16 03:32:02 +08:00
|
|
|
T& emplace(initializer_list<U>, Args&&...);
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <size_t I, class... Args>
|
2017-04-16 03:32:02 +08:00
|
|
|
variant_alternative_t<I, variant>& emplace(Args&&...);
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <size_t I, class U, class... Args>
|
2017-04-16 03:32:02 +08:00
|
|
|
variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
// 20.7.2.5, value status
|
|
|
|
constexpr bool valueless_by_exception() const noexcept;
|
|
|
|
constexpr size_t index() const noexcept;
|
|
|
|
|
|
|
|
// 20.7.2.6, swap
|
|
|
|
void swap(variant&) noexcept(see below);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 20.7.3, variant helper classes
|
|
|
|
template <class T> struct variant_size; // undefined
|
|
|
|
|
|
|
|
template <class T>
|
2018-01-03 01:17:01 +08:00
|
|
|
inline constexpr size_t variant_size_v = variant_size<T>::value;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class T> struct variant_size<const T>;
|
|
|
|
template <class T> struct variant_size<volatile T>;
|
|
|
|
template <class T> struct variant_size<const volatile T>;
|
|
|
|
|
|
|
|
template <class... Types>
|
|
|
|
struct variant_size<variant<Types...>>;
|
|
|
|
|
|
|
|
template <size_t I, class T> struct variant_alternative; // undefined
|
|
|
|
|
|
|
|
template <size_t I, class T>
|
|
|
|
using variant_alternative_t = typename variant_alternative<I, T>::type;
|
|
|
|
|
|
|
|
template <size_t I, class T> struct variant_alternative<I, const T>;
|
|
|
|
template <size_t I, class T> struct variant_alternative<I, volatile T>;
|
|
|
|
template <size_t I, class T> struct variant_alternative<I, const volatile T>;
|
|
|
|
|
|
|
|
template <size_t I, class... Types>
|
|
|
|
struct variant_alternative<I, variant<Types...>>;
|
|
|
|
|
2018-01-03 01:17:01 +08:00
|
|
|
inline constexpr size_t variant_npos = -1;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
// 20.7.4, value access
|
|
|
|
template <class T, class... Types>
|
|
|
|
constexpr bool holds_alternative(const variant<Types...>&) noexcept;
|
|
|
|
|
|
|
|
template <size_t I, class... Types>
|
|
|
|
constexpr variant_alternative_t<I, variant<Types...>>&
|
|
|
|
get(variant<Types...>&);
|
|
|
|
|
|
|
|
template <size_t I, class... Types>
|
|
|
|
constexpr variant_alternative_t<I, variant<Types...>>&&
|
|
|
|
get(variant<Types...>&&);
|
|
|
|
|
|
|
|
template <size_t I, class... Types>
|
|
|
|
constexpr variant_alternative_t<I, variant<Types...>> const&
|
|
|
|
get(const variant<Types...>&);
|
|
|
|
|
|
|
|
template <size_t I, class... Types>
|
|
|
|
constexpr variant_alternative_t<I, variant<Types...>> const&&
|
|
|
|
get(const variant<Types...>&&);
|
|
|
|
|
|
|
|
template <class T, class... Types>
|
|
|
|
constexpr T& get(variant<Types...>&);
|
|
|
|
|
|
|
|
template <class T, class... Types>
|
|
|
|
constexpr T&& get(variant<Types...>&&);
|
|
|
|
|
|
|
|
template <class T, class... Types>
|
|
|
|
constexpr const T& get(const variant<Types...>&);
|
|
|
|
|
|
|
|
template <class T, class... Types>
|
|
|
|
constexpr const T&& get(const variant<Types...>&&);
|
|
|
|
|
|
|
|
template <size_t I, class... Types>
|
|
|
|
constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
|
|
|
|
get_if(variant<Types...>*) noexcept;
|
|
|
|
|
|
|
|
template <size_t I, class... Types>
|
|
|
|
constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
|
|
|
|
get_if(const variant<Types...>*) noexcept;
|
|
|
|
|
|
|
|
template <class T, class... Types>
|
|
|
|
constexpr add_pointer_t<T>
|
|
|
|
get_if(variant<Types...>*) noexcept;
|
|
|
|
|
|
|
|
template <class T, class... Types>
|
|
|
|
constexpr add_pointer_t<const T>
|
|
|
|
get_if(const variant<Types...>*) noexcept;
|
|
|
|
|
|
|
|
// 20.7.5, relational operators
|
|
|
|
template <class... Types>
|
|
|
|
constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
|
|
|
|
|
|
|
|
template <class... Types>
|
|
|
|
constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
|
|
|
|
|
|
|
|
template <class... Types>
|
|
|
|
constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
|
|
|
|
|
|
|
|
template <class... Types>
|
|
|
|
constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
|
|
|
|
|
|
|
|
template <class... Types>
|
|
|
|
constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
|
|
|
|
|
|
|
|
template <class... Types>
|
|
|
|
constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
|
|
|
|
|
|
|
|
// 20.7.6, visitation
|
|
|
|
template <class Visitor, class... Variants>
|
|
|
|
constexpr see below visit(Visitor&&, Variants&&...);
|
|
|
|
|
2021-01-26 00:12:25 +08:00
|
|
|
template <class R, class Visitor, class... Variants>
|
|
|
|
constexpr R visit(Visitor&&, Variants&&...); // since C++20
|
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
// 20.7.7, class monostate
|
|
|
|
struct monostate;
|
|
|
|
|
|
|
|
// 20.7.8, monostate relational operators
|
|
|
|
constexpr bool operator<(monostate, monostate) noexcept;
|
|
|
|
constexpr bool operator>(monostate, monostate) noexcept;
|
|
|
|
constexpr bool operator<=(monostate, monostate) noexcept;
|
|
|
|
constexpr bool operator>=(monostate, monostate) noexcept;
|
|
|
|
constexpr bool operator==(monostate, monostate) noexcept;
|
|
|
|
constexpr bool operator!=(monostate, monostate) noexcept;
|
|
|
|
|
|
|
|
// 20.7.9, specialized algorithms
|
|
|
|
template <class... Types>
|
|
|
|
void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
|
|
|
|
|
|
|
|
// 20.7.10, class bad_variant_access
|
|
|
|
class bad_variant_access;
|
|
|
|
|
|
|
|
// 20.7.11, hash support
|
|
|
|
template <class T> struct hash;
|
|
|
|
template <class... Types> struct hash<variant<Types...>>;
|
|
|
|
template <> struct hash<monostate>;
|
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-03-26 00:55:36 +08:00
|
|
|
#include <__assert> // all public C++ headers provide the assertion handler
|
2020-11-05 04:01:25 +08:00
|
|
|
#include <__availability>
|
2021-05-19 23:57:04 +08:00
|
|
|
#include <__config>
|
2021-05-28 00:23:19 +08:00
|
|
|
#include <__functional/hash.h>
|
2022-02-12 02:15:18 +08:00
|
|
|
#include <__functional/operations.h>
|
|
|
|
#include <__functional/unary_function.h>
|
2021-05-28 00:23:19 +08:00
|
|
|
#include <__tuple>
|
2021-06-05 10:47:47 +08:00
|
|
|
#include <__utility/forward.h>
|
2022-03-06 02:17:07 +08:00
|
|
|
#include <__utility/in_place.h>
|
|
|
|
#include <__utility/move.h>
|
|
|
|
#include <__utility/swap.h>
|
2021-07-08 03:27:27 +08:00
|
|
|
#include <__variant/monostate.h>
|
2016-12-03 07:00:05 +08:00
|
|
|
#include <exception>
|
|
|
|
#include <initializer_list>
|
2021-05-19 23:57:04 +08:00
|
|
|
#include <limits>
|
2016-12-03 07:00:05 +08:00
|
|
|
#include <new>
|
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
2018-09-13 03:41:40 +08:00
|
|
|
#include <version>
|
2016-12-03 07:00:05 +08:00
|
|
|
|
[libc++] Re-add transitive includes that had been removed since LLVM 14
This commit re-adds transitive includes that had been removed by
4cd04d1687f1, c36870c8e79c, a83f4b9cda57, 1458458b558d, 2e2f3158c604,
and 489637e66dd3. This should cover almost all the includes that had
been removed since LLVM 14 and that would contribute to breaking user
code when releasing LLVM 15.
It is possible to disable the inclusion of these headers by defining
_LIBCPP_REMOVE_TRANSITIVE_INCLUDES. The intent is that vendors will
enable that macro and start fixing downstream issues immediately. We
can then remove the macro (and the transitive includes) by default in
a future release. That way, we will break users only once by removing
transitive includes in bulk instead of doing it bit by bit a every
release, which is more disruptive for users.
Note 1: The set of headers to re-add was found by re-generating the
transitive include test on a checkout of release/14.x, which
provided the list of all transitive includes we used to provide.
Note 2: Several includes of <vector>, <optional>, <array> and <unordered_map>
have been added in this commit. These transitive inclusions were
added when we implemented boyer_moore_searcher in <functional>.
Note 3: This is a best effort patch to try and resolve downstream breakage
caused since branching LLVM 14. I wasn't able to perfectly mirror
transitive includes in LLVM 14 for a few headers, so I added a
release note explaining it. To summarize, adding boyer_moore_searcher
created a bunch of circular dependencies, so we have to break
backwards compatibility in a few cases.
Differential Revision: https://reviews.llvm.org/D128661
2022-06-28 03:53:41 +08:00
|
|
|
#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
|
|
|
|
# include <typeinfo>
|
|
|
|
# include <utility>
|
|
|
|
#endif
|
|
|
|
|
2022-06-17 04:43:46 +08:00
|
|
|
// standard-mandated includes
|
|
|
|
#include <compare>
|
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 09:16:40 +08:00
|
|
|
# pragma GCC system_header
|
2016-12-03 07:00:05 +08:00
|
|
|
#endif
|
|
|
|
|
2017-11-19 12:57:22 +08:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
namespace std { // explicitly not using versioning namespace
|
|
|
|
|
2018-11-19 23:37:04 +08:00
|
|
|
class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
|
2016-12-03 07:00:05 +08:00
|
|
|
public:
|
2017-01-01 01:34:26 +08:00
|
|
|
virtual const char* what() const _NOEXCEPT;
|
2016-12-03 07:00:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2021-08-31 22:49:06 +08:00
|
|
|
#if _LIBCPP_STD_VER > 14
|
2016-12-03 07:00:05 +08:00
|
|
|
|
2021-07-08 06:25:53 +08:00
|
|
|
// Light N-dimensional array of function pointers. Used in place of std::array to avoid
|
|
|
|
// adding a dependency.
|
|
|
|
template<class _Tp, size_t _Size>
|
|
|
|
struct __farray {
|
|
|
|
static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
|
|
|
|
_Tp __buf_[_Size] = {};
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
|
|
|
const _Tp &operator[](size_t __n) const noexcept {
|
|
|
|
return __buf_[__n];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-03 09:58:07 +08:00
|
|
|
_LIBCPP_NORETURN
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 09:58:07 +08:00
|
|
|
void __throw_bad_variant_access() {
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
throw bad_variant_access();
|
|
|
|
#else
|
|
|
|
_VSTD::abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
template <class... _Types>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS variant;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_size;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
2021-09-22 21:35:32 +08:00
|
|
|
inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
|
2016-12-03 07:00:05 +08:00
|
|
|
: variant_size<_Tp> {};
|
|
|
|
|
|
|
|
template <class... _Types>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
|
2016-12-03 07:00:05 +08:00
|
|
|
: integral_constant<size_t, sizeof...(_Types)> {};
|
|
|
|
|
|
|
|
template <size_t _Ip, class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_alternative;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <size_t _Ip, class _Tp>
|
|
|
|
using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
|
|
|
|
|
|
|
|
template <size_t _Ip, class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
|
2016-12-03 07:00:05 +08:00
|
|
|
: add_const<variant_alternative_t<_Ip, _Tp>> {};
|
|
|
|
|
|
|
|
template <size_t _Ip, class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
|
2016-12-03 07:00:05 +08:00
|
|
|
: add_volatile<variant_alternative_t<_Ip, _Tp>> {};
|
|
|
|
|
|
|
|
template <size_t _Ip, class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
|
2016-12-03 07:00:05 +08:00
|
|
|
: add_cv<variant_alternative_t<_Ip, _Tp>> {};
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Types>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
|
2017-06-13 00:13:17 +08:00
|
|
|
static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
|
2016-12-03 07:00:05 +08:00
|
|
|
using type = __type_pack_element<_Ip, _Types...>;
|
|
|
|
};
|
|
|
|
|
2021-09-22 21:35:32 +08:00
|
|
|
inline constexpr size_t variant_npos = static_cast<size_t>(-1);
|
2017-11-19 12:19:44 +08:00
|
|
|
|
|
|
|
constexpr int __choose_index_type(unsigned int __num_elem) {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-28 00:02:06 +08:00
|
|
|
if (__num_elem < numeric_limits<unsigned char>::max())
|
2017-11-19 12:19:44 +08:00
|
|
|
return 0;
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-28 00:02:06 +08:00
|
|
|
if (__num_elem < numeric_limits<unsigned short>::max())
|
2017-11-19 12:19:44 +08:00
|
|
|
return 1;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _NumAlts>
|
|
|
|
using __variant_index_t =
|
|
|
|
#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
|
|
|
|
unsigned int;
|
|
|
|
#else
|
|
|
|
std::tuple_element_t<
|
|
|
|
__choose_index_type(_NumAlts),
|
|
|
|
std::tuple<unsigned char, unsigned short, unsigned int>
|
|
|
|
>;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class _IndexType>
|
|
|
|
constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
|
2016-12-03 07:00:05 +08:00
|
|
|
|
2021-03-26 01:09:11 +08:00
|
|
|
template <class... _Types>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS variant;
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&
|
|
|
|
__as_variant(variant<_Types...>& __vs) noexcept {
|
|
|
|
return __vs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&
|
|
|
|
__as_variant(const variant<_Types...>& __vs) noexcept {
|
|
|
|
return __vs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&&
|
|
|
|
__as_variant(variant<_Types...>&& __vs) noexcept {
|
|
|
|
return _VSTD::move(__vs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&&
|
|
|
|
__as_variant(const variant<_Types...>&& __vs) noexcept {
|
|
|
|
return _VSTD::move(__vs);
|
|
|
|
}
|
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
namespace __find_detail {
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr size_t __find_index() {
|
|
|
|
constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
|
|
|
|
size_t __result = __not_found;
|
|
|
|
for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
|
|
|
|
if (__matches[__i]) {
|
|
|
|
if (__result != __not_found) {
|
|
|
|
return __ambiguous;
|
|
|
|
}
|
|
|
|
__result = __i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return __result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Index>
|
|
|
|
struct __find_unambiguous_index_sfinae_impl
|
|
|
|
: integral_constant<size_t, _Index> {};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct __find_unambiguous_index_sfinae_impl<__not_found> {};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
struct __find_unambiguous_index_sfinae
|
|
|
|
: __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
|
|
|
|
|
|
|
|
} // namespace __find_detail
|
|
|
|
|
|
|
|
namespace __variant_detail {
|
|
|
|
|
|
|
|
struct __valueless_t {};
|
|
|
|
|
|
|
|
enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <typename _Tp,
|
|
|
|
template <typename> class _IsTriviallyAvailable,
|
|
|
|
template <typename> class _IsAvailable>
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr _Trait __trait =
|
|
|
|
_IsTriviallyAvailable<_Tp>::value
|
|
|
|
? _Trait::_TriviallyAvailable
|
|
|
|
: _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
|
|
|
|
_Trait __result = _Trait::_TriviallyAvailable;
|
|
|
|
for (_Trait __t : __traits) {
|
|
|
|
if (static_cast<int>(__t) > static_cast<int>(__result)) {
|
|
|
|
__result = __t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return __result;
|
|
|
|
}
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <typename... _Types>
|
2016-12-03 07:00:05 +08:00
|
|
|
struct __traits {
|
|
|
|
static constexpr _Trait __copy_constructible_trait =
|
|
|
|
__common_trait({__trait<_Types,
|
|
|
|
is_trivially_copy_constructible,
|
|
|
|
is_copy_constructible>...});
|
|
|
|
|
|
|
|
static constexpr _Trait __move_constructible_trait =
|
|
|
|
__common_trait({__trait<_Types,
|
|
|
|
is_trivially_move_constructible,
|
|
|
|
is_move_constructible>...});
|
|
|
|
|
|
|
|
static constexpr _Trait __copy_assignable_trait = __common_trait(
|
|
|
|
{__copy_constructible_trait,
|
|
|
|
__trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
|
|
|
|
|
|
|
|
static constexpr _Trait __move_assignable_trait = __common_trait(
|
|
|
|
{__move_constructible_trait,
|
|
|
|
__trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
|
|
|
|
|
|
|
|
static constexpr _Trait __destructible_trait = __common_trait(
|
|
|
|
{__trait<_Types, is_trivially_destructible, is_destructible>...});
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace __access {
|
|
|
|
|
|
|
|
struct __union {
|
|
|
|
template <class _Vp>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
|
|
|
|
return _VSTD::forward<_Vp>(__v).__head;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Vp, size_t _Ip>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
|
|
|
|
return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct __base {
|
|
|
|
template <size_t _Ip, class _Vp>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto&& __get_alt(_Vp&& __v) {
|
|
|
|
return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
|
|
|
|
in_place_index<_Ip>);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct __variant {
|
|
|
|
template <size_t _Ip, class _Vp>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto&& __get_alt(_Vp&& __v) {
|
|
|
|
return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace __access
|
|
|
|
|
|
|
|
namespace __visitation {
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
struct __base {
|
|
|
|
template <class _Visitor, class... _Vs>
|
2020-09-02 10:15:33 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-11-18 06:34:23 +08:00
|
|
|
static constexpr decltype(auto)
|
|
|
|
__visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
|
|
|
|
constexpr auto __fdiagonal =
|
|
|
|
__make_fdiagonal<_Visitor&&,
|
|
|
|
decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
|
|
|
|
return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
|
|
|
|
_VSTD::forward<_Vs>(__vs).__as_base()...);
|
|
|
|
}
|
2016-12-03 07:00:05 +08:00
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Visitor, class... _Vs>
|
2020-10-27 00:34:22 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-11-18 06:34:23 +08:00
|
|
|
static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
|
|
|
|
_Vs&&... __vs) {
|
|
|
|
constexpr auto __fmatrix =
|
|
|
|
__make_fmatrix<_Visitor&&,
|
|
|
|
decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
|
|
|
|
return __at(__fmatrix, __vs.index()...)(
|
|
|
|
_VSTD::forward<_Visitor>(__visitor),
|
|
|
|
_VSTD::forward<_Vs>(__vs).__as_base()...);
|
2020-09-02 10:15:33 +08:00
|
|
|
}
|
[libcxx/variant] Introduce `switch`-based mechanism for `std::visit`.
This patch introduces mechanism for `std::visit` backed by `switch`.
The `switch` is structured such that it's a flattened manual vtable (an n-ary array).
The `switch` mechanism is enabled if `(1 * ... * vs.size()) < 1024`.
The following are performance numbers from the benchmarks added in D85419, tested on my 2017 Macbook Pro.
```
$ ./projects/libcxx/benchmarks/variant_visit_1.libcxx.out
2020-08-09 23:55:14
Running ./projects/libcxx/benchmarks/variant_visit_1.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 2.03, 2.36, 2.43
------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------
BM_Visit<1, 1> 0.260 ns 0.260 ns 1000000000
BM_Visit<1, 2> 1.56 ns 1.56 ns 435925220
BM_Visit<1, 3> 1.55 ns 1.55 ns 444416228
BM_Visit<1, 4> 1.57 ns 1.57 ns 427951336
BM_Visit<1, 5> 1.57 ns 1.56 ns 444766371
BM_Visit<1, 6> 1.70 ns 1.68 ns 446639358
BM_Visit<1, 7> 1.64 ns 1.64 ns 400441630
BM_Visit<1, 8> 1.56 ns 1.56 ns 430729471
BM_Visit<1, 9> 1.58 ns 1.58 ns 449894596
BM_Visit<1, 10> 1.54 ns 1.54 ns 449660506
BM_Visit<1, 20> 1.56 ns 1.56 ns 450813074
BM_Visit<1, 30> 1.59 ns 1.59 ns 440032940
BM_Visit<1, 40> 1.59 ns 1.59 ns 443731656
BM_Visit<1, 50> 1.56 ns 1.56 ns 444709859
BM_Visit<1, 60> 1.59 ns 1.58 ns 439527320
BM_Visit<1, 70> 1.57 ns 1.57 ns 438450890
BM_Visit<1, 80> 1.58 ns 1.58 ns 443001525
BM_Visit<1, 90> 1.63 ns 1.62 ns 448456349
BM_Visit<1, 100> 1.57 ns 1.57 ns 445740630
$ ./projects/libcxx/benchmarks/variant_visit_2.libcxx.out
2020-08-09 23:59:35
Running ./projects/libcxx/benchmarks/variant_visit_2.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 1.40, 1.94, 2.22
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
BM_Visit<2, 1> 0.261 ns 0.260 ns 1000000000
BM_Visit<2, 2> 1.55 ns 1.54 ns 432844219
BM_Visit<2, 3> 1.30 ns 1.30 ns 532529974
BM_Visit<2, 4> 1.54 ns 1.54 ns 446055910
BM_Visit<2, 5> 1.31 ns 1.31 ns 531099680
BM_Visit<2, 6> 1.56 ns 1.56 ns 443203475
BM_Visit<2, 7> 1.29 ns 1.29 ns 526478087
BM_Visit<2, 8> 1.56 ns 1.56 ns 439000834
BM_Visit<2, 9> 1.30 ns 1.30 ns 528756817
BM_Visit<2, 10> 1.56 ns 1.55 ns 442923039
BM_Visit<2, 20> 1.35 ns 1.35 ns 517021072
BM_Visit<2, 30> 1.60 ns 1.59 ns 419724661
BM_Visit<2, 40> 1.45 ns 1.44 ns 472137163
BM_Visit<2, 50> 1.65 ns 1.65 ns 421389743
$ ./projects/libcxx/benchmarks/variant_visit_3.libcxx.out
2020-08-10 00:01:32
Running ./projects/libcxx/benchmarks/variant_visit_3.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 2.20, 2.01, 2.21
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
BM_Visit<3, 1> 0.272 ns 0.271 ns 1000000000
BM_Visit<3, 2> 1.87 ns 1.86 ns 361858090
BM_Visit<3, 3> 1.77 ns 1.77 ns 391192579
BM_Visit<3, 4> 1.84 ns 1.84 ns 374694223
BM_Visit<3, 5> 1.75 ns 1.75 ns 408270392
BM_Visit<3, 6> 1.88 ns 1.88 ns 378759185
BM_Visit<3, 7> 1.79 ns 1.79 ns 395498102
BM_Visit<3, 8> 1.85 ns 1.85 ns 371660366
BM_Visit<3, 9> 1.80 ns 1.80 ns 386872851
BM_Visit<3, 10> 1.84 ns 1.84 ns 362367606
BM_Visit<3, 15> 1.77 ns 1.77 ns 392060220
BM_Visit<3, 20> 1.85 ns 1.85 ns 379157188
```
```
$ ./projects/libcxx/benchmarks/variant_visit_1.libcxx.out
2020-08-10 00:05:57
Running ./projects/libcxx/benchmarks/variant_visit_1.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 2.27, 2.36, 2.34
------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------
BM_Visit<1, 1> 0.271 ns 0.271 ns 1000000000
BM_Visit<1, 2> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 3> 0.271 ns 0.271 ns 1000000000
BM_Visit<1, 4> 0.270 ns 0.270 ns 1000000000
BM_Visit<1, 5> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 6> 0.270 ns 0.269 ns 1000000000
BM_Visit<1, 7> 0.265 ns 0.265 ns 1000000000
BM_Visit<1, 8> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 9> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 10> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 20> 0.267 ns 0.267 ns 1000000000
BM_Visit<1, 30> 0.272 ns 0.272 ns 1000000000
BM_Visit<1, 40> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 50> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 60> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 70> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 80> 0.266 ns 0.266 ns 1000000000
BM_Visit<1, 90> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 100> 0.267 ns 0.267 ns 1000000000
$ ./projects/libcxx/benchmarks/variant_visit_2.libcxx.out
2020-08-12 04:09:59
Running ./projects/libcxx/benchmarks/variant_visit_2.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 2.17, 4.20, 4.78
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
BM_Visit<2, 1> 0.302 ns 0.301 ns 1000000000
BM_Visit<2, 2> 0.297 ns 0.295 ns 1000000000
BM_Visit<2, 3> 0.353 ns 0.351 ns 1000000000
BM_Visit<2, 4> 0.276 ns 0.276 ns 1000000000
BM_Visit<2, 5> 0.285 ns 0.283 ns 1000000000
BM_Visit<2, 6> 0.290 ns 0.287 ns 1000000000
BM_Visit<2, 7> 0.282 ns 0.280 ns 1000000000
BM_Visit<2, 8> 0.290 ns 0.287 ns 1000000000
BM_Visit<2, 9> 0.291 ns 0.285 ns 1000000000
BM_Visit<2, 10> 0.293 ns 0.287 ns 1000000000
BM_Visit<2, 20> 1.70 ns 1.68 ns 391400375
BM_Visit<2, 30> 1.64 ns 1.63 ns 418925874
BM_Visit<2, 40> 1.63 ns 1.62 ns 423623677
BM_Visit<2, 50> 1.68 ns 1.67 ns 411687212
$ ./projects/libcxx/benchmarks/variant_visit_3.libcxx.out
2020-08-12 04:10:43
Running ./projects/libcxx/benchmarks/variant_visit_3.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 1.57, 3.76, 4.59
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
BM_Visit<3, 1> 0.271 ns 0.270 ns 1000000000
BM_Visit<3, 2> 0.344 ns 0.334 ns 1000000000
BM_Visit<3, 3> 0.347 ns 0.336 ns 1000000000
BM_Visit<3, 4> 0.300 ns 0.296 ns 1000000000
BM_Visit<3, 5> 0.290 ns 0.286 ns 1000000000
BM_Visit<3, 6> 0.272 ns 0.271 ns 1000000000
BM_Visit<3, 7> 1.72 ns 1.71 ns 415765841
BM_Visit<3, 8> 1.73 ns 1.72 ns 408909555
BM_Visit<3, 9> 2.16 ns 2.04 ns 380898485
BM_Visit<3, 10> 2.45 ns 2.40 ns 295714256
BM_Visit<3, 15> 1.92 ns 1.85 ns 375990332
BM_Visit<3, 20> 1.66 ns 1.65 ns 414456233
```
Differential Revision: https://reviews.llvm.org/D85420
2020-08-12 06:52:49 +08:00
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
private:
|
|
|
|
template <class _Tp>
|
2020-09-02 10:15:33 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-11-18 06:34:23 +08:00
|
|
|
static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
|
2020-09-02 10:15:33 +08:00
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Tp, size_t _Np, typename... _Indices>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2021-07-08 06:25:53 +08:00
|
|
|
static constexpr auto&& __at(const __farray<_Tp, _Np>& __elems,
|
2020-11-18 06:34:23 +08:00
|
|
|
size_t __index, _Indices... __indices) {
|
|
|
|
return __at(__elems[__index], __indices...);
|
|
|
|
}
|
2020-10-27 00:34:22 +08:00
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Fp, class... _Fs>
|
|
|
|
static constexpr void __std_visit_visitor_return_type_check() {
|
|
|
|
static_assert(
|
|
|
|
__all<is_same_v<_Fp, _Fs>...>::value,
|
|
|
|
"`std::visit` requires the visitor to have a single return type.");
|
|
|
|
}
|
2020-10-27 00:34:22 +08:00
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class... _Fs>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto __make_farray(_Fs&&... __fs) {
|
|
|
|
__std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
|
2021-07-08 06:25:53 +08:00
|
|
|
using __result = __farray<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
|
2020-11-18 06:34:23 +08:00
|
|
|
return __result{{_VSTD::forward<_Fs>(__fs)...}};
|
|
|
|
}
|
|
|
|
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-28 00:02:06 +08:00
|
|
|
template <size_t... _Is>
|
2020-11-18 06:34:23 +08:00
|
|
|
struct __dispatcher {
|
|
|
|
template <class _Fp, class... _Vs>
|
2020-08-31 00:42:35 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-11-18 06:34:23 +08:00
|
|
|
static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
|
2022-04-07 05:10:21 +08:00
|
|
|
return _VSTD::__invoke(
|
2020-11-18 06:34:23 +08:00
|
|
|
static_cast<_Fp>(__f),
|
|
|
|
__access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
|
2020-08-31 00:42:35 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Fp, class... _Vs, size_t... _Is>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto __make_dispatch(index_sequence<_Is...>) {
|
|
|
|
return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
2020-11-18 06:34:23 +08:00
|
|
|
|
|
|
|
template <size_t _Ip, class _Fp, class... _Vs>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto __make_fdiagonal_impl() {
|
|
|
|
return __make_dispatch<_Fp, _Vs...>(
|
2022-03-19 00:49:02 +08:00
|
|
|
index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
|
[libcxx/variant] Introduce `switch`-based mechanism for `std::visit`.
This patch introduces mechanism for `std::visit` backed by `switch`.
The `switch` is structured such that it's a flattened manual vtable (an n-ary array).
The `switch` mechanism is enabled if `(1 * ... * vs.size()) < 1024`.
The following are performance numbers from the benchmarks added in D85419, tested on my 2017 Macbook Pro.
```
$ ./projects/libcxx/benchmarks/variant_visit_1.libcxx.out
2020-08-09 23:55:14
Running ./projects/libcxx/benchmarks/variant_visit_1.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 2.03, 2.36, 2.43
------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------
BM_Visit<1, 1> 0.260 ns 0.260 ns 1000000000
BM_Visit<1, 2> 1.56 ns 1.56 ns 435925220
BM_Visit<1, 3> 1.55 ns 1.55 ns 444416228
BM_Visit<1, 4> 1.57 ns 1.57 ns 427951336
BM_Visit<1, 5> 1.57 ns 1.56 ns 444766371
BM_Visit<1, 6> 1.70 ns 1.68 ns 446639358
BM_Visit<1, 7> 1.64 ns 1.64 ns 400441630
BM_Visit<1, 8> 1.56 ns 1.56 ns 430729471
BM_Visit<1, 9> 1.58 ns 1.58 ns 449894596
BM_Visit<1, 10> 1.54 ns 1.54 ns 449660506
BM_Visit<1, 20> 1.56 ns 1.56 ns 450813074
BM_Visit<1, 30> 1.59 ns 1.59 ns 440032940
BM_Visit<1, 40> 1.59 ns 1.59 ns 443731656
BM_Visit<1, 50> 1.56 ns 1.56 ns 444709859
BM_Visit<1, 60> 1.59 ns 1.58 ns 439527320
BM_Visit<1, 70> 1.57 ns 1.57 ns 438450890
BM_Visit<1, 80> 1.58 ns 1.58 ns 443001525
BM_Visit<1, 90> 1.63 ns 1.62 ns 448456349
BM_Visit<1, 100> 1.57 ns 1.57 ns 445740630
$ ./projects/libcxx/benchmarks/variant_visit_2.libcxx.out
2020-08-09 23:59:35
Running ./projects/libcxx/benchmarks/variant_visit_2.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 1.40, 1.94, 2.22
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
BM_Visit<2, 1> 0.261 ns 0.260 ns 1000000000
BM_Visit<2, 2> 1.55 ns 1.54 ns 432844219
BM_Visit<2, 3> 1.30 ns 1.30 ns 532529974
BM_Visit<2, 4> 1.54 ns 1.54 ns 446055910
BM_Visit<2, 5> 1.31 ns 1.31 ns 531099680
BM_Visit<2, 6> 1.56 ns 1.56 ns 443203475
BM_Visit<2, 7> 1.29 ns 1.29 ns 526478087
BM_Visit<2, 8> 1.56 ns 1.56 ns 439000834
BM_Visit<2, 9> 1.30 ns 1.30 ns 528756817
BM_Visit<2, 10> 1.56 ns 1.55 ns 442923039
BM_Visit<2, 20> 1.35 ns 1.35 ns 517021072
BM_Visit<2, 30> 1.60 ns 1.59 ns 419724661
BM_Visit<2, 40> 1.45 ns 1.44 ns 472137163
BM_Visit<2, 50> 1.65 ns 1.65 ns 421389743
$ ./projects/libcxx/benchmarks/variant_visit_3.libcxx.out
2020-08-10 00:01:32
Running ./projects/libcxx/benchmarks/variant_visit_3.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 2.20, 2.01, 2.21
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
BM_Visit<3, 1> 0.272 ns 0.271 ns 1000000000
BM_Visit<3, 2> 1.87 ns 1.86 ns 361858090
BM_Visit<3, 3> 1.77 ns 1.77 ns 391192579
BM_Visit<3, 4> 1.84 ns 1.84 ns 374694223
BM_Visit<3, 5> 1.75 ns 1.75 ns 408270392
BM_Visit<3, 6> 1.88 ns 1.88 ns 378759185
BM_Visit<3, 7> 1.79 ns 1.79 ns 395498102
BM_Visit<3, 8> 1.85 ns 1.85 ns 371660366
BM_Visit<3, 9> 1.80 ns 1.80 ns 386872851
BM_Visit<3, 10> 1.84 ns 1.84 ns 362367606
BM_Visit<3, 15> 1.77 ns 1.77 ns 392060220
BM_Visit<3, 20> 1.85 ns 1.85 ns 379157188
```
```
$ ./projects/libcxx/benchmarks/variant_visit_1.libcxx.out
2020-08-10 00:05:57
Running ./projects/libcxx/benchmarks/variant_visit_1.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 2.27, 2.36, 2.34
------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------
BM_Visit<1, 1> 0.271 ns 0.271 ns 1000000000
BM_Visit<1, 2> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 3> 0.271 ns 0.271 ns 1000000000
BM_Visit<1, 4> 0.270 ns 0.270 ns 1000000000
BM_Visit<1, 5> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 6> 0.270 ns 0.269 ns 1000000000
BM_Visit<1, 7> 0.265 ns 0.265 ns 1000000000
BM_Visit<1, 8> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 9> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 10> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 20> 0.267 ns 0.267 ns 1000000000
BM_Visit<1, 30> 0.272 ns 0.272 ns 1000000000
BM_Visit<1, 40> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 50> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 60> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 70> 0.269 ns 0.269 ns 1000000000
BM_Visit<1, 80> 0.266 ns 0.266 ns 1000000000
BM_Visit<1, 90> 0.268 ns 0.268 ns 1000000000
BM_Visit<1, 100> 0.267 ns 0.267 ns 1000000000
$ ./projects/libcxx/benchmarks/variant_visit_2.libcxx.out
2020-08-12 04:09:59
Running ./projects/libcxx/benchmarks/variant_visit_2.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 2.17, 4.20, 4.78
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
BM_Visit<2, 1> 0.302 ns 0.301 ns 1000000000
BM_Visit<2, 2> 0.297 ns 0.295 ns 1000000000
BM_Visit<2, 3> 0.353 ns 0.351 ns 1000000000
BM_Visit<2, 4> 0.276 ns 0.276 ns 1000000000
BM_Visit<2, 5> 0.285 ns 0.283 ns 1000000000
BM_Visit<2, 6> 0.290 ns 0.287 ns 1000000000
BM_Visit<2, 7> 0.282 ns 0.280 ns 1000000000
BM_Visit<2, 8> 0.290 ns 0.287 ns 1000000000
BM_Visit<2, 9> 0.291 ns 0.285 ns 1000000000
BM_Visit<2, 10> 0.293 ns 0.287 ns 1000000000
BM_Visit<2, 20> 1.70 ns 1.68 ns 391400375
BM_Visit<2, 30> 1.64 ns 1.63 ns 418925874
BM_Visit<2, 40> 1.63 ns 1.62 ns 423623677
BM_Visit<2, 50> 1.68 ns 1.67 ns 411687212
$ ./projects/libcxx/benchmarks/variant_visit_3.libcxx.out
2020-08-12 04:10:43
Running ./projects/libcxx/benchmarks/variant_visit_3.libcxx.out
Run on (8 X 3100 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Load Average: 1.57, 3.76, 4.59
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
BM_Visit<3, 1> 0.271 ns 0.270 ns 1000000000
BM_Visit<3, 2> 0.344 ns 0.334 ns 1000000000
BM_Visit<3, 3> 0.347 ns 0.336 ns 1000000000
BM_Visit<3, 4> 0.300 ns 0.296 ns 1000000000
BM_Visit<3, 5> 0.290 ns 0.286 ns 1000000000
BM_Visit<3, 6> 0.272 ns 0.271 ns 1000000000
BM_Visit<3, 7> 1.72 ns 1.71 ns 415765841
BM_Visit<3, 8> 1.73 ns 1.72 ns 408909555
BM_Visit<3, 9> 2.16 ns 2.04 ns 380898485
BM_Visit<3, 10> 2.45 ns 2.40 ns 295714256
BM_Visit<3, 15> 1.92 ns 1.85 ns 375990332
BM_Visit<3, 20> 1.66 ns 1.65 ns 414456233
```
Differential Revision: https://reviews.llvm.org/D85420
2020-08-12 06:52:49 +08:00
|
|
|
}
|
2020-09-02 10:15:33 +08:00
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Fp, class... _Vs, size_t... _Is>
|
2020-09-02 10:15:33 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-11-18 06:34:23 +08:00
|
|
|
static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
|
|
|
|
return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
|
2020-09-02 10:15:33 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Fp, class _Vp, class... _Vs>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto __make_fdiagonal() {
|
|
|
|
constexpr size_t _Np = __uncvref_t<_Vp>::__size();
|
|
|
|
static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
|
|
|
|
return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
|
2020-09-02 10:15:33 +08:00
|
|
|
}
|
2020-11-18 06:34:23 +08:00
|
|
|
|
|
|
|
template <class _Fp, class... _Vs, size_t... _Is>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
|
|
|
|
return __make_dispatch<_Fp, _Vs...>(__is);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
|
|
|
|
index_sequence<_Js...>,
|
|
|
|
_Ls... __ls) {
|
|
|
|
return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
|
|
|
|
index_sequence<_Is..., _Js>{}, __ls...)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Fp, class... _Vs>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto __make_fmatrix() {
|
|
|
|
return __make_fmatrix_impl<_Fp, _Vs...>(
|
|
|
|
index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct __variant {
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Visitor, class... _Vs>
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr decltype(auto)
|
2020-11-18 06:34:23 +08:00
|
|
|
__visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
|
2016-12-03 07:00:05 +08:00
|
|
|
return __base::__visit_alt_at(__index,
|
2020-11-18 06:34:23 +08:00
|
|
|
_VSTD::forward<_Visitor>(__visitor),
|
|
|
|
_VSTD::forward<_Vs>(__vs).__impl...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Visitor, class... _Vs>
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-11-18 06:34:23 +08:00
|
|
|
static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
|
|
|
|
_Vs&&... __vs) {
|
2021-03-26 01:09:11 +08:00
|
|
|
return __base::__visit_alt(
|
|
|
|
_VSTD::forward<_Visitor>(__visitor),
|
|
|
|
_VSTD::__as_variant(_VSTD::forward<_Vs>(__vs)).__impl...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Visitor, class... _Vs>
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr decltype(auto)
|
2020-11-18 06:34:23 +08:00
|
|
|
__visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
|
|
|
|
return __visit_alt_at(
|
|
|
|
__index,
|
|
|
|
__make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
|
|
|
|
_VSTD::forward<_Vs>(__vs)...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Visitor, class... _Vs>
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-11-18 06:34:23 +08:00
|
|
|
static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
|
|
|
|
_Vs&&... __vs) {
|
|
|
|
return __visit_alt(
|
|
|
|
__make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
|
|
|
|
_VSTD::forward<_Vs>(__vs)...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
2021-03-26 01:09:11 +08:00
|
|
|
|
2021-01-26 00:12:25 +08:00
|
|
|
#if _LIBCPP_STD_VER > 17
|
|
|
|
template <class _Rp, class _Visitor, class... _Vs>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr _Rp __visit_value(_Visitor&& __visitor,
|
|
|
|
_Vs&&... __vs) {
|
|
|
|
return __visit_alt(
|
|
|
|
__make_value_visitor<_Rp>(_VSTD::forward<_Visitor>(__visitor)),
|
|
|
|
_VSTD::forward<_Vs>(__vs)...);
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
private:
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Visitor, class... _Values>
|
2016-12-03 07:00:05 +08:00
|
|
|
static constexpr void __std_visit_exhaustive_visitor_check() {
|
2020-11-18 06:34:23 +08:00
|
|
|
static_assert(is_invocable_v<_Visitor, _Values...>,
|
2016-12-03 07:00:05 +08:00
|
|
|
"`std::visit` requires the visitor to be exhaustive.");
|
|
|
|
}
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Visitor>
|
2016-12-03 07:00:05 +08:00
|
|
|
struct __value_visitor {
|
|
|
|
template <class... _Alts>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr decltype(auto) operator()(_Alts&&... __alts) const {
|
|
|
|
__std_visit_exhaustive_visitor_check<
|
2020-11-18 06:34:23 +08:00
|
|
|
_Visitor,
|
|
|
|
decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
|
2022-04-07 05:10:21 +08:00
|
|
|
return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
|
|
|
|
_VSTD::forward<_Alts>(__alts).__value...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
2020-11-18 06:34:23 +08:00
|
|
|
_Visitor&& __visitor;
|
2016-12-03 07:00:05 +08:00
|
|
|
};
|
|
|
|
|
2021-01-26 00:12:25 +08:00
|
|
|
#if _LIBCPP_STD_VER > 17
|
|
|
|
template <class _Rp, class _Visitor>
|
|
|
|
struct __value_visitor_return_type {
|
|
|
|
template <class... _Alts>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr _Rp operator()(_Alts&&... __alts) const {
|
|
|
|
__std_visit_exhaustive_visitor_check<
|
|
|
|
_Visitor,
|
|
|
|
decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
|
|
|
|
if constexpr (is_void_v<_Rp>) {
|
2022-04-07 05:10:21 +08:00
|
|
|
_VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
|
|
|
|
_VSTD::forward<_Alts>(__alts).__value...);
|
2021-01-26 00:12:25 +08:00
|
|
|
}
|
|
|
|
else {
|
2022-04-07 05:10:21 +08:00
|
|
|
return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
|
|
|
|
_VSTD::forward<_Alts>(__alts).__value...);
|
2021-01-26 00:12:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_Visitor&& __visitor;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2020-11-18 06:34:23 +08:00
|
|
|
template <class _Visitor>
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-11-18 06:34:23 +08:00
|
|
|
static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
|
|
|
|
return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
2021-01-26 00:12:25 +08:00
|
|
|
|
|
|
|
#if _LIBCPP_STD_VER > 17
|
|
|
|
template <class _Rp, class _Visitor>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
|
|
|
|
return __value_visitor_return_type<_Rp, _Visitor>{_VSTD::forward<_Visitor>(__visitor)};
|
|
|
|
}
|
|
|
|
#endif
|
2021-01-26 04:10:41 +08:00
|
|
|
};
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
} // namespace __visitation
|
|
|
|
|
|
|
|
template <size_t _Index, class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS __alt {
|
2016-12-03 07:00:05 +08:00
|
|
|
using __value_type = _Tp;
|
|
|
|
|
|
|
|
template <class... _Args>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
explicit constexpr __alt(in_place_t, _Args&&... __args)
|
|
|
|
: __value(_VSTD::forward<_Args>(__args)...) {}
|
|
|
|
|
|
|
|
__value_type __value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
|
2017-01-05 07:56:00 +08:00
|
|
|
union _LIBCPP_TEMPLATE_VIS __union;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <_Trait _DestructibleTrait, size_t _Index>
|
2017-01-05 07:56:00 +08:00
|
|
|
union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
|
|
|
|
template <size_t _Index, class _Tp, class... _Types> \
|
2020-11-18 06:34:23 +08:00
|
|
|
union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
|
2016-12-03 07:00:05 +08:00
|
|
|
_Index, \
|
|
|
|
_Tp, \
|
|
|
|
_Types...> { \
|
|
|
|
public: \
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY \
|
|
|
|
explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
|
|
|
|
\
|
|
|
|
template <class... _Args> \
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY \
|
|
|
|
explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
|
|
|
|
: __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
|
|
|
|
\
|
|
|
|
template <size_t _Ip, class... _Args> \
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY \
|
|
|
|
explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
|
|
|
|
: __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
|
|
|
|
\
|
|
|
|
__union(const __union&) = default; \
|
|
|
|
__union(__union&&) = default; \
|
|
|
|
\
|
|
|
|
destructor \
|
|
|
|
\
|
|
|
|
__union& operator=(const __union&) = default; \
|
|
|
|
__union& operator=(__union&&) = default; \
|
|
|
|
\
|
|
|
|
private: \
|
|
|
|
char __dummy; \
|
|
|
|
__alt<_Index, _Tp> __head; \
|
|
|
|
__union<destructible_trait, _Index + 1, _Types...> __tail; \
|
|
|
|
\
|
|
|
|
friend struct __access::__union; \
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
|
|
|
|
_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
|
|
|
|
_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
|
|
|
|
|
|
|
|
#undef _LIBCPP_VARIANT_UNION
|
|
|
|
|
|
|
|
template <_Trait _DestructibleTrait, class... _Types>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __base {
|
2016-12-03 07:00:05 +08:00
|
|
|
public:
|
2017-11-19 12:19:44 +08:00
|
|
|
using __index_t = __variant_index_t<sizeof...(_Types)>;
|
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2022-07-09 00:17:26 +08:00
|
|
|
explicit constexpr __base(__valueless_t __tag) noexcept
|
|
|
|
: __data(__tag), __index(__variant_npos<__index_t>) {}
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <size_t _Ip, class... _Args>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
|
2016-12-03 07:17:33 +08:00
|
|
|
:
|
|
|
|
__data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
|
|
|
|
__index(_Ip) {}
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool valueless_by_exception() const noexcept {
|
|
|
|
return index() == variant_npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr size_t index() const noexcept {
|
2017-11-19 12:19:44 +08:00
|
|
|
return __index == __variant_npos<__index_t> ? variant_npos : __index;
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr auto&& __as_base() & { return *this; }
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr auto&& __as_base() && { return _VSTD::move(*this); }
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr auto&& __as_base() const & { return *this; }
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
static constexpr size_t __size() { return sizeof...(_Types); }
|
|
|
|
|
|
|
|
__union<_DestructibleTrait, 0, _Types...> __data;
|
2017-11-19 12:19:44 +08:00
|
|
|
__index_t __index;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
friend struct __access::__base;
|
|
|
|
friend struct __visitation::__base;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Traits, _Trait = _Traits::__destructible_trait>
|
2020-10-06 04:30:23 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __dtor;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
|
|
|
|
template <class... _Types> \
|
2020-10-06 04:30:23 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, \
|
|
|
|
destructible_trait> \
|
2016-12-03 07:00:05 +08:00
|
|
|
: public __base<destructible_trait, _Types...> { \
|
|
|
|
using __base_type = __base<destructible_trait, _Types...>; \
|
2017-11-19 12:19:44 +08:00
|
|
|
using __index_t = typename __base_type::__index_t; \
|
2016-12-03 07:00:05 +08:00
|
|
|
\
|
|
|
|
public: \
|
|
|
|
using __base_type::__base_type; \
|
|
|
|
using __base_type::operator=; \
|
|
|
|
\
|
2020-10-06 04:30:23 +08:00
|
|
|
__dtor(const __dtor&) = default; \
|
|
|
|
__dtor(__dtor&&) = default; \
|
2016-12-03 07:00:05 +08:00
|
|
|
destructor \
|
2020-10-06 04:30:23 +08:00
|
|
|
__dtor& operator=(const __dtor&) = default; \
|
|
|
|
__dtor& operator=(__dtor&&) = default; \
|
2016-12-03 07:00:05 +08:00
|
|
|
\
|
|
|
|
protected: \
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY \
|
|
|
|
destroy \
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_DESTRUCTOR(
|
|
|
|
_Trait::_TriviallyAvailable,
|
2020-10-06 04:30:23 +08:00
|
|
|
~__dtor() = default;,
|
2017-11-19 12:19:44 +08:00
|
|
|
void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
_LIBCPP_VARIANT_DESTRUCTOR(
|
|
|
|
_Trait::_Available,
|
2020-10-06 04:30:23 +08:00
|
|
|
~__dtor() { __destroy(); },
|
2016-12-03 07:00:05 +08:00
|
|
|
void __destroy() noexcept {
|
|
|
|
if (!this->valueless_by_exception()) {
|
|
|
|
__visitation::__base::__visit_alt(
|
|
|
|
[](auto& __alt) noexcept {
|
2018-02-12 23:41:25 +08:00
|
|
|
using __alt_type = __uncvref_t<decltype(__alt)>;
|
2016-12-03 07:00:05 +08:00
|
|
|
__alt.~__alt_type();
|
|
|
|
},
|
|
|
|
*this);
|
|
|
|
}
|
2017-11-19 12:19:44 +08:00
|
|
|
this->__index = __variant_npos<__index_t>;
|
2016-12-03 07:00:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_DESTRUCTOR(
|
|
|
|
_Trait::_Unavailable,
|
2020-10-06 04:30:23 +08:00
|
|
|
~__dtor() = delete;,
|
2016-12-03 07:00:05 +08:00
|
|
|
void __destroy() noexcept = delete;);
|
|
|
|
|
|
|
|
#undef _LIBCPP_VARIANT_DESTRUCTOR
|
|
|
|
|
|
|
|
template <class _Traits>
|
2020-10-06 04:30:23 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
|
|
|
|
using __base_type = __dtor<_Traits>;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
using __base_type::__base_type;
|
|
|
|
using __base_type::operator=;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template <size_t _Ip, class _Tp, class... _Args>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-04-16 03:32:02 +08:00
|
|
|
static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
|
2020-11-18 06:34:23 +08:00
|
|
|
::new ((void*)_VSTD::addressof(__a))
|
2016-12-03 07:00:05 +08:00
|
|
|
__alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
|
2020-11-18 06:34:23 +08:00
|
|
|
return __a.__value;
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Rhs>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2020-10-06 04:30:23 +08:00
|
|
|
static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
|
2016-12-03 07:00:05 +08:00
|
|
|
__lhs.__destroy();
|
|
|
|
if (!__rhs.valueless_by_exception()) {
|
|
|
|
__visitation::__base::__visit_alt_at(
|
|
|
|
__rhs.index(),
|
|
|
|
[](auto& __lhs_alt, auto&& __rhs_alt) {
|
|
|
|
__construct_alt(
|
|
|
|
__lhs_alt,
|
|
|
|
_VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
|
|
|
|
},
|
|
|
|
__lhs, _VSTD::forward<_Rhs>(__rhs));
|
|
|
|
__lhs.__index = __rhs.index();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Traits, _Trait = _Traits::__move_constructible_trait>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __move_constructor;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
|
|
|
|
move_constructor) \
|
|
|
|
template <class... _Types> \
|
2020-10-06 04:30:23 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
|
|
|
|
move_constructible_trait> \
|
|
|
|
: public __ctor<__traits<_Types...>> { \
|
|
|
|
using __base_type = __ctor<__traits<_Types...>>; \
|
2016-12-03 07:00:05 +08:00
|
|
|
\
|
|
|
|
public: \
|
|
|
|
using __base_type::__base_type; \
|
|
|
|
using __base_type::operator=; \
|
|
|
|
\
|
|
|
|
__move_constructor(const __move_constructor&) = default; \
|
|
|
|
move_constructor \
|
|
|
|
~__move_constructor() = default; \
|
|
|
|
__move_constructor& operator=(const __move_constructor&) = default; \
|
|
|
|
__move_constructor& operator=(__move_constructor&&) = default; \
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
|
|
|
|
_Trait::_TriviallyAvailable,
|
|
|
|
__move_constructor(__move_constructor&& __that) = default;);
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
|
|
|
|
_Trait::_Available,
|
|
|
|
__move_constructor(__move_constructor&& __that) noexcept(
|
|
|
|
__all<is_nothrow_move_constructible_v<_Types>...>::value)
|
|
|
|
: __move_constructor(__valueless_t{}) {
|
|
|
|
this->__generic_construct(*this, _VSTD::move(__that));
|
|
|
|
});
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
|
|
|
|
_Trait::_Unavailable,
|
|
|
|
__move_constructor(__move_constructor&&) = delete;);
|
|
|
|
|
|
|
|
#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
|
|
|
|
|
|
|
|
template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __copy_constructor;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
|
|
|
|
copy_constructor) \
|
|
|
|
template <class... _Types> \
|
2020-11-18 06:34:23 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
|
2016-12-03 07:00:05 +08:00
|
|
|
copy_constructible_trait> \
|
|
|
|
: public __move_constructor<__traits<_Types...>> { \
|
|
|
|
using __base_type = __move_constructor<__traits<_Types...>>; \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
using __base_type::__base_type; \
|
|
|
|
using __base_type::operator=; \
|
|
|
|
\
|
|
|
|
copy_constructor \
|
|
|
|
__copy_constructor(__copy_constructor&&) = default; \
|
|
|
|
~__copy_constructor() = default; \
|
|
|
|
__copy_constructor& operator=(const __copy_constructor&) = default; \
|
|
|
|
__copy_constructor& operator=(__copy_constructor&&) = default; \
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
|
|
|
|
_Trait::_TriviallyAvailable,
|
|
|
|
__copy_constructor(const __copy_constructor& __that) = default;);
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
|
|
|
|
_Trait::_Available,
|
|
|
|
__copy_constructor(const __copy_constructor& __that)
|
|
|
|
: __copy_constructor(__valueless_t{}) {
|
|
|
|
this->__generic_construct(*this, __that);
|
|
|
|
});
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
|
|
|
|
_Trait::_Unavailable,
|
|
|
|
__copy_constructor(const __copy_constructor&) = delete;);
|
|
|
|
|
|
|
|
#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
|
|
|
|
|
|
|
|
template <class _Traits>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
|
2016-12-03 07:00:05 +08:00
|
|
|
using __base_type = __copy_constructor<_Traits>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using __base_type::__base_type;
|
|
|
|
using __base_type::operator=;
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Args>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-04-16 03:32:02 +08:00
|
|
|
auto& __emplace(_Args&&... __args) {
|
2016-12-03 07:00:05 +08:00
|
|
|
this->__destroy();
|
2017-04-16 03:32:02 +08:00
|
|
|
auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
|
2020-11-18 06:34:23 +08:00
|
|
|
_VSTD::forward<_Args>(__args)...);
|
2016-12-03 07:00:05 +08:00
|
|
|
this->__index = _Ip;
|
2017-04-16 03:32:02 +08:00
|
|
|
return __res;
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-06-07 18:22:43 +08:00
|
|
|
template <size_t _Ip, class _Tp, class _Arg>
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-06-07 18:22:43 +08:00
|
|
|
void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
|
2016-12-03 07:00:05 +08:00
|
|
|
if (this->index() == _Ip) {
|
|
|
|
__a.__value = _VSTD::forward<_Arg>(__arg);
|
|
|
|
} else {
|
|
|
|
struct {
|
|
|
|
void operator()(true_type) const {
|
2017-06-07 18:22:43 +08:00
|
|
|
__this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
void operator()(false_type) const {
|
2017-06-07 18:22:43 +08:00
|
|
|
__this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
__assignment* __this;
|
|
|
|
_Arg&& __arg;
|
|
|
|
} __impl{this, _VSTD::forward<_Arg>(__arg)};
|
2017-06-07 18:22:43 +08:00
|
|
|
__impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
|
|
|
|
!is_nothrow_move_constructible_v<_Tp>>{});
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _That>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __generic_assign(_That&& __that) {
|
|
|
|
if (this->valueless_by_exception() && __that.valueless_by_exception()) {
|
|
|
|
// do nothing.
|
|
|
|
} else if (__that.valueless_by_exception()) {
|
|
|
|
this->__destroy();
|
|
|
|
} else {
|
|
|
|
__visitation::__base::__visit_alt_at(
|
|
|
|
__that.index(),
|
|
|
|
[this](auto& __this_alt, auto&& __that_alt) {
|
|
|
|
this->__assign_alt(
|
|
|
|
__this_alt,
|
2017-06-07 18:22:43 +08:00
|
|
|
_VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
|
2016-12-03 07:00:05 +08:00
|
|
|
},
|
|
|
|
*this, _VSTD::forward<_That>(__that));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Traits, _Trait = _Traits::__move_assignable_trait>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __move_assignment;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
|
|
|
|
move_assignment) \
|
|
|
|
template <class... _Types> \
|
2020-11-18 06:34:23 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
|
2016-12-03 07:00:05 +08:00
|
|
|
move_assignable_trait> \
|
|
|
|
: public __assignment<__traits<_Types...>> { \
|
|
|
|
using __base_type = __assignment<__traits<_Types...>>; \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
using __base_type::__base_type; \
|
|
|
|
using __base_type::operator=; \
|
|
|
|
\
|
|
|
|
__move_assignment(const __move_assignment&) = default; \
|
|
|
|
__move_assignment(__move_assignment&&) = default; \
|
|
|
|
~__move_assignment() = default; \
|
|
|
|
__move_assignment& operator=(const __move_assignment&) = default; \
|
|
|
|
move_assignment \
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
|
|
|
|
_Trait::_TriviallyAvailable,
|
|
|
|
__move_assignment& operator=(__move_assignment&& __that) = default;);
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
|
|
|
|
_Trait::_Available,
|
|
|
|
__move_assignment& operator=(__move_assignment&& __that) noexcept(
|
|
|
|
__all<(is_nothrow_move_constructible_v<_Types> &&
|
|
|
|
is_nothrow_move_assignable_v<_Types>)...>::value) {
|
|
|
|
this->__generic_assign(_VSTD::move(__that));
|
|
|
|
return *this;
|
|
|
|
});
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
|
|
|
|
_Trait::_Unavailable,
|
|
|
|
__move_assignment& operator=(__move_assignment&&) = delete;);
|
|
|
|
|
|
|
|
#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
|
|
|
|
|
|
|
|
template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __copy_assignment;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
|
|
|
|
copy_assignment) \
|
|
|
|
template <class... _Types> \
|
2020-11-18 06:34:23 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
|
2016-12-03 07:00:05 +08:00
|
|
|
copy_assignable_trait> \
|
|
|
|
: public __move_assignment<__traits<_Types...>> { \
|
|
|
|
using __base_type = __move_assignment<__traits<_Types...>>; \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
using __base_type::__base_type; \
|
|
|
|
using __base_type::operator=; \
|
|
|
|
\
|
|
|
|
__copy_assignment(const __copy_assignment&) = default; \
|
|
|
|
__copy_assignment(__copy_assignment&&) = default; \
|
|
|
|
~__copy_assignment() = default; \
|
|
|
|
copy_assignment \
|
|
|
|
__copy_assignment& operator=(__copy_assignment&&) = default; \
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_COPY_ASSIGNMENT(
|
|
|
|
_Trait::_TriviallyAvailable,
|
|
|
|
__copy_assignment& operator=(const __copy_assignment& __that) = default;);
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_COPY_ASSIGNMENT(
|
|
|
|
_Trait::_Available,
|
|
|
|
__copy_assignment& operator=(const __copy_assignment& __that) {
|
|
|
|
this->__generic_assign(__that);
|
|
|
|
return *this;
|
|
|
|
});
|
|
|
|
|
|
|
|
_LIBCPP_VARIANT_COPY_ASSIGNMENT(
|
|
|
|
_Trait::_Unavailable,
|
|
|
|
__copy_assignment& operator=(const __copy_assignment&) = delete;);
|
|
|
|
|
|
|
|
#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
|
|
|
|
|
|
|
|
template <class... _Types>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS __impl
|
2016-12-03 07:00:05 +08:00
|
|
|
: public __copy_assignment<__traits<_Types...>> {
|
|
|
|
using __base_type = __copy_assignment<__traits<_Types...>>;
|
|
|
|
|
|
|
|
public:
|
2022-03-04 02:39:12 +08:00
|
|
|
using __base_type::__base_type; // get in_place_index_t constructor & friends
|
|
|
|
__impl(__impl const&) = default;
|
|
|
|
__impl(__impl&&) = default;
|
|
|
|
__impl& operator=(__impl const&) = default;
|
|
|
|
__impl& operator=(__impl&&) = default;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <size_t _Ip, class _Arg>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __assign(_Arg&& __arg) {
|
|
|
|
this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
|
2017-06-07 18:22:43 +08:00
|
|
|
_VSTD::forward<_Arg>(__arg));
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __swap(__impl& __that) {
|
|
|
|
if (this->valueless_by_exception() && __that.valueless_by_exception()) {
|
|
|
|
// do nothing.
|
|
|
|
} else if (this->index() == __that.index()) {
|
|
|
|
__visitation::__base::__visit_alt_at(
|
|
|
|
this->index(),
|
|
|
|
[](auto& __this_alt, auto& __that_alt) {
|
|
|
|
using _VSTD::swap;
|
|
|
|
swap(__this_alt.__value, __that_alt.__value);
|
|
|
|
},
|
|
|
|
*this,
|
|
|
|
__that);
|
|
|
|
} else {
|
|
|
|
__impl* __lhs = this;
|
|
|
|
__impl* __rhs = _VSTD::addressof(__that);
|
|
|
|
if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
|
|
|
|
_VSTD::swap(__lhs, __rhs);
|
|
|
|
}
|
|
|
|
__impl __tmp(_VSTD::move(*__rhs));
|
2020-06-17 06:15:10 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2020-07-09 01:46:02 +08:00
|
|
|
if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
|
2016-12-03 07:00:05 +08:00
|
|
|
this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
|
2020-06-17 04:29:23 +08:00
|
|
|
} else {
|
|
|
|
// EXTENSION: When the move construction of `__lhs` into `__rhs` throws
|
|
|
|
// and `__tmp` is nothrow move constructible then we move `__tmp` back
|
|
|
|
// into `__rhs` and provide the strong exception safety guarantee.
|
|
|
|
try {
|
|
|
|
this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
|
|
|
|
} catch (...) {
|
|
|
|
if (__tmp.__move_nothrow()) {
|
|
|
|
this->__generic_construct(*__rhs, _VSTD::move(__tmp));
|
|
|
|
}
|
|
|
|
throw;
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-17 06:15:10 +08:00
|
|
|
#else
|
|
|
|
// this isn't consolidated with the `if constexpr` branch above due to
|
|
|
|
// `throw` being ill-formed with exceptions disabled even when discarded.
|
|
|
|
this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
|
|
|
|
#endif
|
2016-12-03 07:00:05 +08:00
|
|
|
this->__generic_construct(*__lhs, _VSTD::move(__tmp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool __move_nothrow() const {
|
|
|
|
constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
|
|
|
|
return this->valueless_by_exception() || __results[this->index()];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-15 02:21:15 +08:00
|
|
|
struct __no_narrowing_check {
|
|
|
|
template <class _Dest, class _Source>
|
2022-03-19 00:49:02 +08:00
|
|
|
using _Apply = __type_identity<_Dest>;
|
2019-07-15 02:21:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct __narrowing_check {
|
|
|
|
template <class _Dest>
|
2022-03-19 00:49:02 +08:00
|
|
|
static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
|
2019-07-15 02:21:15 +08:00
|
|
|
template <class _Dest, class _Source>
|
2021-08-31 22:32:11 +08:00
|
|
|
using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({declval<_Source>()}));
|
2019-07-15 02:21:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Dest, class _Source>
|
2021-08-31 22:32:11 +08:00
|
|
|
using __check_for_narrowing _LIBCPP_NODEBUG =
|
2019-07-15 05:29:39 +08:00
|
|
|
typename _If<
|
2019-07-15 02:21:15 +08:00
|
|
|
#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
|
|
|
|
false &&
|
|
|
|
#endif
|
|
|
|
is_arithmetic<_Dest>::value,
|
|
|
|
__narrowing_check,
|
|
|
|
__no_narrowing_check
|
2019-07-15 05:29:39 +08:00
|
|
|
>::template _Apply<_Dest, _Source>;
|
2019-06-21 06:09:40 +08:00
|
|
|
|
2019-07-15 05:29:39 +08:00
|
|
|
template <class _Tp, size_t _Idx>
|
|
|
|
struct __overload {
|
2019-06-21 06:09:40 +08:00
|
|
|
template <class _Up>
|
2019-07-15 02:31:55 +08:00
|
|
|
auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
|
2019-06-21 06:09:40 +08:00
|
|
|
};
|
|
|
|
|
2019-07-15 05:29:39 +08:00
|
|
|
template <class _Tp, size_t>
|
|
|
|
struct __overload_bool {
|
2019-06-21 06:09:40 +08:00
|
|
|
template <class _Up, class _Ap = __uncvref_t<_Up>>
|
|
|
|
auto operator()(bool, _Up&&) const
|
2022-03-19 00:49:02 +08:00
|
|
|
-> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
|
2016-12-03 07:00:05 +08:00
|
|
|
};
|
|
|
|
|
2019-07-15 05:29:39 +08:00
|
|
|
template <size_t _Idx>
|
|
|
|
struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
|
|
|
|
template <size_t _Idx>
|
|
|
|
struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
|
|
|
|
template <size_t _Idx>
|
|
|
|
struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
|
|
|
|
template <size_t _Idx>
|
|
|
|
struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
|
|
|
|
|
|
|
|
template <class ..._Bases>
|
|
|
|
struct __all_overloads : _Bases... {
|
|
|
|
void operator()() const;
|
|
|
|
using _Bases::operator()...;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class IdxSeq>
|
|
|
|
struct __make_overloads_imp;
|
|
|
|
|
|
|
|
template <size_t ..._Idx>
|
|
|
|
struct __make_overloads_imp<__tuple_indices<_Idx...> > {
|
|
|
|
template <class ..._Types>
|
2021-08-31 22:32:11 +08:00
|
|
|
using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
|
2019-07-15 05:29:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ..._Types>
|
2021-08-31 22:32:11 +08:00
|
|
|
using _MakeOverloads _LIBCPP_NODEBUG = typename __make_overloads_imp<
|
2019-07-15 05:29:39 +08:00
|
|
|
__make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
|
2019-06-21 06:09:40 +08:00
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
template <class _Tp, class... _Types>
|
2019-06-21 06:09:40 +08:00
|
|
|
using __best_match_t =
|
2019-07-15 05:29:39 +08:00
|
|
|
typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
|
2016-12-03 07:00:05 +08:00
|
|
|
|
2022-02-02 01:11:49 +08:00
|
|
|
} // namespace __variant_detail
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class... _Types>
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS variant
|
2016-12-03 07:00:05 +08:00
|
|
|
: private __sfinae_ctor_base<
|
|
|
|
__all<is_copy_constructible_v<_Types>...>::value,
|
|
|
|
__all<is_move_constructible_v<_Types>...>::value>,
|
|
|
|
private __sfinae_assign_base<
|
|
|
|
__all<(is_copy_constructible_v<_Types> &&
|
|
|
|
is_copy_assignable_v<_Types>)...>::value,
|
|
|
|
__all<(is_move_constructible_v<_Types> &&
|
|
|
|
is_move_assignable_v<_Types>)...>::value> {
|
|
|
|
static_assert(0 < sizeof...(_Types),
|
|
|
|
"variant must consist of at least one alternative.");
|
|
|
|
|
|
|
|
static_assert(__all<!is_array_v<_Types>...>::value,
|
|
|
|
"variant can not have an array type as an alternative.");
|
|
|
|
|
|
|
|
static_assert(__all<!is_reference_v<_Types>...>::value,
|
|
|
|
"variant can not have a reference type as an alternative.");
|
|
|
|
|
|
|
|
static_assert(__all<!is_void_v<_Types>...>::value,
|
|
|
|
"variant can not have a void type as an alternative.");
|
|
|
|
|
|
|
|
using __first_type = variant_alternative_t<0, variant>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <bool _Dummy = true,
|
|
|
|
enable_if_t<__dependent_type<is_default_constructible<__first_type>,
|
|
|
|
_Dummy>::value,
|
|
|
|
int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
|
|
|
|
: __impl(in_place_index<0>) {}
|
|
|
|
|
|
|
|
variant(const variant&) = default;
|
|
|
|
variant(variant&&) = default;
|
|
|
|
|
|
|
|
template <
|
|
|
|
class _Arg,
|
2018-02-07 04:56:55 +08:00
|
|
|
enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
|
|
|
|
enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
|
|
|
|
enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
|
2016-12-03 07:00:05 +08:00
|
|
|
class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
|
|
|
|
size_t _Ip =
|
|
|
|
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
|
|
|
|
enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr variant(_Arg&& __arg) noexcept(
|
|
|
|
is_nothrow_constructible_v<_Tp, _Arg>)
|
|
|
|
: __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Args,
|
2018-03-24 07:42:30 +08:00
|
|
|
class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
|
|
|
|
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
|
|
|
|
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-03-24 07:42:30 +08:00
|
|
|
explicit constexpr variant(
|
|
|
|
in_place_index_t<_Ip>,
|
|
|
|
_Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
|
2016-12-03 07:00:05 +08:00
|
|
|
: __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
|
|
|
|
|
2018-03-24 07:42:30 +08:00
|
|
|
template <
|
|
|
|
size_t _Ip,
|
|
|
|
class _Up,
|
|
|
|
class... _Args,
|
|
|
|
enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
|
|
|
|
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
|
2016-12-03 07:00:05 +08:00
|
|
|
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
|
|
|
|
int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-03-24 07:42:30 +08:00
|
|
|
explicit constexpr variant(
|
|
|
|
in_place_index_t<_Ip>,
|
|
|
|
initializer_list<_Up> __il,
|
|
|
|
_Args&&... __args) noexcept(
|
|
|
|
is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
|
2016-12-03 07:00:05 +08:00
|
|
|
: __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
|
|
|
|
|
|
|
|
template <
|
|
|
|
class _Tp,
|
|
|
|
class... _Args,
|
|
|
|
size_t _Ip =
|
|
|
|
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
|
|
|
|
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
|
|
|
|
is_nothrow_constructible_v<_Tp, _Args...>)
|
|
|
|
: __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
|
|
|
|
|
|
|
|
template <
|
|
|
|
class _Tp,
|
|
|
|
class _Up,
|
|
|
|
class... _Args,
|
|
|
|
size_t _Ip =
|
|
|
|
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
|
|
|
|
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
|
|
|
|
int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
explicit constexpr variant(
|
|
|
|
in_place_type_t<_Tp>,
|
|
|
|
initializer_list<_Up> __il,
|
|
|
|
_Args&&... __args) noexcept(
|
|
|
|
is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
|
|
|
|
: __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
|
|
|
|
|
|
|
|
~variant() = default;
|
|
|
|
|
|
|
|
variant& operator=(const variant&) = default;
|
|
|
|
variant& operator=(variant&&) = default;
|
|
|
|
|
|
|
|
template <
|
|
|
|
class _Arg,
|
2018-02-07 04:56:55 +08:00
|
|
|
enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
|
2016-12-03 07:00:05 +08:00
|
|
|
class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
|
|
|
|
size_t _Ip =
|
|
|
|
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
|
|
|
|
enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
|
|
|
|
int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
variant& operator=(_Arg&& __arg) noexcept(
|
|
|
|
is_nothrow_assignable_v<_Tp&, _Arg> &&
|
|
|
|
is_nothrow_constructible_v<_Tp, _Arg>) {
|
|
|
|
__impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
size_t _Ip,
|
|
|
|
class... _Args,
|
|
|
|
enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
|
|
|
|
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
|
|
|
|
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-04-16 03:32:02 +08:00
|
|
|
_Tp& emplace(_Args&&... __args) {
|
|
|
|
return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
size_t _Ip,
|
|
|
|
class _Up,
|
|
|
|
class... _Args,
|
|
|
|
enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
|
|
|
|
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
|
|
|
|
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
|
|
|
|
int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-04-16 03:32:02 +08:00
|
|
|
_Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
|
|
|
|
return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
class _Tp,
|
|
|
|
class... _Args,
|
|
|
|
size_t _Ip =
|
|
|
|
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
|
|
|
|
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-04-16 03:32:02 +08:00
|
|
|
_Tp& emplace(_Args&&... __args) {
|
|
|
|
return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
class _Tp,
|
|
|
|
class _Up,
|
|
|
|
class... _Args,
|
|
|
|
size_t _Ip =
|
|
|
|
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
|
|
|
|
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
|
|
|
|
int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-04-16 03:32:02 +08:00
|
|
|
_Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
|
|
|
|
return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool valueless_by_exception() const noexcept {
|
|
|
|
return __impl.valueless_by_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr size_t index() const noexcept { return __impl.index(); }
|
|
|
|
|
|
|
|
template <
|
|
|
|
bool _Dummy = true,
|
|
|
|
enable_if_t<
|
|
|
|
__all<(
|
|
|
|
__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
|
|
|
|
__dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
|
|
|
|
int> = 0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void swap(variant& __that) noexcept(
|
|
|
|
__all<(is_nothrow_move_constructible_v<_Types> &&
|
|
|
|
is_nothrow_swappable_v<_Types>)...>::value) {
|
|
|
|
__impl.__swap(__that.__impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
__variant_detail::__impl<_Types...> __impl;
|
|
|
|
|
|
|
|
friend struct __variant_detail::__access::__variant;
|
|
|
|
friend struct __variant_detail::__visitation::__variant;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
|
|
|
|
return __v.index() == _Ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
|
|
|
|
return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Ip, class _Vp>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2018-08-28 05:41:50 +08:00
|
|
|
constexpr auto&& __generic_get(_Vp&& __v) {
|
2016-12-03 07:00:05 +08:00
|
|
|
using __variant_detail::__access::__variant;
|
|
|
|
if (!__holds_alternative<_Ip>(__v)) {
|
2016-12-03 09:58:07 +08:00
|
|
|
__throw_bad_variant_access();
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
|
|
|
|
variant<_Types...>& __v) {
|
|
|
|
static_assert(_Ip < sizeof...(_Types));
|
|
|
|
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
|
|
|
|
return __generic_get<_Ip>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
|
|
|
|
variant<_Types...>&& __v) {
|
|
|
|
static_assert(_Ip < sizeof...(_Types));
|
|
|
|
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
|
|
|
|
return __generic_get<_Ip>(_VSTD::move(__v));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
|
|
|
|
const variant<_Types...>& __v) {
|
|
|
|
static_assert(_Ip < sizeof...(_Types));
|
|
|
|
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
|
|
|
|
return __generic_get<_Ip>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
|
|
|
|
const variant<_Types...>&& __v) {
|
|
|
|
static_assert(_Ip < sizeof...(_Types));
|
|
|
|
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
|
|
|
|
return __generic_get<_Ip>(_VSTD::move(__v));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr _Tp& get(variant<_Types...>& __v) {
|
|
|
|
static_assert(!is_void_v<_Tp>);
|
|
|
|
return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr _Tp&& get(variant<_Types...>&& __v) {
|
|
|
|
static_assert(!is_void_v<_Tp>);
|
|
|
|
return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
|
|
|
|
_VSTD::move(__v));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr const _Tp& get(const variant<_Types...>& __v) {
|
|
|
|
static_assert(!is_void_v<_Tp>);
|
|
|
|
return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
|
2016-12-03 07:00:05 +08:00
|
|
|
constexpr const _Tp&& get(const variant<_Types...>&& __v) {
|
|
|
|
static_assert(!is_void_v<_Tp>);
|
|
|
|
return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
|
|
|
|
_VSTD::move(__v));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Ip, class _Vp>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr auto* __generic_get_if(_Vp* __v) noexcept {
|
|
|
|
using __variant_detail::__access::__variant;
|
|
|
|
return __v && __holds_alternative<_Ip>(*__v)
|
|
|
|
? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
|
|
|
|
get_if(variant<_Types...>* __v) noexcept {
|
|
|
|
static_assert(_Ip < sizeof...(_Types));
|
|
|
|
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
|
|
|
|
return __generic_get_if<_Ip>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _Ip, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
|
|
|
|
get_if(const variant<_Types...>* __v) noexcept {
|
|
|
|
static_assert(_Ip < sizeof...(_Types));
|
|
|
|
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
|
|
|
|
return __generic_get_if<_Ip>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr add_pointer_t<_Tp>
|
|
|
|
get_if(variant<_Types...>* __v) noexcept {
|
|
|
|
static_assert(!is_void_v<_Tp>);
|
|
|
|
return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr add_pointer_t<const _Tp>
|
|
|
|
get_if(const variant<_Types...>* __v) noexcept {
|
|
|
|
static_assert(!is_void_v<_Tp>);
|
|
|
|
return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
|
|
|
|
}
|
|
|
|
|
2018-09-20 01:53:21 +08:00
|
|
|
template <class _Operator>
|
|
|
|
struct __convert_to_bool {
|
|
|
|
template <class _T1, class _T2>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-28 00:02:06 +08:00
|
|
|
static_assert(is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
|
2018-09-20 01:53:21 +08:00
|
|
|
"the relational operator does not return a type which is implicitly convertible to bool");
|
|
|
|
return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
template <class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool operator==(const variant<_Types...>& __lhs,
|
|
|
|
const variant<_Types...>& __rhs) {
|
|
|
|
using __variant_detail::__visitation::__variant;
|
|
|
|
if (__lhs.index() != __rhs.index()) return false;
|
|
|
|
if (__lhs.valueless_by_exception()) return true;
|
2018-09-20 01:53:21 +08:00
|
|
|
return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool operator!=(const variant<_Types...>& __lhs,
|
|
|
|
const variant<_Types...>& __rhs) {
|
|
|
|
using __variant_detail::__visitation::__variant;
|
|
|
|
if (__lhs.index() != __rhs.index()) return true;
|
|
|
|
if (__lhs.valueless_by_exception()) return false;
|
|
|
|
return __variant::__visit_value_at(
|
2018-09-20 01:53:21 +08:00
|
|
|
__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool operator<(const variant<_Types...>& __lhs,
|
|
|
|
const variant<_Types...>& __rhs) {
|
|
|
|
using __variant_detail::__visitation::__variant;
|
|
|
|
if (__rhs.valueless_by_exception()) return false;
|
|
|
|
if (__lhs.valueless_by_exception()) return true;
|
|
|
|
if (__lhs.index() < __rhs.index()) return true;
|
|
|
|
if (__lhs.index() > __rhs.index()) return false;
|
2018-09-20 01:53:21 +08:00
|
|
|
return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool operator>(const variant<_Types...>& __lhs,
|
|
|
|
const variant<_Types...>& __rhs) {
|
|
|
|
using __variant_detail::__visitation::__variant;
|
|
|
|
if (__lhs.valueless_by_exception()) return false;
|
|
|
|
if (__rhs.valueless_by_exception()) return true;
|
|
|
|
if (__lhs.index() > __rhs.index()) return true;
|
|
|
|
if (__lhs.index() < __rhs.index()) return false;
|
2018-09-20 01:53:21 +08:00
|
|
|
return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool operator<=(const variant<_Types...>& __lhs,
|
|
|
|
const variant<_Types...>& __rhs) {
|
|
|
|
using __variant_detail::__visitation::__variant;
|
|
|
|
if (__lhs.valueless_by_exception()) return true;
|
|
|
|
if (__rhs.valueless_by_exception()) return false;
|
|
|
|
if (__lhs.index() < __rhs.index()) return true;
|
|
|
|
if (__lhs.index() > __rhs.index()) return false;
|
|
|
|
return __variant::__visit_value_at(
|
2018-09-20 01:53:21 +08:00
|
|
|
__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr bool operator>=(const variant<_Types...>& __lhs,
|
|
|
|
const variant<_Types...>& __rhs) {
|
|
|
|
using __variant_detail::__visitation::__variant;
|
|
|
|
if (__rhs.valueless_by_exception()) return true;
|
|
|
|
if (__lhs.valueless_by_exception()) return false;
|
|
|
|
if (__lhs.index() > __rhs.index()) return true;
|
|
|
|
if (__lhs.index() < __rhs.index()) return false;
|
|
|
|
return __variant::__visit_value_at(
|
2018-09-20 01:53:21 +08:00
|
|
|
__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 02:34:03 +08:00
|
|
|
template <class... _Vs>
|
2016-12-03 07:00:05 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2021-03-26 01:09:11 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void
|
|
|
|
__throw_if_valueless(_Vs&&... __vs) {
|
|
|
|
const bool __valueless =
|
|
|
|
(... || _VSTD::__as_variant(__vs).valueless_by_exception());
|
2021-01-26 00:12:25 +08:00
|
|
|
if (__valueless) {
|
2021-03-26 01:09:11 +08:00
|
|
|
__throw_bad_variant_access();
|
2020-11-18 06:34:23 +08:00
|
|
|
}
|
2021-01-26 00:12:25 +08:00
|
|
|
}
|
|
|
|
|
2021-03-26 01:09:11 +08:00
|
|
|
template <
|
|
|
|
class _Visitor, class... _Vs,
|
2021-05-11 01:04:16 +08:00
|
|
|
typename = void_t<decltype(_VSTD::__as_variant(declval<_Vs>()))...> >
|
2021-01-26 00:12:25 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2021-03-26 01:09:11 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr
|
|
|
|
decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
|
2021-01-26 00:12:25 +08:00
|
|
|
using __variant_detail::__visitation::__variant;
|
2021-01-26 02:34:03 +08:00
|
|
|
_VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
|
2020-11-18 06:34:23 +08:00
|
|
|
return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
|
2016-12-03 07:00:05 +08:00
|
|
|
_VSTD::forward<_Vs>(__vs)...);
|
|
|
|
}
|
|
|
|
|
2021-01-26 00:12:25 +08:00
|
|
|
#if _LIBCPP_STD_VER > 17
|
2021-03-26 01:09:11 +08:00
|
|
|
template <
|
|
|
|
class _Rp, class _Visitor, class... _Vs,
|
2021-05-11 01:04:16 +08:00
|
|
|
typename = void_t<decltype(_VSTD::__as_variant(declval<_Vs>()))...> >
|
2021-01-26 00:12:25 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2021-03-26 01:09:11 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
|
|
|
|
visit(_Visitor&& __visitor, _Vs&&... __vs) {
|
2021-01-26 00:12:25 +08:00
|
|
|
using __variant_detail::__visitation::__variant;
|
2021-01-26 02:34:03 +08:00
|
|
|
_VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
|
2021-01-26 00:12:25 +08:00
|
|
|
return __variant::__visit_value<_Rp>(_VSTD::forward<_Visitor>(__visitor),
|
|
|
|
_VSTD::forward<_Vs>(__vs)...);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-03 07:00:05 +08:00
|
|
|
template <class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2021-08-17 00:41:58 +08:00
|
|
|
auto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
|
|
|
|
noexcept(noexcept(__lhs.swap(__rhs)))
|
|
|
|
-> decltype( __lhs.swap(__rhs))
|
|
|
|
{ return __lhs.swap(__rhs); }
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
template <class... _Types>
|
2017-01-21 08:02:12 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS hash<
|
|
|
|
__enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
|
2016-12-03 07:00:05 +08:00
|
|
|
using argument_type = variant<_Types...>;
|
|
|
|
using result_type = size_t;
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
result_type operator()(const argument_type& __v) const {
|
|
|
|
using __variant_detail::__visitation::__variant;
|
2016-12-03 07:38:31 +08:00
|
|
|
size_t __res =
|
|
|
|
__v.valueless_by_exception()
|
2016-12-05 05:37:37 +08:00
|
|
|
? 299792458 // Random value chosen by the universe upon creation
|
2016-12-03 07:00:05 +08:00
|
|
|
: __variant::__visit_alt(
|
|
|
|
[](const auto& __alt) {
|
2018-02-12 23:41:25 +08:00
|
|
|
using __alt_type = __uncvref_t<decltype(__alt)>;
|
2017-01-21 08:02:12 +08:00
|
|
|
using __value_type = remove_const_t<
|
|
|
|
typename __alt_type::__value_type>;
|
2016-12-03 07:00:05 +08:00
|
|
|
return hash<__value_type>{}(__alt.__value);
|
|
|
|
},
|
|
|
|
__v);
|
2016-12-03 07:38:31 +08:00
|
|
|
return __hash_combine(__res, hash<size_t>{}(__v.index()));
|
2016-12-03 07:00:05 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-28 00:23:19 +08:00
|
|
|
// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
|
|
|
|
// type whereas std::get will throw or returning nullptr. This makes it faster than
|
|
|
|
// std::get.
|
|
|
|
template <size_t _Ip, class _Vp>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
|
|
|
|
using __variant_detail::__access::__variant;
|
|
|
|
return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
|
|
|
|
return __unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class... _Types>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
|
|
|
|
return __unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_STD_VER > 14
|
2016-12-03 07:00:05 +08:00
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2017-11-19 12:57:22 +08:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_VARIANT
|