|
RPNX::DataStructures
Header-only C++ data structures and supporting utilities.
|
Heap-backed tagged union with allocator-aware storage. More...
#include <variant.hpp>
Public Types | |
| using | allocator_type = Allocator |
| Allocator type used by this variant. | |
Public Member Functions | |
| constexpr | basic_variant (const allocator_type &alloc=allocator_type()) |
| Default-constructs the first alternative. | |
| constexpr | basic_variant (basic_variant< Allocator, Ts... > &&other) noexcept(std::is_nothrow_move_constructible_v< Allocator >) |
| Move-constructs from another variant of the same type. | |
| constexpr | basic_variant (basic_variant< Allocator, Ts... > const &other) |
| Copy-constructs from another variant of the same type. | |
| ~basic_variant () | |
| Destroys the currently-held value if present. | |
| void | reset () |
| Resets the variant to the valueless state. | |
| template<typename... Ts2> | |
| basic_variant (basic_variant< Allocator, Ts2... > const &other, std::enable_if_t< !std::is_same_v< basic_variant< Allocator, Ts... >, basic_variant< Allocator, Ts2... > > &&!has_cvref_removed_identical_type< basic_variant< Allocator, Ts2... > >(), int >=0) | |
| Converting copy constructor from another compatible variant type. | |
| template<typename T2> | |
| constexpr | basic_variant (T2 &&value, const allocator_type &alloc=allocator_type(), std::enable_if_t< rpnx::basic_variant< Allocator, Ts... >::can_construct_subtype_with< T2 >(), int >=0) |
| Constructs the variant from a value convertible to one of the alternatives. | |
| template<typename T, std::enable_if_t< can_construct_subtype_with< T >(), int > = 0> | |
| constexpr basic_variant< Allocator, Ts... > & | operator= (T &&value) |
| Assigns from a value convertible to one of the alternatives. | |
| basic_variant< Allocator, Ts... > & | operator= (basic_variant< Allocator, Ts... > other) |
| Copy-assigns by value using swap semantics. | |
| template<typename T> | |
| T & | static_cast_as () |
Returns the held value cast to T& via visitor dispatch. | |
| template<typename T> | |
| T const & | static_cast_as () const |
Returns the held value cast to T const& via visitor dispatch. | |
| template<typename T> | |
| T & | get_as () |
Retrieves the held value as T& with runtime type checking. | |
| template<typename T> | |
| T & | as () |
| Alias of get_as(). | |
| template<typename T> | |
| T & | unwrap () |
| Alias of get_as(). | |
| template<typename T> | |
| T & | unwrap_unchecked () |
Retrieves the held value as T& without runtime checks. | |
| template<typename T> | |
| T const & | unwrap_unchecked () const |
Retrieves the held value as T const& without runtime checks. | |
| template<typename T> | |
| T const & | get_as () const |
Retrieves the held value as T const& with runtime type checking. | |
| template<typename T> | |
| T const & | as () const |
| Const alias of get_as(). | |
| template<typename T> | |
| T const & | unwrap () const |
| Const alias of get_as(). | |
| template<std::size_t N> | |
| auto const & | get_n () const |
| Retrieves the held value by alternative index with runtime checking. | |
| template<std::size_t N> | |
| auto const & | get_n_unchecked () const |
| Retrieves the held value by alternative index without runtime checking. | |
| bool | operator== (basic_variant< Allocator, Ts... > const &other) const |
| Equality comparison. | |
| bool | operator!= (basic_variant< Allocator, Ts... > const &other) const |
| Inequality comparison. | |
| bool | operator< (basic_variant< Allocator, Ts... > const &other) const |
| Strict-weak ordering comparison. | |
| std::strong_ordering | operator<=> (basic_variant< Allocator, Ts... > const &other) const |
| Three-way comparison. | |
| template<typename T> | |
| bool | type_is () const |
Checks whether the active alternative is exactly T. | |
| template<typename... Ts2> | |
| bool | type_any_of () const |
Checks whether the active alternative is any of Ts2. | |
| template<typename R, dispatch_type D = dispatch_type::automatic, typename F> | |
| R | apply_visitor (F &&func) & |
| Applies a visitor to the active alternative. | |
| template<typename R, dispatch_type D = dispatch_type::automatic, typename F> | |
| R | apply_visitor (F &&func) const & |
| Const lvalue overload of apply_visitor(). | |
| template<typename R, dispatch_type D = dispatch_type::automatic, typename F> | |
| R | apply_visitor (F &&func) && |
| Rvalue overload of apply_visitor(). | |
| template<typename R, typename F> | |
| R | apply_visitor_checked (F &&func) & |
| Applies a visitor and throws if not invocable for the active alternative. | |
| template<typename R, typename F> | |
| R | apply_visitor_checked (F &&func) const & |
| Const lvalue overload of apply_visitor_checked(). | |
| template<typename R, typename F> | |
| R | apply_visitor_checked (F &&func) && |
| Rvalue overload of apply_visitor_checked(). | |
| template<typename R, typename F> | |
| R | try_apply_visitor (F &&func) & |
Applies a visitor and returns default R{} when not invocable for the active type. | |
| template<typename R, typename F> | |
| R | try_apply_visitor (F &&func) const & |
| Const lvalue overload of try_apply_visitor(). | |
| template<typename R, typename F> | |
| R | try_apply_visitor (F &&func) && |
| Rvalue overload of try_apply_visitor(). | |
| template<typename T, typename F> | |
| bool | match (F &&func) |
Invokes func with the held value if it is of type T. | |
| template<typename T, typename F> | |
| bool | test (F &&func) |
Invokes func with the held value if it is of type T and returns predicate result. | |
| template<typename T, typename F> | |
| bool | match (F &&func) const |
| Const overload of match(). | |
| template<typename T, typename F> | |
| bool | test (F &&func) const |
| Const overload of test(). | |
| template<typename T> | |
| T * | cast_ptr () |
Returns pointer to held value if active type is T. | |
| template<typename T> | |
| T const * | cast_ptr () const |
| Const overload of cast_ptr(). | |
| std::type_info const & | type () const |
| Returns RTTI for the active alternative. | |
| std::type_index | type_index () const |
Returns std::type_index for the active alternative. | |
| template<std::size_t N> | |
| auto & | get_n () |
| Retrieves the held value by alternative index with runtime checking. | |
| template<std::size_t N> | |
| auto & | get_n_unchecked () |
| Retrieves the held value by alternative index without runtime checking. | |
| std::size_t | index () const |
| Returns the active alternative index. | |
Static Public Member Functions | |
| template<typename T> | |
| static consteval bool | can_construct_subtype_with () |
Indicates whether this variant can be constructed from T. | |
Heap-backed tagged union with allocator-aware storage.
The active alternative is tracked by a runtime index and each stored value is allocated through Allocator rebound to the concrete held type.
| Allocator | Allocator used to allocate and destroy held values. |
| Ts | Variant alternative types. |
|
inlineconstexpr |
Default-constructs the first alternative.
| alloc | Allocator instance used for internal allocations. |
| Any | exception thrown by allocating or constructing Ts[0]. |
|
inlineconstexprnoexcept |
Move-constructs from another variant of the same type.
| other | Source variant to move from. |
|
inlineconstexpr |
Copy-constructs from another variant of the same type.
| other | Source variant to copy from. |
| Any | exception thrown by allocation or copy construction of the held value. |
|
inline |
Converting copy constructor from another compatible variant type.
| Ts2 | Source variant alternatives. |
| other | Source variant. |
| Any | exception thrown while assigning the converted held value. |
|
inlineconstexpr |
Constructs the variant from a value convertible to one of the alternatives.
| T2 | Source type. |
| value | Value used to initialize the held alternative. |
| alloc | Allocator instance used for internal allocations. |
| Any | exception thrown by allocation or construction of the selected alternative. |
|
inline |
Applies a visitor to the active alternative.
| R | Visitor return type. |
| D | Dispatch policy. |
| F | Visitor type. |
| func | Visitor callable. |
|
inline |
Rvalue overload of apply_visitor().
| R | Visitor return type. |
| D | Dispatch policy. |
| F | Visitor type. |
| func | Visitor callable. |
|
inline |
Const lvalue overload of apply_visitor().
| R | Visitor return type. |
| D | Dispatch policy. |
| F | Visitor type. |
| func | Visitor callable. |
|
inline |
Applies a visitor and throws if not invocable for the active alternative.
| R | Visitor return type. |
| F | Visitor type. |
| func | Visitor callable. |
| std::bad_variant_access | If func cannot be called for the active type. |
|
inline |
Rvalue overload of apply_visitor_checked().
| R | Visitor return type. |
| F | Visitor type. |
| func | Visitor callable. |
| std::bad_variant_access | If func cannot be called for the active type. |
|
inline |
Const lvalue overload of apply_visitor_checked().
| R | Visitor return type. |
| F | Visitor type. |
| func | Visitor callable. |
| std::bad_variant_access | If func cannot be called for the active type. |
|
inline |
Alias of get_as().
| T | Expected held type. |
| std::bad_variant_access | If the active alternative is not T. |
|
inline |
Const alias of get_as().
| T | Expected held type. |
| std::bad_variant_access | If the active alternative is not T. |
|
inlinestaticconsteval |
Indicates whether this variant can be constructed from T.
| T | Candidate source type. |
true when T can initialize one of Ts..., excluding self-type conversion.
|
inline |
Returns pointer to held value if active type is T.
| T | Type to retrieve. |
nullptr if type does not match.
|
inline |
Const overload of cast_ptr().
| T | Type to retrieve. |
nullptr if type does not match.
|
inline |
Retrieves the held value as T& with runtime type checking.
| T | Expected held type. |
| std::bad_variant_access | If the active alternative is not T. |
|
inline |
Retrieves the held value as T const& with runtime type checking.
| T | Expected held type. |
| std::bad_variant_access | If the active alternative is not T. |
|
inline |
Retrieves the held value by alternative index with runtime checking.
| N | Alternative index. |
| std::bad_variant_access | If the active index is not N. |
|
inline |
Retrieves the held value by alternative index with runtime checking.
| N | Alternative index. |
| std::bad_variant_access | If the active index is not N. |
|
inline |
Retrieves the held value by alternative index without runtime checking.
| N | Alternative index. |
|
inline |
Retrieves the held value by alternative index without runtime checking.
| N | Alternative index. |
|
inline |
Returns the active alternative index.
Ts.... | std::bad_variant_access | If the variant is valueless. |
|
inline |
Invokes func with the held value if it is of type T.
| T | Type to match. |
| F | Callable type. |
| func | Callable invoked on match. |
true if the type matched and callable was invoked.
|
inline |
Const overload of match().
| T | Type to match. |
| F | Callable type. |
| func | Callable invoked on match. |
true if the type matched and callable was invoked.
|
inline |
Inequality comparison.
| other | Variant to compare against. |
true if variants are not equal. | std::bad_variant_access | If either variant is valueless. |
|
inline |
Strict-weak ordering comparison.
| other | Variant to compare against. |
true if *this is ordered before other. | std::bad_variant_access | If either variant is valueless. |
|
inline |
Three-way comparison.
| other | Variant to compare against. |
| std::bad_variant_access | If either variant is valueless. |
|
inline |
Copy-assigns by value using swap semantics.
| other | Source variant. |
*this.
|
inlineconstexpr |
Assigns from a value convertible to one of the alternatives.
| T | Source type. |
| value | Value to store. |
*this. | Any | exception thrown by allocation or construction of the new value. |
|
inline |
Equality comparison.
| other | Variant to compare against. |
true if both active alternative and value are equal. | std::bad_variant_access | If either variant is valueless. |
|
inline |
Returns the held value cast to T& via visitor dispatch.
| T | Target reference type. |
T&. | std::bad_variant_access | If conversion is not valid for the active alternative. |
|
inline |
Returns the held value cast to T const& via visitor dispatch.
| T | Target referenced type. |
T const&. | std::bad_variant_access | If conversion is not valid for the active alternative. |
|
inline |
Invokes func with the held value if it is of type T and returns predicate result.
| T | Type to test. |
| F | Callable type. |
| func | Predicate callable. |
false.
|
inline |
Const overload of test().
| T | Type to test. |
| F | Callable type. |
| func | Predicate callable. |
false.
|
inline |
Applies a visitor and returns default R{} when not invocable for the active type.
| R | Visitor return type. |
| F | Visitor type. |
| func | Visitor callable. |
R.
|
inline |
Rvalue overload of try_apply_visitor().
| R | Visitor return type. |
| F | Visitor type. |
| func | Visitor callable. |
R.
|
inline |
Const lvalue overload of try_apply_visitor().
| R | Visitor return type. |
| F | Visitor type. |
| func | Visitor callable. |
R.
|
inline |
Returns RTTI for the active alternative.
std::type_info for the active type. | std::bad_variant_access | If the variant is valueless. |
|
inline |
Checks whether the active alternative is any of Ts2.
| Ts2 | Types to test. |
true if active type is in Ts2....
|
inline |
Returns std::type_index for the active alternative.
| std::bad_variant_access | If the variant is valueless. |
|
inline |
Checks whether the active alternative is exactly T.
| T | Type to test. |
true if T is the active alternative.
|
inline |
Alias of get_as().
| T | Expected held type. |
| std::bad_variant_access | If the active alternative is not T. |
|
inline |
Const alias of get_as().
| T | Expected held type. |
| std::bad_variant_access | If the active alternative is not T. |
|
inline |
Retrieves the held value as T& without runtime checks.
| T | Expected held type. |
|
inline |
Retrieves the held value as T const& without runtime checks.
| T | Expected held type. |