5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_VARIANT_VARIANT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_VARIANT_VARIANT_HPP_
22#include "arene/base/detail/exceptions.hpp"
25#include "arene/base/stdlib_choice/add_pointer.hpp"
26#include "arene/base/stdlib_choice/addressof.hpp"
27#include "arene/base/stdlib_choice/cstddef.hpp"
28#include "arene/base/stdlib_choice/decay.hpp"
29#include "arene/base/stdlib_choice/declval.hpp"
30#include "arene/base/stdlib_choice/enable_if.hpp"
31#include "arene/base/stdlib_choice/equal_to.hpp"
32#include "arene/base/stdlib_choice/forward.hpp"
33#include "arene/base/stdlib_choice/ignore.hpp"
34#include "arene/base/stdlib_choice/initializer_list.hpp"
35#include "arene/base/stdlib_choice/integral_constant.hpp"
36#include "arene/base/stdlib_choice/is_constructible.hpp"
37#include "arene/base/stdlib_choice/is_copy_assignable.hpp"
38#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
39#include "arene/base/stdlib_choice/is_default_constructible.hpp"
40#include "arene/base/stdlib_choice/is_move_assignable.hpp"
41#include "arene/base/stdlib_choice/is_move_constructible.hpp"
42#include "arene/base/stdlib_choice/is_reference.hpp"
43#include "arene/base/stdlib_choice/is_same.hpp"
44#include "arene/base/stdlib_choice/is_void.hpp"
45#include "arene/base/stdlib_choice/less.hpp"
46#include "arene/base/stdlib_choice/max_initializer_list_overload.hpp"
49#include "arene/base/stdlib_choice/move.hpp"
50#include "arene/base/stdlib_choice/remove_reference.hpp"
61#if ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED)
62#include "arene/base/variant/detail/bad_variant_access_exceptions_enabled.hpp"
64#include "arene/base/variant/detail/bad_variant_access_exceptions_disabled.hpp"
84template <
typename... Ts>
87namespace variant_detail {
94template <
typename... Ts>
95class variant_util_impl;
104inline constexpr void copy_construct_object(
bool selected,
void const* source,
void* dest)
noexcept(
109 construct_at(
static_cast<T*
>(dest), *
static_cast<T const*
>(source));
121inline constexpr void copy_assign_object(
bool selected,
void const* source,
void* dest)
noexcept(
126 *
static_cast<T*
>(dest) = *
static_cast<T const*
>(source);
138inline constexpr void move_construct_object(
bool selected,
void* source,
void* dest)
noexcept(
155inline constexpr void move_assign_object(
bool selected,
void* source,
void* dest)
noexcept(
171inline constexpr void destroy_object(
bool selected,
void* data)
noexcept {
173 destroy_at(
static_cast<T*
>(data));
186inline constexpr auto check_object_equality(
bool selected,
void const* lhs,
void const* rhs)
noexcept ->
bool {
191 return std::equal_to<T>{}(*
static_cast<T const*
>(lhs), *
static_cast<T const*
>(rhs));
203inline constexpr auto check_object_less_than(
bool selected,
void const* lhs,
void const* rhs)
noexcept ->
bool {
208 return std::less<T>{}(*
static_cast<T const*
>(lhs), *
static_cast<T const*
>(rhs));
215template <
typename T,
typename... Ts>
216class variant_util_impl<T, Ts...> {
224 static constexpr void
229 copy_construct_object<T>(current_idx == requested_idx, source, dest);
230 variant_util_impl<Ts...>::copy(current_idx + 1U, requested_idx, source, dest);
238 static constexpr void
242 copy_assign_object<T>(current_idx == requested_idx, source, dest);
243 variant_util_impl<Ts...>::copy_assign(current_idx + 1U, requested_idx, source, dest);
256 move_construct_object<T>(current_idx == requested_idx, source, dest);
257 variant_util_impl<Ts...>::move(current_idx + 1U, requested_idx, source, dest);
265 static constexpr void
269 move_assign_object<T>(current_idx == requested_idx, source, dest);
270 variant_util_impl<Ts...>::move_assign(current_idx + 1U, requested_idx, source, dest);
278 destroy_object<T>(current_idx == requested_idx, data);
279 variant_util_impl<Ts...>::destroy(current_idx + 1U, requested_idx, data);
288 static constexpr auto
289 check_equality(
std::size_t current_idx,
std::size_t requested_idx,
void const* lhs,
void const* rhs)
noexcept
291 return check_object_equality<T>(current_idx == requested_idx, lhs, rhs) ||
292 variant_util_impl<Ts...>::check_equality(current_idx + 1U, requested_idx, lhs, rhs);
302 static constexpr auto
303 check_less_than(
std::size_t current_idx,
std::size_t requested_idx,
void const* lhs,
void const* rhs)
noexcept
305 return check_object_less_than<T>(current_idx == requested_idx, lhs, rhs) ||
306 variant_util_impl<Ts...>::check_less_than(current_idx + 1U, requested_idx, lhs, rhs);
312class variant_util_impl<> {
320 static constexpr void
327 static constexpr void
339 static constexpr void
356 static constexpr auto check_equality(
359 void const*
const lhs,
360 void const*
const rhs
374 static constexpr auto check_less_than(
377 void const*
const lhs,
378 void const*
const rhs
394template <
typename... Ts>
402 inline static void copy(
406 )
noexcept(base::all_of_v<std::is_nothrow_copy_constructible<Ts>::value...>) {
407 variant_util_impl<Ts...>::copy(0U, idx, source, dest);
414 inline static void copy_assign(
418 )
noexcept(base::all_of_v<std::is_nothrow_copy_assignable<Ts>::value...>) {
419 variant_util_impl<Ts...>::copy_assign(0U, idx, source, dest);
427 inline static void move(
431 )
noexcept(base::all_of_v<std::is_nothrow_move_constructible<Ts>::value...>) {
432 variant_util_impl<Ts...>::move(0U, idx, source, dest);
439 inline static void move_assign(
443 )
noexcept(base::all_of_v<std::is_nothrow_move_assignable<Ts>::value...>) {
444 variant_util_impl<Ts...>::move_assign(0U, idx, source, dest);
450 inline static void destroy(
std::size_t idx,
void* data)
noexcept { variant_util_impl<Ts...>::destroy(0U, idx, data); }
458 inline static auto check_equality(
std::size_t idx,
void const* lhs,
void const* rhs)
noexcept ->
bool {
459 return variant_util_impl<Ts...>::check_equality(0U, idx, lhs, rhs);
468 inline static auto check_less_than(
std::size_t idx,
void const* lhs,
void const* rhs)
noexcept ->
bool {
469 return variant_util_impl<Ts...>::check_less_than(0U, idx, lhs, rhs);
476template <
class Variant, std::
size_t I = 0>
477class convertible_checker;
481template <std::
size_t I>
482class convertible_checker<variant<>, I> {
488 static void run() =
delete;
501class convertible_checker<variant<H, Ts...>, I> : convertible_checker<variant<Ts...>, I + 1> {
503 using convertible_checker<variant<Ts...>, I + 1>::run;
508 static auto run(H
const&, std::integral_constant<std::size_t, I>) -> std::integral_constant<std::size_t, I>;
514 static auto run(H&&, std::integral_constant<std::size_t, I>) -> std::integral_constant<std::size_t, I>;
521template <
class Variant,
class T,
class =
void>
522class accepted_index {};
527template <
class Variant,
class T>
528class accepted_index<Variant, T,
void_t<decltype(convertible_checker<Variant>::run(std::declval<T>(), {}))>>
529 :
public decltype(convertible_checker<Variant>::run(std::declval<T>(), {})) {};
537template <
typename... Ts>
543 using util_type = variant_detail::variant_util<Ts...>;
549 template <std::
size_t Index>
553 static_assert(
sizeof...(Ts) > 0,
"Empty variant is not allowed");
558 static constexpr std::size_t num_types{
sizeof...(Ts)};
596 using copy_ctor_arg =
603 using dummy_copy_ctor_source =
632 using move_ctor_arg =
639 using dummy_move_ctor_source =
649 typename T0 = nth_type<0>,
651 variant() noexcept(
std::is_nothrow_default_constructible<T0>::value)
657 construct_at(
reinterpret_cast<T0*
>(data_.
data()));
671 util_type::copy(other.idx_, other.data_.data(), data_.
data());
680 variant(dummy_copy_ctor_source
const& other) =
delete;
696 util_type::move(other.idx_, other.data_.data(), data_.
data());
708 variant(dummy_move_ctor_source&& other) =
delete;
723 std::size_t I = variant_detail::accepted_index<variant, T>::type::value,
724 typename Tj = nth_type<I>,
801 if (
index() != rhs.index()) {
803 util_type::copy(rhs.idx_, rhs.data_.data(), data_.data());
806 util_type::copy_assign(rhs.idx_, rhs.data_.data(), data_.data());
832 if (
index() != rhs.index()) {
834 util_type::move(rhs.idx_, rhs.data_.data(), data_.data());
837 util_type::move_assign(rhs.idx_, rhs.data_.data(), data_.data());
867 if (lhs.index() != rhs.index()) {
872 return util_type::check_equality(
874 static_cast<void const*
>(lhs.data_.data()),
875 static_cast<void const*
>(rhs.data_.data())
891 if (lhs.index() != rhs.index()) {
892 return (lhs.index() + 1U) < (rhs.index() + 1U);
896 return util_type::check_less_than(
898 static_cast<void const*
>(lhs.data_.data()),
899 static_cast<void const*
>(rhs.data_.data())
908 auto index() const noexcept ->
std::
size_t {
return idx_; }
923 using selected_type = nth_type<I>;
927 construct_at(
reinterpret_cast<selected_type*
>(data_.data()),
std::forward<Args>(args)...);
943 template <
std::size_t I,
typename U,
typename... Args>
948 using selected_type = nth_type<I>;
952 construct_at(
reinterpret_cast<selected_type*
>(data_.data()), init,
std::forward<Args>(args)...);
965 template <
typename T,
typename... Args>
985 template <
typename T,
typename U,
typename... Args>
1005 template <
typename T>
1006 auto get() &
noexcept(!detail::are_exceptions_enabled_v) -> T& {
1016 template <
typename T>
1017 auto get() const& noexcept(!detail::are_exceptions_enabled_v) -> T const& {
1028 template <
typename T>
1029 auto get() &&
noexcept(!detail::are_exceptions_enabled_v) -> T&& {
1039 template <
typename T>
1040 auto get() const&& noexcept(!detail::are_exceptions_enabled_v) -> T const&& {
1053 template <std::
size_t Index>
1054 auto get() &
noexcept(!detail::are_exceptions_enabled_v) -> nth_type<Index>& {
1055 return get_impl<Index, nth_type<Index>&>(*this);
1065 template <std::
size_t Index>
1066 auto get() const& noexcept(!detail::are_exceptions_enabled_v) -> nth_type<Index> const& {
1067 return get_impl<Index, nth_type<Index>
const&>(*this);
1077 template <std::
size_t Index>
1078 auto get() &&
noexcept(!detail::are_exceptions_enabled_v) -> nth_type<Index>&& {
1079 return get_impl<Index, nth_type<Index>&&>(
std::move(*
this));
1089 template <std::
size_t Index>
1090 auto get() const&& noexcept(!detail::are_exceptions_enabled_v) -> nth_type<Index> const&& {
1092 return get_impl<Index, nth_type<Index>
const&&>(
std::move(*
this));
1118 template <std::
size_t Index,
typename AlternativeRefT,
typename Self>
1119 static auto get_impl(Self&& self)
noexcept(!detail::are_exceptions_enabled_v) -> AlternativeRefT {
1120 variant_detail::variant_must_have_value(Index, self.idx_);
1123 "-Wstrict-aliasing",
1124 "It's safe to use reinterpret_cast here since the check above ensures that the type of the object "
1125 "currently held by the variant is the same as the type requested."
1127 return ::arene::base::forward_like<Self>(
1136 void destroy() noexcept {
1138 util_type::destroy(idx_, data_.
data());
1145 alignas(max_align) array<byte, max_size> data_{};
1158template <
typename T,
typename... Ts>
1161 return var.template
get<T>();
1172template <
typename T,
typename... Ts>
1173inline constexpr auto get(
variant<Ts...> const& var)
noexcept(!detail::are_exceptions_enabled_v) -> T
const& {
1175 return var.template
get<T>();
1186template <
typename T,
typename... Ts>
1200template <
typename T,
typename... Ts>
1201inline constexpr auto get(
variant<Ts...> const&& var)
noexcept(!detail::are_exceptions_enabled_v) -> T
const&& {
1221constexpr auto get(variant<Ts...>& var)
noexcept(!detail::are_exceptions_enabled_v)
1223 static_assert(Index <
sizeof...(Ts),
"The index should be in [0, number of alternatives)");
1240constexpr auto get(variant<Ts...>
const& var)
noexcept(!detail::are_exceptions_enabled_v)
1242 static_assert(Index <
sizeof...(Ts),
"The index should be in [0, number of alternatives)");
1260constexpr auto get(
variant<Ts...>&& var)
noexcept(!detail::are_exceptions_enabled_v)
1262 static_assert(Index <
sizeof...(Ts),
"The index should be in [0, number of alternatives)");
1279constexpr auto get(
variant<Ts...> const&& var)
noexcept(!detail::are_exceptions_enabled_v)
1281 static_assert(Index <
sizeof...(Ts),
"The index should be in [0, number of alternatives)");
1296template <
class T,
class... Ts>
1311 static_assert(Index <
sizeof...(Ts),
"The index should be in [0, number of alternatives)");
1313 if ((ptr !=
nullptr) && (ptr->index() == Index)) {
1328 static_assert(Index <
sizeof...(Ts),
"The index should be in [0, number of alternatives)");
1330 if ((ptr !=
nullptr) && (ptr->index() == Index)) {
1344template <
typename T,
typename... Ts>
1357template <
typename T,
typename... Ts>
Provides a backport of std::void_t.
constexpr auto data() noexcept -> T *
Obtain a pointer to the first element of the array.
Definition array.hpp:145
constexpr full_ordering_operators_from_less_than_and_equals() noexcept=default
backport of std::in_place_index_t<size_t> from C++17
Definition in_place.hpp:54
A special dummy class that cannot be constructed. It can be used as a substitute argument for copy/mo...
Definition non_constructible_dummy.hpp:16
Forward declaration of variant.
Definition variant.hpp:540
~variant()
Calls the destructor of the alternative type.
Definition variant.hpp:1104
auto get() const &&noexcept(!detail::are_exceptions_enabled_v) -> T const &&
Get by type.
Definition variant.hpp:1040
constexpr auto valueless_by_exception() const noexcept -> bool
Check if the variant is valueless.
Definition variant.hpp:1101
variant(dummy_move_ctor_source &&other)=delete
Move Constructor form another variant.
auto get() &&noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > &&
Get the object stored in this variant by the index.
Definition variant.hpp:1078
auto operator=(copy_assignment_arg const &rhs) noexcept(base::all_of_v< std::is_nothrow_copy_constructible< Ts >::value... > &&base::all_of_v< std::is_nothrow_copy_assignable< Ts >::value... >) -> variant &
Copy Assignment operator from another variant of the same alternative types.
Definition variant.hpp:797
variant(copy_ctor_arg const &other) noexcept(arene::base::all_of_v< std::is_nothrow_copy_constructible< Ts >::value... >)
Copy Constructor from another variant of the same alternative types.
Definition variant.hpp:667
variant(T &&value) noexcept(std::is_nothrow_constructible< Tj, T >::value)
Constructor that initializes this variant from one of its alternative types.
Definition variant.hpp:732
variant(in_place_index_t< I >, Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, Args... >::value)
Constructor that initializes this variant from a specified alternative index.
Definition variant.hpp:754
variant() noexcept(std::is_nothrow_default_constructible< T0 >::value)
Default constructor, which initializes the variant to a default constructed instance of its 0'th alte...
Definition variant.hpp:651
auto operator=(dummy_copy_assign_source const &rhs) -> variant &=delete
Copy Assignment operator from another variant of the same alternative types.
auto emplace(Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, Args... >::value) -> nth_type< I > &
Emplaces a value into the variant by index.
Definition variant.hpp:921
auto get() const &&noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > const &&
Get the object stored in this variant by the index.
Definition variant.hpp:1090
variant(move_ctor_arg &&other) noexcept(base::all_of_v< std::is_nothrow_move_constructible< Ts >::value... >)
Move Constructor from another variant.
Definition variant.hpp:692
auto get() &&noexcept(!detail::are_exceptions_enabled_v) -> T &&
Get by type.
Definition variant.hpp:1029
variant(in_place_index_t< I >, std::initializer_list< U > init_list, Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, std::initializer_list< U > &, Args... >::value)
Constructor that initializes this variant from a specified alternative index via an initializer list.
Definition variant.hpp:781
auto emplace(std::initializer_list< U > init, Args &&... args) noexcept(std::is_nothrow_constructible< T, std::initializer_list< U >, Args... >::value) -> T &
Emplaces a value into the variant by type.
Definition variant.hpp:986
auto get() &noexcept(!detail::are_exceptions_enabled_v) -> T &
Get by type.
Definition variant.hpp:1006
auto emplace(Args &&... args) noexcept(std::is_nothrow_constructible< T, Args... >::value) -> T &
Emplaces a value into the variant by type.
Definition variant.hpp:966
friend auto operator<(variant const &lhs, variant const &rhs) noexcept -> bool
Less-than comparison.
Definition variant.hpp:890
auto emplace(std::initializer_list< U > init, Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, std::initializer_list< U >, Args... >::value) -> nth_type< I > &
Emplaces a value into the variant by index.
Definition variant.hpp:944
friend auto operator==(variant const &lhs, variant const &rhs) noexcept -> bool
Equality comparison.
Definition variant.hpp:866
variant(dummy_copy_ctor_source const &other)=delete
Copy Constructor from another variant of the same alternative types.
auto get() &noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > &
Get the object stored in this variant by the index.
Definition variant.hpp:1054
auto operator=(dummy_move_assign_source &&rhs) -> variant &=delete
Move Assignment operator from another variant of the same alternative types.
auto get() const &noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > const &
Get the object stored in this variant by the index.
Definition variant.hpp:1066
auto operator=(move_assignment_arg &&rhs) noexcept(base::all_of_v< std::is_nothrow_move_constructible< Ts >::value... > &&base::all_of_v< std::is_nothrow_move_assignable< Ts >::value... >) -> variant &
Move Assignment operator from another variant of the same alternative types.
Definition variant.hpp:828
auto get() const &noexcept(!detail::are_exceptions_enabled_v) -> T const &
Get by type.
Definition variant.hpp:1017
auto index() const noexcept -> std::size_t
Get the index of the alternative type that this variant currently holds.
Definition variant.hpp:908
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if a type is constructible from arguments of the specified types and is noexcept...
Definition is_constructible.hpp:84
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
#define ARENE_IGNORE_ALL(s, comment)
Pushes an ignore for a diagnostic into the compiler for any compiler.
Definition diagnostics.hpp:122
Implementation of a backport of std::forward_like.
Provides backports of std::in_place_t, std::in_place_type_t, and std::in_place_index_t from C++17.
typename at< L0, Index >::type at_t
Get the n-th element from a type list.
Definition at.hpp:113
Definition accumulate.hpp:37
constexpr auto move(InIterator first, InIterator last, OutIterator output) noexcept(noexcept(*first++) &&noexcept(output++) &&//CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) noexcept(first !=last) &&//CODEQLFP(A5-2-6) std::is_nothrow_assignable<, typename std::iterator_traits< InIterator >::value_type >::value) -> OutIterator
Moves the elements in the range [first, last), to another range beginning at output,...
Definition move.hpp:46
typename static_if< B >::template then_else< T, F > conditional_t
lazily select between two types based on a boolean condition
Definition conditional.hpp:28
constexpr std::size_t variant_npos
Special value used as the return value of variant::index() when variant::valueless_by_exception() is ...
Definition traits.hpp:34
constexpr auto copy(InputIterator first, InputIterator last, OutputIterator output) noexcept(noexcept(*(++output)) &&noexcept(*(++first)) &&noexcept(first !=last) &&std::is_nothrow_assignable<, typename std::iterator_traits< InputIterator >::reference >::value) -> OutputIterator
Copies the elements in the range, defined by [first, last), to another range beginning at output.
Definition copy.hpp:40
void void_t
An alias for void that accepts any number of type parameters, used for substitution checks.
Definition substitution_succeeds.hpp:17
constexpr auto get_if(variant< Ts... > *ptr) noexcept -> std::add_pointer_t< variant_alternative_t< Index, variant< Ts... > > >
Attempt to get a pointer to the object held by the variant by index.
Definition variant.hpp:1308
typename variant_alternative< I, T >::type variant_alternative_t
Helper type of variant_size variant_alternative.
Definition traits.hpp:84
constexpr bool all_of_v
Trait which tests if every value in a parameter pack is true .
constexpr auto holds_alternative(variant< Ts... > const &var) noexcept -> bool
Check if the variant holds a value of a given alternative type.
Definition variant.hpp:1297
constexpr auto get(array< T, N > &arr) noexcept -> T &
Get a reference to the element at the specified index in the array.
Definition array.hpp:885
constexpr auto index_of_v
Template meta-function for querying the 0-based index of 'T' within a list of types,...
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
constexpr auto max(initializer_list< T > list, Compare comp) noexcept(noexcept(comp(std::declval< T & >(), std::declval< T & >())) &&std::is_nothrow_copy_constructible_v< T >) -> T
Returns the largest value in the std::initializer_list.
Definition max_initializer_list_overload.hpp:49
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
constexpr auto addressof(T &value) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:24
typename remove_reference< T >::type remove_reference_t
An alias for T, but with top-level reference qualifiers removed.
Definition remove_reference.hpp:47
typename add_pointer< Type >::type add_pointer_t
Adds a pointer to the given type.
Definition add_pointer.hpp:62
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24
an empty struct that is a list of types - cheaper to instantiate than tuple
Definition type_list.hpp:19
Provides the all_of_v<bool...> trait.