sumty
0.1.0
Better sum types for C++
|
General discriminated union. More...
#include <sumty/variant.hpp>
Public Member Functions | |
constexpr | variant () CONDITIONALLY_NOEXCEPT |
Default constructor. More... | |
constexpr | variant (const variant &) |
Copy constructor. More... | |
constexpr | variant (variant &&) |
Move constructor. More... | |
template<size_t IDX, typename... Args> | |
constexpr CONDITIONALLY_EXPLICIT | variant (std::in_place_index_t< IDX > inplace, Args &&... args) |
Index emplacement constructor. More... | |
template<size_t IDX, typename U , typename... Args> | |
constexpr | variant (std::in_place_index_t< IDX > inplace, std::initializer_list< U > init, Args &&... args) |
Index emplacement constructor with std::initializer_list More... | |
template<typename U , typename... Args> | |
constexpr CONDITIONALLY_EXPLICIT | variant ([[maybe_unused]] std::in_place_type_t< U > inplace, Args &&... args) |
Type emplacement constructor. More... | |
template<typename U , typename V , typename... Args> | |
constexpr | variant ([[maybe_unused]] std::in_place_type_t< U > inplace, std::initializer_list< V > init, Args &&... args) |
Type emplacement constructor with std::initializer_list More... | |
template<typename U > | |
constexpr CONDITIONALLY_EXPLICIT | variant (U &&value) |
Forwarding constructor. More... | |
template<typename U > | |
constexpr CONDITIONALLY_EXPLICIT | variant (std::initializer_list< U > init) |
Forwarding constructor with initializer list. More... | |
constexpr | ~variant () CONDITIONALLY_NOEXCEPT |
Destructor. More... | |
constexpr variant & | operator= (const variant &rhs) |
Copy assignment operator. More... | |
constexpr variant & | operator= (variant &&rhs) |
Move assignment operator. More... | |
template<typename U > | |
constexpr variant & | operator= (U &&rhs) |
Forwarding assignment operator. More... | |
template<typename U > | |
constexpr variant & | operator= (std::initializer_list< U > rhs) |
Forwarding assignment operator with initializer list. More... | |
constexpr size_t | index () const noexcept |
Gets the index of the contained alternative. More... | |
template<size_t I, typename... Args> | |
constexpr REFERENCE | emplace (Args &&... args) |
Constructs a new alternative in place by index. More... | |
template<size_t I, typename U , typename... Args> | |
constexpr REFERENCE | emplace (std::initializer_list< U > ilist, Args &&... args) |
Constructs a new alternative in place by index. More... | |
template<typename U , typename... Args> | |
constexpr REFERENCE | emplace (Args &&... args) |
Constructs a new alternative in place by type. More... | |
template<typename U , typename V , typename... Args> | |
constexpr REFERENCE | emplace (std::initializer_list< V > ilist, Args &&... args) |
Constructs a new alternative in place by type. More... | |
template<size_t I> | |
constexpr REFERENCE | operator[] ([[maybe_unused]] index_t< I > index) &noexcept |
Alternative access operator by index. More... | |
template<size_t I> | |
constexpr CONST_REFERENCE | operator[] ([[maybe_unused]] index_t< I > index) const &noexcept |
Alternative access operator by index. More... | |
template<size_t I> | |
constexpr RVALUE_REFERENCE | operator[] ([[maybe_unused]] index_t< I > index) && |
Alternative access operator by index. More... | |
template<size_t I> | |
constexpr CONST_RVALUE_REFERENCE | operator[] ([[maybe_unused]] index_t< I > index) const && |
Alternative access operator by index. More... | |
template<typename U > | |
constexpr REFERENCE | operator[] ([[maybe_unused]] type_t< U > type) &noexcept |
Alternative access operator by type. More... | |
template<typename U > | |
constexpr CONST_REFERENCE | operator[] ([[maybe_unused]] type_t< U > type) const &noexcept |
Alternative access operator by type. More... | |
template<typename U > | |
constexpr RVALUE_REFERENCE | operator[] ([[maybe_unused]] type_t< U > type) && |
Alternative access operator by type. More... | |
template<typename U > | |
constexpr CONST_RVALUE_REFERENCE | operator[] ([[maybe_unused]] type_t< U > type) const && |
Alternative access operator by type. More... | |
template<size_t I> | |
constexpr REFERENCE | get () & |
Gets an alternative by index. More... | |
template<size_t I> | |
constexpr CONST_REFERENCE | get () const & |
Gets an alternative by index. More... | |
template<size_t I> | |
constexpr RVALUE_REFERENCE | get () && |
Gets an alternative by index. More... | |
template<size_t I> | |
constexpr CONST_RVALUE_REFERENCE | get () const && |
Gets an alternative by index. More... | |
template<typename U > | |
constexpr REFERENCE | get () & |
Gets an alternative by type. More... | |
template<typename U > | |
constexpr CONST_REFERENCE | get () const & |
Gets an alternative by type. More... | |
template<typename U > | |
constexpr RVALUE_REFERENCE | get () && |
Gets an alternative by type. More... | |
template<typename U > | |
constexpr CONST_RVALUE_REFERENCE | get () const && |
Gets an alternative by type. More... | |
template<size_t I> | |
constexpr POINTER | get_if () noexcept |
Gets an alternative pointer by index if the variant holds it. More... | |
template<size_t I> | |
constexpr CONST_POINTER | get_if () const noexcept |
Gets an alternative pointer by index if the variant holds it. More... | |
template<typename U > | |
constexpr POINTER | get_if () noexcept |
Gets an alternative pointer by type if the variant holds it. More... | |
template<typename U > | |
constexpr CONST_POINTER | get_if () const noexcept |
Gets an alternative pointer by type if the variant holds it. More... | |
template<typename U > | |
constexpr bool | holds_alternative () const noexcept |
Checks if a variant contains a particular alternative. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) & |
Calls a visitor callable with the contained alternative. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) const & |
Calls a visitor callable with the contained alternative. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) && |
Calls a visitor callable with the contained alternative. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) const && |
Calls a visitor callable with the contained alternative. More... | |
template<typename V > | |
constexpr DEDUCED | visit_informed (V &&visitor) & |
Calls a visitor callable with the contained alternative and metadata. More... | |
template<typename V > | |
constexpr DEDUCED | visit_informed (V &&visitor) const & |
Calls a visitor callable with the contained alternative and metadata. More... | |
template<typename V > | |
constexpr DEDUCED | visit_informed (V &&visitor) && |
Calls a visitor callable with the contained alternative and metadata. More... | |
template<typename V > | |
constexpr DEDUCED | visit_informed (V &&visitor) const && |
Calls a visitor callable with the contained alternative and metadata. More... | |
constexpr void | swap (variant &other) CONDITIONALLY_NOEXCEPT |
Swaps two variant instances. More... | |
Friends | |
class | error_set< T... > |
template<typename , typename > | |
class | result |
Related Functions | |
(Note that these are not member functions.) | |
template<typename T , typename... U> | |
constexpr bool | holds_alternative (const variant< U... > &v) noexcept |
Checks if a variant contains a particular alternative. More... | |
template<size_t I, typename... T> | |
constexpr REFERENCE | get (variant< T... > &v) |
Gets a variant alternative by index. More... | |
template<size_t I, typename... T> | |
constexpr CONST_REFERENCE | get (const variant< T... > &v) |
Gets a variant alternative by index. More... | |
template<size_t I, typename... T> | |
constexpr RVALUE_REFERENCE | get (variant< T... > &&v) |
Gets a variant alternative by index. More... | |
template<size_t I, typename... T> | |
constexpr CONST_RVALUE_REFERENCE | get (const variant< T... > &&v) |
Gets a variant alternative by index. More... | |
template<typename T , typename... U> | |
constexpr REFERENCE | get (variant< U... > &v) |
Gets a variant alternative by type. More... | |
template<typename T , typename... U> | |
constexpr CONST_REFERENCE | get (const variant< U... > &v) |
Gets a variant alternative by type. More... | |
template<typename T , typename... U> | |
constexpr RVALUE_REFERENCE | get (variant< U... > &&v) |
Gets a variant alternative by type. More... | |
template<typename T , typename... U> | |
constexpr CONST_RVALUE_REFERENCE | get (const variant< U... > &&v) |
Gets a variant alternative by type. More... | |
template<size_t I, typename... T> | |
constexpr POINTER | get_if (variant< T... > &v) noexcept |
Gets a variant alternative pointer by index if the variant holds it. More... | |
template<size_t I, typename... T> | |
constexpr CONST_POINTER | get_if (const variant< T... > &v) noexcept |
Gets a variant alternative pointer by index if the variant holds it. More... | |
template<typename T , typename... U> | |
constexpr POINTER | get_if (variant< U... > &v) noexcept |
Gets a variant alternative pointer by type if the variant holds it. More... | |
template<typename T , typename... U> | |
constexpr CONST_POINTER | get_if (const variant< U... > &v) noexcept |
Gets a variant alternative pointer by type if the variant holds it. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) |
Calls a visitor callable with the contained variant alternatives. More... | |
template<typename V , typename T0 , typename... TN> | |
constexpr DEDUCED | visit (V &&visitor, T0 &&var0, TN &&... varn) |
Calls a visitor callable with the contained variant alternatives. More... | |
template<typename... T> | |
constexpr void | swap (variant< T... > &a, variant< T... > &b) CONDITIONALLY_NOEXCEPT |
Swaps two variant instances. More... | |
General discriminated union.
variant is the fundamental sum type of sumty. option, result, and error_set are defined in terms of variant.
variant is very similar to std::variant
, but it does have some key differences. Most notably, variant does not support type-based alternative selection unless all alternatives have a distinct type. For example, std::variant
will allow the code shown below, but variant will not.
Any such functionality from std::variant
that is similarly ambiguous to the reader is disallowed by variant. Otherwise, the interface provided by variant is identical to std::variant
, but with some additional functions available.
Another key difference from std::variant
is that variant allows alternatives to have void
and references (both lvalue and rvalue) as types. When using an lvalue reference as an alternative type, copy and move semantics applied to the variant are the same as a raw pointer (both trivial), instead of immovability of lvalue references.
A more subtle, but important, difference from std::variant
is that variant makes size optimizations for several special cases. These special cases revolve around alternatives with types that are lvalue references, void
, and empty types (such as std::monostate
).
void
and empty types need not occupy any memory at runtime, despite the fact that sizeof(<empty-type>) == 1
in C++. A variant consisting entirely of alternatives with types that are void
or empty types only takes up enough space to store a discriminant, which is always the smallest possible integral type necessary to store the value N - 1
, where N
is the number of alternatives. Here are some examples:
lvalue references have the invariant that the underlying pointer is not null. This means in cases where there are only two alternatives, with one being an lvalue reference and the other being void
or an empty type, null and non-null can be used as the discriminanting values instead of having a separate discriminant. Here are some more examples:
T | variant alternative types |
|
constexpr |
Default constructor.
Initializes the variant such that it contains a default constructed value of the first (index 0) alternative.
The first alternative must be default constructible for this constructor to participate in overload resoltuion, but no other alternatives need be default constructible.
Copy constructor.
A new variant is initialized such that it contains a copy constructed instance of the alternative contained in the source variant. If the source variant has more than one alternative of the same type, the new variant will contain the alternative of the same index.
All alternative types must be copy constructible for this constructor to participate in overload resolution.
Move constructor.
A new variant is initialized such that it contains a move constructed instance of the alternative contained in the source variant. If the source vairant has more than one alternative of the same type, the new variant will contain the alternative of the same index.
All alternative types must be move constructible for this constructor to participate in overload resolution.
The source variant will continue to contain an instance of the same alternative, but the value of the alternative after being moved depends on the move constructor of the type. In general, moved values are said to be in a valid, but unspecified, state.
|
inlineconstexpr |
Index emplacement constructor.
A new variant is initialized such that it contains a newly constructed instance of the alternative with the specified index. The arguments following inplace
are forwarded directly to the constructor of the alternative type.
Given that U
is the type of the alternative at the specified index, this constructor is always valid as long as U
is constructible with the arguments, std::forward<Args>(args)...
.
inplace | Constructor tag that specifies the alternative index. |
args | Arguments used to construct the alternative. |
|
inlineconstexpr |
Index emplacement constructor with std::initializer_list
A new variant is initialized such that it contains a newly constructed instance of the alternative with the specified index. The arguments following inplace
are forwarded directly to the constructor of the alternative type.
Given that U
is the type of the alternative at the specified index, this constructor is always valid as long as U
is constructible with the arguments, init, std::forward<Args>(args)...
.
inplace | Constructor tag that specifies the alternative index. |
init | Initializer list forwarded to the alternative constructor |
args | Additional arguments used to construct the alternative. |
|
inlineconstexpr |
Type emplacement constructor.
A new variant is initialized such that it contains a newly constructed instance of the alternative with the specified type. The arguments following inplace
are forwarded directory to the constructor of the alternative type.
This constructor only participates in overload resolution if the alternative type, U
, is unique among all alternatives of the variant. That is, variant<A, B, A>
, cannot use this constructor to initialize an alternative of type A
, but it could use this constructor to initialize the alternative of type B
. This constraint is imposed to prevent ambiguity to the reader. Use the index based emplacement constructor instead to initialize the alternative of type A
.
|
inlineconstexpr |
Type emplacement constructor with std::initializer_list
A new variant is initialized such that it contains a newly constructed instance of the alternative with the specified type. The arguments following inplace
are forwarded directory to the constructor of the alternative type.
This constructor only participates in overload resolution if the alternative type, U
, is unique among all alternatives of the variant. That is, variant<A, B, A>
, cannot use this constructor to initialize an alternative of type A
, but it could use this constructor to initialize the alternative of type B
. This constraint is imposed to prevent ambiguity to the reader. Use the index based emplacement constructor instead to initialize the alternative of type A
.
|
inlineconstexpr |
Forwarding constructor.
A new variant is initialized such that an alternative is constructed in place with the provided value as the constructor argument.
To avoid ambiguity to the reader, this constructor only participates in overload resolution when there is only one alternative that could possibly be constructed from the value.
This constructor is explicit
if the value is not implicitly convertible to the target alternative type.
This constructor should be avoided in generic code, because some variant instantiations will have distinct types while others will not. Prefer the emplacement constructors.
value | The value that is used to construct the alternative |
|
inlineconstexpr |
Forwarding constructor with initializer list.
A new variant is initialized such that an alternative is constructed in place with the provided std::initializer_list
as the constructor argument.
To avoid ambiguity to the reader, this constructor only participates in overload resulotion when there is only one alternative that could possible be constructed from the initializer list.
This constructor is explicit
if the std::initializer_list
is not implicitly convertible to the target alternative type.
This constructor should be avoided in generic code, because some variant instantiations will have distinct types while others will not. Prefer the emplacement constructors.
init | The std::initializer_list that is used to construct the alternative |
|
constexpr |
Destructor.
The contained alternative of the variant will is destroyed in place.
The destructor is noexcept
if all alternative types are nothrow destructible.
|
inlineconstexpr |
Constructs a new alternative in place by index.
This function destroys the alternative that the variant contains before the call, and constructs a new alternative with the specified index in place.
args | Constructor arguments forwarded to the new alternative |
|
inlineconstexpr |
Constructs a new alternative in place by type.
This function destroy the alternative that the variant contains before the call, and constructs a new alternative with the specified type in place.
This function only participates in overload resolution if the type, U
, is unique among all the alternative types of the variant. That is, variant<A, B, A>
cannot use this function to emplace an alternative of type A
, but it can use this function to emplace an alternative of type B
.
args | Constructor arguments forwarded to the new alternative |
|
inlineconstexpr |
Constructs a new alternative in place by index.
This function destroys the alternative that the variant contains before the call, and constructs a new alternative with the specified index in place.
ilist | Initializer list forward to the new alternative |
args | Constructor arguments forwarded to the new alternative |
|
inlineconstexpr |
Constructs a new alternative in place by type.
This function destroy the alternative that the variant contains before the call, and constructs a new alternative with the specified type in place.
This function only participates in overload resolution if the type, U
, is unique among all the alternative types of the variant. That is, variant<A, B, A>
cannot use this function to emplace an alternative of type A
, but it can use this function to emplace an alternative of type B
.
ilist | Initializer list forward to the new alternative |
args | Constructor arguments forwarded to the new alternative |
|
inlineconstexpr |
Gets an alternative by index.
This function allows accessing alternatives by index, which is provided as a template argument.
I | The index of the alternative to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding index. |
|
inlineconstexpr |
Gets an alternative by type.
This function allows accessing alternatives by type, which is provided as a template argument.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding type. |
|
inlineconstexpr |
Gets an alternative by index.
This function allows accessing alternatives by index, which is provided as a template argument.
I | The index of the alternative to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding index. |
|
inlineconstexpr |
Gets an alternative by type.
This function allows accessing alternatives by type, which is provided as a template argument.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding type. |
|
inlineconstexpr |
Gets an alternative by index.
This function allows accessing alternatives by index, which is provided as a template argument.
I | The index of the alternative to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding index. |
|
inlineconstexpr |
Gets an alternative by type.
This function allows accessing alternatives by type, which is provided as a template argument.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding type. |
|
inlineconstexpr |
Gets an alternative by index.
This function allows accessing alternatives by index, which is provided as a template argument.
I | The index of the alternative to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding index. |
|
inlineconstexpr |
Gets an alternative by type.
This function allows accessing alternatives by type, which is provided as a template argument.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding type. |
|
inlineconstexprnoexcept |
Gets an alternative pointer by index if the variant holds it.
This functions tries to access an alternative by index. If the variant contains the alternative, this function returns a pointer to the alternative, if applicable. If the variant does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
I | The index of the alternative to access. |
|
inlineconstexprnoexcept |
Gets an alternative pointer by type if the variant holds it.
This functions tries to access an alternative by type. If the variant contains the alternative, this function returns a pointer to the alternative, if applicable. If the variant does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
I | The index of the alternative to access. |
|
inlineconstexprnoexcept |
Gets an alternative pointer by index if the variant holds it.
This functions tries to access an alternative by index. If the variant contains the alternative, this function returns a pointer to the alternative, if applicable. If the variant does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
I | The index of the alternative to access. |
|
inlineconstexprnoexcept |
Gets an alternative pointer by type if the variant holds it.
This functions tries to access an alternative by type. If the variant contains the alternative, this function returns a pointer to the alternative, if applicable. If the variant does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
I | The index of the alternative to access. |
|
inlineconstexprnoexcept |
Checks if a variant contains a particular alternative.
Given a type parameter, this function checks if the variant currently holds an alternative that has the exact same type.
true
if the variant holds an alternative of the given type.
|
inlineconstexprnoexcept |
Gets the index of the contained alternative.
The set of alternatives of a variant has a zero-based index based on the order in which they are specified in the variant template arguments.
This index is the normalized discriminant of the variant. The discriminant may be represented differently internally, depending on the alternative types, so this function normalizes the discriminant by converting it to a zero-based index in order to provide a common interface for all variant instantiations.
Copy assignment operator.
Copy assignment of a variant can take one of two possible code paths.
If the source and destination variant hold the same alternative (same index), the alternative value is copied via copy assignment.
Otherwise, if the source and destination hold different alternatives (different indices, but possibly the same type), the alternative of the destination variant is destroyed in place, and the new alternative is copy constructed.
All alternatives must be both copy assignable and copy constructible for this function to participate in overload resolution.
|
inlineconstexpr |
Forwarding assignment operator with initializer list.
This function assigns a std::initializer_list
directoy to an alternative. If the variant already contains the target alternative, the std::initializer_list
is assigned uting the alternative type's assignment operator. Otherwise, the target alternative is constructed with the std::initializer_list
.
To avoid ambiguity to the reader, this function only participates in overload resolution when there is only one alternative that could possibly be assigned from the std::initializer_list
.
This opreator should be avoided in generic code, because some variant instantiations will have distinct types while others will not.
rhs | The std::initializer_list to be assigned to the alternative |
|
inlineconstexpr |
Forwarding assignment operator.
This function assigns a value directly to an alternative. If the variant already contains the target alternative, the value is assigned using the alternative type's assignemnt operator. Otherwise, the target alternative is constructed with the value.
To avoid ambiguity to the reader, this function only participates in overload resolution when there is only one alternative that could possibly be assigned from the value.
This operator should be avoided in generic code, because some variant instantiations will have distinct types while others will not.
rhs | The value to be assigned to the alternative |
Move assignment operator.
Move assignment of a variant can take one of two possible code paths.
If the source and destination variant hold the same alternative (same index), the alternative value is moved from the source to the destination via move assignment.
Otherwise, if the source and destination hold different alternatives (different indices, but possibly the same type), the alternative of the destination variant is destroyed in place, and the new alternative is move constructed.
The source variant will still contain the same alternative, but the value of the alternative depends on the move assignment or move constructor of the alternative's type. In general, moved values are said to be in a valid, but unspecified, state.
All alternatives must be both move assignable and move constructible for this function to participate in overload resolution.
|
inlineconstexpr |
Alternative access operator by index.
This function allows accessing alternatives by index using the square bracket operator. Because the index must be a compile time value, instead of passing the index directly, the index is provided as an instance of index_t.
This operator is unchecked and does not throw an exception. Passing an index that does not correspond to the currently contained alternative results in undefined behavior.
index | A tag value that communicates a compile time index |
|
inlineconstexprnoexcept |
Alternative access operator by index.
This function allows accessing alternatives by index using the square bracket operator. Because the index must be a compile time value, instead of passing the index directly, the index is provided as an instance of index_t.
This operator is unchecked and does not throw an exception. Passing an index that does not correspond to the currently contained alternative results in undefined behavior.
index | A tag value that communicates a compile time index |
|
inlineconstexpr |
Alternative access operator by index.
This function allows accessing alternatives by index using the square bracket operator. Because the index must be a compile time value, instead of passing the index directly, the index is provided as an instance of index_t.
This operator is unchecked and does not throw an exception. Passing an index that does not correspond to the currently contained alternative results in undefined behavior.
index | A tag value that communicates a compile time index |
|
inlineconstexprnoexcept |
Alternative access operator by index.
This function allows accessing alternatives by index using the square bracket operator. Because the index must be a compile time value, instead of passing the index directly, the index is provided as an instance of index_t.
This operator is unchecked and does not throw an exception. Passing an index that does not correspond to the currently contained alternative results in undefined behavior.
index | A tag value that communicates a compile time index |
|
inlineconstexpr |
Alternative access operator by type.
This function allows accessing alternatives by type using the square bracket operator. Because the type must be a compile time value, the type is provided as an instance of type_t.
This operator is unchecked and does not throw an exception. Passing a type that does not correspond to the currently contained alternative results in undefined behavior.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
index | A tag value that communicates a compile time index |
|
inlineconstexprnoexcept |
Alternative access operator by type.
This function allows accessing alternatives by type using the square bracket operator. Because the type must be a compile time value, the type is provided as an instance of type_t.
This operator is unchecked and does not throw an exception. Passing a type that does not correspond to the currently contained alternative results in undefined behavior.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
index | A tag value that communicates a compile time index |
|
inlineconstexpr |
Alternative access operator by type.
This function allows accessing alternatives by type using the square bracket operator. Because the type must be a compile time value, the type is provided as an instance of type_t.
This operator is unchecked and does not throw an exception. Passing a type that does not correspond to the currently contained alternative results in undefined behavior.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
index | A tag value that communicates a compile time index |
|
inlineconstexprnoexcept |
Alternative access operator by type.
This function allows accessing alternatives by type using the square bracket operator. Because the type must be a compile time value, the type is provided as an instance of type_t.
This operator is unchecked and does not throw an exception. Passing a type that does not correspond to the currently contained alternative results in undefined behavior.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
index | A tag value that communicates a compile time index |
|
inlineconstexpr |
Swaps two variant instances.
If the two variant instances contain the same alternative, the alternative values are swapped directly. Otherwise, the alternatives are swapped by moving out of the variants, destroying the old alternatives, and move constructed into the new alternatives.
|
inlineconstexpr |
Calls a visitor callable with the contained alternative.
This function calls the visitor as std::invoke(visitor, alternative)
and returns the result of that call, if any. As such, visitor
must be able to accecpt any alternative type as an argument. In the case of an alternative of type void
, the visitor must be callable as std::invoke(visitor, void_v)
.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
inlineconstexpr |
Calls a visitor callable with the contained alternative.
This function calls the visitor as std::invoke(visitor, alternative)
and returns the result of that call, if any. As such, visitor
must be able to accecpt any alternative type as an argument. In the case of an alternative of type void
, the visitor must be callable as std::invoke(visitor, void_v)
.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
inlineconstexpr |
Calls a visitor callable with the contained alternative.
This function calls the visitor as std::invoke(visitor, alternative)
and returns the result of that call, if any. As such, visitor
must be able to accecpt any alternative type as an argument. In the case of an alternative of type void
, the visitor must be callable as std::invoke(visitor, void_v)
.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
inlineconstexpr |
Calls a visitor callable with the contained alternative.
This function calls the visitor as std::invoke(visitor, alternative)
and returns the result of that call, if any. As such, visitor
must be able to accecpt any alternative type as an argument. In the case of an alternative of type void
, the visitor must be callable as std::invoke(visitor, void_v)
.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
inlineconstexpr |
Calls a visitor callable with the contained alternative and metadata.
This function calls the visitor as std::invoke(visitor, alternative, info)
, and returns the result of that call, if any. As such, visitor
must be able to accept any alternative type as an argument. In the case of an alternative of type void
, the visitor must be callable as std::invoke(visitor, void_v, info)
.
The info
argument passed to the visitor, which differentiates this function from .visit(...)
, communicates constexpr
information about the alternative being visited. The type of the info
object is not meant to be named, but it has the API shown below. Note that info
is always an empty type.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
inlineconstexpr |
Calls a visitor callable with the contained alternative and metadata.
This function calls the visitor as std::invoke(visitor, alternative, info)
, and returns the result of that call, if any. As such, visitor
must be able to accept any alternative type as an argument. In the case of an alternative of type void
, the visitor must be callable as std::invoke(visitor, void_v, info)
.
The info
argument passed to the visitor, which differentiates this function from .visit(...)
, communicates constexpr
information about the alternative being visited. The type of the info
object is not meant to be named, but it has the API shown below. Note that info
is always an empty type.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
inlineconstexpr |
Calls a visitor callable with the contained alternative and metadata.
This function calls the visitor as std::invoke(visitor, alternative, info)
, and returns the result of that call, if any. As such, visitor
must be able to accept any alternative type as an argument. In the case of an alternative of type void
, the visitor must be callable as std::invoke(visitor, void_v, info)
.
The info
argument passed to the visitor, which differentiates this function from .visit(...)
, communicates constexpr
information about the alternative being visited. The type of the info
object is not meant to be named, but it has the API shown below. Note that info
is always an empty type.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
inlineconstexpr |
Calls a visitor callable with the contained alternative and metadata.
This function calls the visitor as std::invoke(visitor, alternative, info)
, and returns the result of that call, if any. As such, visitor
must be able to accept any alternative type as an argument. In the case of an alternative of type void
, the visitor must be callable as std::invoke(visitor, void_v, info)
.
The info
argument passed to the visitor, which differentiates this function from .visit(...)
, communicates constexpr
information about the alternative being visited. The type of the info
object is not meant to be named, but it has the API shown below. Note that info
is always an empty type.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
related |
Gets a variant alternative by index.
This function allows accessing variant alternatives by index, which is provided as a template argument.
I | The index of the alternative to access. |
v | The variant to access |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding index. |
|
related |
Gets a variant alternative by index.
This function allows accessing variant alternatives by index, which is provided as a template argument.
I | The index of the alternative to access. |
v | The variant to access |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding index. |
|
related |
Gets a variant alternative by type.
This function allows accessing variant alternatives by type, which is provided as a template argument.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
U | The type of the alternative to access. |
v | The variant to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding type. |
|
related |
Gets a variant alternative by type.
This function allows accessing variant alternatives by type, which is provided as a template argument.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
U | The type of the alternative to access. |
v | The variant to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding type. |
|
related |
Gets a variant alternative by index.
This function allows accessing variant alternatives by index, which is provided as a template argument.
I | The index of the alternative to access. |
v | The variant to access |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding index. |
|
related |
Gets a variant alternative by index.
This function allows accessing variant alternatives by index, which is provided as a template argument.
I | The index of the alternative to access. |
v | The variant to access |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding index. |
|
related |
Gets a variant alternative by type.
This function allows accessing variant alternatives by type, which is provided as a template argument.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
U | The type of the alternative to access. |
v | The variant to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding type. |
|
related |
Gets a variant alternative by type.
This function allows accessing variant alternatives by type, which is provided as a template argument.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
U | The type of the alternative to access. |
v | The variant to access. |
bad_variant_access | Thrown if the variant does not contain the alternative with the corresponding type. |
|
related |
Gets a variant alternative pointer by index if the variant holds it.
This functions tries to access a variant alternative by index. If the variant contains the alternative, this function returns a pointer to the alternative, if applicable. If the variant does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
I | The index of the alternative to access. |
v | The variant to access |
|
related |
Gets a variant alternative pointer by type if the variant holds it.
This functions tries to access an alternative by type. If the variant contains the alternative, this function returns a pointer to the alternative, if applicable. If the variant does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
I | The index of the alternative to access. |
v | The variant to access. |
|
related |
Gets a variant alternative pointer by index if the variant holds it.
This functions tries to access a variant alternative by index. If the variant contains the alternative, this function returns a pointer to the alternative, if applicable. If the variant does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
I | The index of the alternative to access. |
v | The variant to access |
|
related |
Gets a variant alternative pointer by type if the variant holds it.
This functions tries to access an alternative by type. If the variant contains the alternative, this function returns a pointer to the alternative, if applicable. If the variant does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
This function only participates in overload resolution if the type is unique across all alternative types of the variant. That is, variant<A, B, A>
cannot use this function to access an alternative of type A
, but it can use this fucntion to access an alternative of type B
.
I | The index of the alternative to access. |
v | The variant to access. |
|
related |
Checks if a variant contains a particular alternative.
Given a type parameter, this function checks if the variant currently holds an alternative that has the exact same type.
v | The variant to check. |
true
if the variant holds an alternative of the given type. Swaps two variant instances.
If the two variant instances contain the same alternative, the alternative values are swapped directly. Otherwise, the alternatives are swapped by moving out of the variants, destroying the old alternatives, and move constructed into the new alternatives.
a | The first variant in the swap |
b | The second variant in the swap |
|
related |
Calls a visitor callable with the contained variant alternatives.
This function calls the visitor as std::invoke(visitor, alternative...)
and returns the result of that call, if any. As such, visitor
must be able to accecpt any combination of alternative types as arguments. In the case of an alternative type void
, the visitor must accept nothing for that argument. That is, the alternative type combination int, void, int
, would result in the visitor being called as std::invoke(visitor, int_value, int_value)
, essentially skipping the void
alternative.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
|
related |
Calls a visitor callable with the contained variant alternatives.
This function calls the visitor as std::invoke(visitor, alternative...)
and returns the result of that call, if any. As such, visitor
must be able to accecpt any combination of alternative types as arguments. In the case of an alternative type void
, the visitor must accept nothing for that argument. That is, the alternative type combination int, void, int
, would result in the visitor being called as std::invoke(visitor, int_value, int_value)
, essentially skipping the void
alternative.
Note that the overload function can be helpful for defining a visitor inline.
Also note that this function is implemented as a compile-time-defined jump table (array of function pointers). In performance critical applications, be wary of any assumptions about how well or poorly your compiler will optimize a call to this function.
visitor | The callable object that will be passed an alternative. |
var0 | The first variant to visit |
varn | The remaining variant to visit |