3#ifndef TETL_VARIANT_VARIANT2_HPP
4#define TETL_VARIANT_VARIANT2_HPP
72inline constexpr auto is_in_place_index =
false;
75inline constexpr auto is_in_place_index<in_place_index_t<I>> =
true;
78inline constexpr auto is_in_place_type =
false;
81inline constexpr auto is_in_place_type<in_place_type_t<T>> =
true;
83constexpr auto make_variant_compare_op(
auto op)
85 return [op](
auto const& l,
auto const& r) ->
bool {
97template <
typename... Ts>
114 template <
typename T>
118 and not detail::is_in_place_index<remove_cvref_t<T>>
119 and not detail::is_in_place_type<remove_cvref_t<T>>
124 :
variant(in_place_type<detail::variant_alternative_selector_t<T, Ts...>>,
etl::
forward<T>(t))
128 template <
size_t I,
typename... Args>
131 : _index(static_cast<index_type>(I))
136 template <
typename T,
typename... Args>
147 :
variant(other, copy_move_tag{})
164 (... and detail::variant_copy_assignable<Ts>) and not(... and detail::variant_trivially_copy_assignable<Ts>)
175 (... and detail::variant_move_assignable<Ts>) and !(... and detail::variant_trivially_move_assignable<Ts>)
181 template <
typename T>
183 constexpr auto operator=(T&& t)
noexcept(
199 [[nodiscard]]
constexpr auto index() const noexcept ->
size_t {
return static_cast<size_t>(_index); }
206 static_assert(I <
sizeof...(Ts));
208 return _union[
index];
216 static_assert(I <
sizeof...(Ts));
218 return _union[
index];
226 static_assert(I <
sizeof...(Ts));
236 static_assert(I <
sizeof...(Ts));
241 template <
typename T,
typename... Args>
243 constexpr auto emplace(Args&&... args) ->
auto&
249 template <
size_t I,
typename... Args>
251 constexpr auto emplace(Args&&... args) ->
auto&
263 if (lhs.index() != rhs.index()) {
276 if (lhs.index() < rhs.index()) {
279 if (lhs.index() > rhs.index()) {
293 if (lhs.index() < rhs.index()) {
296 if (lhs.index() > rhs.index()) {
310 if (lhs.index() > rhs.index()) {
313 if (lhs.index() < rhs.index()) {
327 if (lhs.index() > rhs.index()) {
330 if (lhs.index() < rhs.index()) {
338 struct copy_move_tag { };
340 template <
typename Other>
341 constexpr variant(Other&& other, [[maybe_unused]] copy_move_tag tag)
343 , _union(uninitialized_union())
346 replace(param.index,
etl::move(param).value());
350 template <
typename Other>
351 constexpr auto assign(Other&& other) ->
void
354 if constexpr (lhs.index == rhs.index) {
363 template <
typename... Args>
364 constexpr auto replace(
auto index, Args&&... args) ->
auto&
367 _index =
static_cast<index_type
>(index.value);
368 return (*
this)[index];
371 constexpr auto destroy() ->
void
383template <
typename T,
typename... Ts>
393template <
size_t I,
typename... Ts>
396 static_assert(I <
sizeof...(Ts));
404template <
size_t I,
typename... Ts>
407 static_assert(I <
sizeof...(Ts));
415template <
size_t I,
typename... Ts>
418 static_assert(I <
sizeof...(Ts));
426template <
size_t I,
typename... Ts>
429 static_assert(I <
sizeof...(Ts));
438template <
size_t I,
typename... Ts>
441 static_assert(I <
sizeof...(Ts));
442 if (pv ==
nullptr or pv->index() != I) {
452template <
size_t I,
typename... Ts>
456 static_assert(I <
sizeof...(Ts));
457 if (pv ==
nullptr or pv->index() != I) {
465template <
typename T,
typename... Ts>
473template <
typename T,
typename... Ts>
#define TETL_TRIVIAL_ABI
Definition attributes.hpp:47
#define TETL_NO_UNIQUE_ADDRESS
Definition attributes.hpp:41
#define TETL_PRECONDITION(...)
Definition check.hpp:16
constexpr auto replace(ForwardIt first, ForwardIt last, T const &oldValue, T const &newValue) -> void
Replaces all elements satisfying specific criteria with new_value in the range [first,...
Definition replace.hpp:15
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
conditional_t<(N< static_cast< unsigned char >(-1)), unsigned char, conditional_t<(N< static_cast< unsigned short >(-1)), unsigned short, conditional_t<(N< static_cast< unsigned int >(-1)), unsigned int, conditional_t<(N< static_cast< unsigned long >(-1)), unsigned long, unsigned long long > > > > smallest_size_t
Smallest unsigned integer type that can represent values in the range [0, N].
Definition smallest_size_t.hpp:13
constexpr auto visit(F &&f, Vs &&... vs)
Applies the visitor vis (Callable that can be called with any combination of types from variants) to ...
Definition visit.hpp:177
constexpr auto visit_with_index(F &&f, Vs &&... vs)
Applies the visitor vis (Callable that can be called with any combination of types from variants) to ...
Definition visit.hpp:149
constexpr struct etl::ranges::destroy_fn destroy
Definition adjacent_find.hpp:8
constexpr bool is_copy_constructible_v
Definition is_copy_constructible.hpp:30
integral_constant< size_t, I > index_constant
Definition index_constant.hpp:12
constexpr bool is_constructible_v
Definition is_constructible.hpp:24
constexpr bool is_nothrow_copy_constructible_v
Definition is_nothrow_copy_constructible.hpp:27
constexpr auto addressof(T &arg) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:15
constexpr bool is_nothrow_default_constructible_v
Definition is_nothrow_default_constructible.hpp:26
constexpr bool is_nothrow_copy_assignable_v
Definition is_nothrow_copy_assignable.hpp:28
constexpr bool is_nothrow_move_constructible_v
Definition is_nothrow_move_constructible.hpp:20
constexpr auto index_v
Definition index_constant.hpp:15
constexpr bool is_move_constructible_v
Definition is_move_constructible.hpp:20
constexpr bool is_trivially_move_assignable_v
Definition is_trivially_move_assignable.hpp:29
constexpr bool is_trivially_copy_assignable_v
Definition is_trivially_copy_assignable.hpp:29
constexpr bool is_default_constructible_v
Definition is_default_constructible.hpp:26
constexpr bool is_trivially_destructible_v
Definition is_trivially_destructible.hpp:33
constexpr bool is_assignable_v
Definition is_assignable.hpp:20
typename variant_alternative< I, T >::type variant_alternative_t
Definition variant_alternative.hpp:21
constexpr auto construct_at(T *p, Args &&... args) -> T *
Creates a T object initialized with arguments args... at given address p.
Definition construct_at.hpp:38
constexpr auto unchecked_get(variant< Ts... > &v) -> auto &
constexpr bool is_trivially_copy_constructible_v
Definition is_trivially_copy_constructible.hpp:27
constexpr bool is_same_v
Definition is_same.hpp:11
constexpr auto is_trivially_move_constructible_v
Definition is_trivially_move_constructible.hpp:20
auto unreachable() -> void
Definition unreachable.hpp:10
constexpr bool is_nothrow_constructible_v
Definition is_nothrow_constructible.hpp:50
constexpr bool is_nothrow_assignable_v
Definition is_nothrow_assignable.hpp:24
constexpr bool is_nothrow_move_assignable_v
Definition is_nothrow_move_assignable.hpp:28
constexpr auto get_if(variant< Types... > *pv) noexcept -> add_pointer_t< T >
constexpr auto destroy_at(T *p) -> void
If T is not an array type, calls the destructor of the object pointed to by p, as if by p->~T()....
Definition destroy_at.hpp:16
constexpr auto forward(remove_reference_t< T > ¶m) noexcept -> T &&
Forwards lvalues as either lvalues or as rvalues, depending on T. When t is a forwarding reference (a...
Definition forward.hpp:18
constexpr bool is_move_assignable_v
Definition is_move_assignable.hpp:29
constexpr bool is_copy_assignable_v
Definition is_copy_assignable.hpp:28
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:14
Function object for performing comparisons. Unless specialised, invokes operator>= on type T....
Definition greater_equal.hpp:14
Function object for performing comparisons. Unless specialised, invokes operator> on type T....
Definition greater.hpp:14
Disambiguation tags that can be passed to the constructors of etl::optional, etl::variant,...
Definition in_place_index.hpp:19
Disambiguation tags that can be passed to the constructors of etl::optional, etl::variant,...
Definition in_place_type.hpp:19
Function object for performing comparisons. Unless specialised, invokes operator<= on type T....
Definition less_equal.hpp:14
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:14
Definition variant.hpp:98
constexpr variant(variant const &)=default
constexpr variant(in_place_type_t< T >, Args &&... args)
Definition variant.hpp:138
constexpr auto get_if(variant< Ts... > *pv) noexcept -> add_pointer_t< variant_alternative_t< I, variant< Ts... > > >
If pv is not a null pointer and pv->index() == I, returns a pointer to the value stored in the varian...
Definition variant.hpp:439
friend constexpr auto operator==(variant const &lhs, variant const &rhs) -> bool
Equality operator.
Definition variant.hpp:261
constexpr auto emplace(Args &&... args) -> auto &
Definition variant.hpp:251
friend constexpr auto operator<=(variant const &lhs, variant const &rhs) -> bool
Less-equal operator.
Definition variant.hpp:291
constexpr variant(in_place_index_t< I >, Args &&... args)
Definition variant.hpp:130
constexpr auto unchecked_get(variant< Ts... > const &&v) -> auto const &&
Returns a reference to the object stored in the variant.
Definition variant.hpp:427
constexpr auto operator=(variant &&other) noexcept((... and is_nothrow_move_assignable_v< Ts >) and(... and is_nothrow_move_constructible_v< Ts >)) -> variant &requires((... and detail::variant_move_assignable< Ts >) and !(... and detail::variant_trivially_move_assignable< Ts >))
Definition variant.hpp:172
constexpr auto get_if(variant< Ts... > *pv) noexcept -> add_pointer_t< T >
Type-based non-throwing accessor: The call is ill-formed if T is not a unique element of Ts....
Definition variant.hpp:466
constexpr variant(T &&t) noexcept(is_nothrow_constructible_v< detail::variant_alternative_selector_t< T, Ts... >, T >)
Definition variant.hpp:123
constexpr auto operator[](index_constant< I > index) &-> auto &
Returns a reference to the object stored in the variant.
Definition variant.hpp:204
constexpr auto operator=(variant const &other) noexcept((... and is_nothrow_copy_assignable_v< Ts >) and(... and is_nothrow_copy_constructible_v< Ts >)) -> variant &requires((... and detail::variant_copy_assignable< Ts >) and not(... and detail::variant_trivially_copy_assignable< Ts >))
Definition variant.hpp:161
constexpr auto unchecked_get(variant< Ts... > &v) -> auto &
Returns a reference to the object stored in the variant.
Definition variant.hpp:394
constexpr auto operator[](index_constant< I > index) &&-> auto &&
Returns a reference to the object stored in the variant.
Definition variant.hpp:224
constexpr auto operator=(variant &&) -> variant &=default
constexpr auto unchecked_get(variant< Ts... > &&v) -> auto &&
Returns a reference to the object stored in the variant.
Definition variant.hpp:416
constexpr auto emplace(Args &&... args) -> auto &
Definition variant.hpp:243
constexpr variant(variant &&other) noexcept((... and is_nothrow_move_constructible_v< Ts >))
Definition variant.hpp:153
constexpr auto index() const noexcept -> size_t
Returns the zero-based index of the alternative that is currently held by the variant.
Definition variant.hpp:199
constexpr auto get_if(variant< Ts... > const *pv) noexcept -> add_pointer_t< T const >
Type-based non-throwing accessor: The call is ill-formed if T is not a unique element of Ts....
Definition variant.hpp:474
constexpr auto operator[](index_constant< I > index) const &-> auto const &
Returns a reference to the object stored in the variant.
Definition variant.hpp:214
friend constexpr auto operator<(variant const &lhs, variant const &rhs) -> bool
Less-than operator.
Definition variant.hpp:274
constexpr auto holds_alternative(variant< Ts... > const &v) noexcept -> bool
Checks if the variant v holds the alternative T. The call is ill-formed if T does not appear exactly ...
Definition variant.hpp:384
constexpr auto operator[](index_constant< I > index) const &&-> auto const &&
Returns a reference to the object stored in the variant.
Definition variant.hpp:234
constexpr auto operator=(variant const &) -> variant &=default
constexpr auto unchecked_get(variant< Ts... > const &v) -> auto const &
Returns a reference to the object stored in the variant.
Definition variant.hpp:405
friend constexpr auto operator>=(variant const &lhs, variant const &rhs) -> bool
Greater-equal operator.
Definition variant.hpp:325
constexpr ~variant()
Definition variant.hpp:196
constexpr variant() noexcept(is_nothrow_default_constructible_v< first_type >)
Definition variant.hpp:107
friend constexpr auto operator>(variant const &lhs, variant const &rhs) -> bool
Greater-than operator.
Definition variant.hpp:308
constexpr variant(variant const &other) noexcept((... and etl::is_nothrow_copy_constructible_v< Ts >))
Definition variant.hpp:145
constexpr variant(variant &&)=default
constexpr auto get_if(variant< Ts... > const *pv) noexcept -> add_pointer_t< variant_alternative_t< I, variant< Ts... > > const >
If pv is not a null pointer and pv->index() == I, returns a pointer to the value stored in the varian...
Definition variant.hpp:453