sumty
0.1.0
Better sum types for C++
|
A discriminated union of unique error types. More...
#include <sumty/error_set.hpp>
Public Member Functions | |
constexpr | error_set () CONDITIONALLY_NOEXCEPT |
Default constructor. More... | |
constexpr | error_set (const error_set &) CONDITIONALLY_NOEXCEPT |
Copy constructor. More... | |
constexpr | error_set (error_set &&) CONDITIONALLY_NOEXCEPT |
Move constructor. More... | |
template<size_t IDX, typename... Args> | |
constexpr CONDITIONALLY_EXPLICIT | error_set (std::in_place_index_t< IDX > inplace, Args &&... args) |
Index emplacement constructor. More... | |
template<size_t IDX, typename U , typename... Args> | |
constexpr | error_set (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 | error_set ([[maybe_unused]] std::in_place_type_t< U > inplace, Args &&... args) |
Type emplacement constructor. More... | |
template<typename U , typename V , typename... Args> | |
constexpr | error_set ([[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 | error_set (U &&value) |
Forwarding constructor. More... | |
template<typename U > | |
constexpr CONDITIONALLY_EXPLICIT | error_set (std::initializer_list< U > init) |
Forwarding constructor with initializer list. More... | |
template<typename... U> | |
constexpr | error_set (const error_set< U... > &other) |
Copy conversion constructor. More... | |
template<typename... U> | |
constexpr | error_set (error_set< U... > &&other) |
Move conversion constructor. More... | |
constexpr | ~error_set () CONDITIONALLY_NOEXCEPT |
Destructor. More... | |
constexpr error_set & | operator= (const error_set &) CONDITIONALLY_NOEXCEPT |
Copy assignment operator. More... | |
constexpr error_set & | operator= (error_set &&) CONDITIONALLY_NOEXCEPT |
Move assignment operator. More... | |
template<typename U > | |
constexpr error_set & | operator= (U &&rhs) |
Forwarding assignment operator. More... | |
template<typename U > | |
constexpr error_set & | operator= (std::initializer_list< U > rhs) |
Forwarding assignment operator with initializer list. More... | |
template<typename... U> | |
constexpr error_set & | operator= (const error_set< U... > &rhs) |
Copy conversion assignment operator. More... | |
template<typename... U> | |
constexpr error_set & | operator= (error_set< U... > &&rhs) |
Move conversion assignment operator. 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 error_set holds it. More... | |
template<size_t I> | |
constexpr CONST_POINTER | get_if () const noexcept |
Gets an alternative pointer by index if the error_set holds it. More... | |
template<typename U > | |
constexpr POINTER | get_if () noexcept |
Gets an alternative pointer by type if the error_set holds it. More... | |
template<typename U > | |
constexpr CONST_POINTER | get_if () const noexcept |
Gets an alternative pointer by type if the error_set holds it. More... | |
template<typename U > | |
constexpr bool | holds_alternative () const noexcept |
Checks if an error_set 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 (error_set &other) CONDITIONALLY_NOEXCEPT |
Swaps two error_set instances. More... | |
Related Functions | |
(Note that these are not member functions.) | |
template<typename T , typename... U> | |
constexpr bool | holds_alternative (const error_set< U... > &e) noexcept |
Checks if an error_set contains a particular alternative. More... | |
template<size_t I, typename... T> | |
constexpr REFERENCE | get (error_set< T... > &e) |
Gets an alternative by index. More... | |
template<size_t I, typename... T> | |
constexpr CONST_REFERENCE | get (const error_set< T... > &e) |
Gets an alternative by index. More... | |
template<size_t I, typename... T> | |
constexpr RVALUE_REFERENCE | get (error_set< T... > &&e) |
Gets an alternative by index. More... | |
template<size_t I, typename... T> | |
constexpr CONST_RVALUE_REFERENCE | get (const error_set< T... > &&e) |
Gets an alternative by index. More... | |
template<typename T , typename... U> | |
constexpr REFERENCE | get (error_set< U... > &e) |
Gets an alternative by type. More... | |
template<typename T , typename... U> | |
constexpr CONST_REFERENCE | get (const error_set< U... > &e) |
Gets an alternative by type. More... | |
template<typename T , typename... U> | |
constexpr RVALUE_REFERENCE | get (error_set< U... > &&e) |
Gets an alternative by type. More... | |
template<typename T , typename... U> | |
constexpr CONST_RVALUE_REFERENCE | get (const error_set< U... > &&e) |
Gets an alternative by type. More... | |
template<size_t I, typename... T> | |
constexpr POINTER | get_if (error_set< T... > &e) noexcept |
Gets an alternative pointer by index if the error_set holds it. More... | |
template<size_t I, typename... T> | |
constexpr CONST_POINTER | get_if (const error_set< T... > &e) noexcept |
Gets an alternative pointer by index if the error_set holds it. More... | |
template<typename T , typename... U> | |
constexpr POINTER | get_if (error_set< U... > &e) noexcept |
Gets an alternative pointer by type if the error_set holds it. More... | |
template<typename T , typename... U> | |
constexpr CONST_POINTER | get_if (const error_set< U... > &e) noexcept |
Gets an alternative pointer by type if the error_set holds it. More... | |
template<typename... T> | |
constexpr void | swap (error_set< T... > &a, error_set< T... > &b) CONDITIONALLY_NOEXCEPT |
Swaps two error_set instances. More... | |
![]() | |
constexpr REFERENCE | get (variant< T... > &v) |
Gets a variant alternative by index. More... | |
constexpr CONST_REFERENCE | get (const variant< T... > &v) |
Gets a variant alternative by index. More... | |
constexpr RVALUE_REFERENCE | get (variant< T... > &&v) |
Gets a variant alternative by index. More... | |
constexpr CONST_RVALUE_REFERENCE | get (const variant< T... > &&v) |
Gets a variant alternative by index. More... | |
constexpr REFERENCE | get (variant< U... > &v) |
Gets a variant alternative by type. More... | |
constexpr CONST_REFERENCE | get (const variant< U... > &v) |
Gets a variant alternative by type. More... | |
constexpr RVALUE_REFERENCE | get (variant< U... > &&v) |
Gets a variant alternative by type. More... | |
constexpr CONST_RVALUE_REFERENCE | get (const variant< U... > &&v) |
Gets a variant alternative by type. More... | |
constexpr POINTER | get_if (variant< T... > &v) noexcept |
Gets a variant alternative pointer by index if the variant holds it. More... | |
constexpr CONST_POINTER | get_if (const variant< T... > &v) noexcept |
Gets a variant alternative pointer by index if the variant holds it. More... | |
constexpr POINTER | get_if (variant< U... > &v) noexcept |
Gets a variant alternative pointer by type if the variant holds it. More... | |
constexpr CONST_POINTER | get_if (const variant< U... > &v) noexcept |
Gets a variant alternative pointer by type if the variant holds it. More... | |
constexpr bool | holds_alternative (const variant< U... > &v) noexcept |
Checks if a variant contains a particular alternative. More... | |
constexpr DEDUCED | visit (V &&visitor) |
Calls a visitor callable with the contained variant alternatives. More... | |
constexpr DEDUCED | visit (V &&visitor, T0 &&var0, TN &&... varn) |
Calls a visitor callable with the contained variant alternatives. More... | |
constexpr void | swap (variant< T... > &a, variant< T... > &b) CONDITIONALLY_NOEXCEPT |
Swaps two variant instances. More... | |
A discriminated union of unique error types.
error_set is very similar to variant in term of API. The key difference from variant is that error_set requires all its alternatives to be unique, and error_set allows direct conversions between different, but compatible, error_set instantiations.
Given distinct types A
, B
, and C
, error_set allows the following implicit conversions (this is not exhaustive):
A -> error_set<A, B, C>
B -> error_set<A, B, C>
C -> error_set<A, B, C>
error_set<B> -> error_set<A, B, C>
error_set<C, A> -> error_set<A, B, C>
error_set<C, A, B> -> error_set<A, B, C>
Generally, an error_set can convert into a different error set as long as each of its alternatives unambiguously map to an alternative in the destination error_set. Additionally, any bare type can convert into an error_set if it can unambiguously convert into one of the destination alternatives.
The primary purpose of error_set is to improve the convenience of error propagation with result. The conversions shown above also apply to an error_set nested as the error type of a result. Below is a listing of the previous conversions examples, but now wrapped in result, which is equally valid.
result<T, A> -> result<T, error_set<A, B, C>>
result<T, B> -> result<T, error_set<A, B, C>>
result<T, C> -> result<T, error_set<A, B, C>>
result<T, error_set<B>> -> result<T, error_set<A, B, C>>
result<T, error_set<C, A>> -> result<T, error_set<A, B, C>>
result<T, error_set<C, A, B>> -> result<T, error_set<A, B, C>>
Below is an example of how error_set might be used in practice.
One possible pattern when using error_set is to define all errors as empty types. Like all other sum types in sumty, an error_set of all empty types only needs to actually store the discriminant. In this case, error_set behaves like a flexible enum partially mapped into the type system. That is, a specific (empty) error type uniquely identifies an alternative in any error_set that contains that error type, similar to how an enum value name identifies a value of that enum. So it is possible to create a set of error codes as types without giving them specific values and define subsets of those error codes for different use cases. A function might emit some, but not all, of the errors in your set of error codes, so that function need only declare that it can return those specific errors instead of all codes in the set.
That said, it is also perfectly valid for each error type to contain additional error information as needed.
|
constexpr |
Default constructor.
Initializes the error_set 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 error_set is initialized such that it contains a copy constructed instance of the alternative contained in the source error_set. If the source error_set has more than one alternative of the same type, the new error_set 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 error_set is initialized such that it contains a move constructed instance of the alternative contained in the source error_set. If the source error_set has more than one alternative of the same type, the new error_set 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 error_set 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 error_set 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 error_set 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 error_set 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.
|
inlineconstexpr |
Type emplacement constructor with std::initializer_list
A new error_set 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.
|
inlineconstexpr |
Forwarding constructor.
A new error_set 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.
value | The value that is used to construct the alternative |
|
inlineconstexpr |
Forwarding constructor with initializer list.
A new error_set 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.
init | The std::initializer_list that is used to construct the alternative |
Copy conversion constructor.
A new error_set is initialized from the value contained in another error_set that is a non-strict subset of the destination error_set. Each of the alternative types in the source error_set must also be an alternative type in the destination error_set, but they need not have the same index.
This constructor allows for values of error_set to be easily propagated, whether returned directly, or as an alternative of any other sum type. Most commonly, this will be used to implicitly convert from a result to another result with a superset of the errors as the source, such as when returning from a function.
Move conversion constructor.
A new error_set is initialized from the value contained in another error_set that is a non-strict subset of the destination error_set. Each of the alternative types in the source error_set must also be an alternative type in the destination error_set, but they need not have the same index.
This constructor allows for values of error_set to be easily propagated, whether returned directly, or as an alternative of any other sum type. Most commonly, this will be used to implicitly convert from a result to another result with a superset of the errors as the source, such as when returning from a function.
|
constexpr |
Destructor.
The contained alternative of the error_set 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 error_set 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 error_set contains before the call, and constructs a new alternative with the specified type in place.
args | Constructor arguments forwarded to the new alternative |
|
inlineconstexpr |
Constructs a new alternative in place by index.
This function destroys the alternative that the error_set 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 error_set contains before the call, and constructs a new alternative with the specified type in place.
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 error_set 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.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the error_set 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 error_set 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.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the error_set 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 error_set 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.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the error_set 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 error_set 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.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the error_set does not contain the alternative with the corresponding type. |
|
inlineconstexprnoexcept |
Gets an alternative pointer by index if the error_set holds it.
This functions tries to access an alternative by index. If the error_set contains the alternative, this function returns a pointer to the alternative, if applicable. If the error_set 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 error_set holds it.
This functions tries to access an alternative by type. If the error_set contains the alternative, this function returns a pointer to the alternative, if applicable. If the error_set does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
T | The type of the alternative to access. |
|
inlineconstexprnoexcept |
Gets an alternative pointer by index if the error_set holds it.
This functions tries to access an alternative by index. If the error_set contains the alternative, this function returns a pointer to the alternative, if applicable. If the error_set 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 error_set holds it.
This functions tries to access an alternative by type. If the error_set contains the alternative, this function returns a pointer to the alternative, if applicable. If the error_set does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
T | The type of the alternative to access. |
|
inlineconstexprnoexcept |
Checks if an error_set contains a particular alternative.
Given a type parameter, this function checks if the error_set currently holds an alternative that has the exact same type.
true
if the error_set holds an alternative of the given type.
|
inlineconstexprnoexcept |
Gets the index of the contained alternative.
The set of alternatives of a error_set has a zero-based index based on the order in which they are specified in the error_set template arguments.
This index is the normalized discriminant of the error_set. 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 error_set instantiations.
Copy assignment operator.
Copy assignment of an error_set can take one of two possible code paths.
If the source and destination error_set 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 error_set 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.
Copy conversion assignment operator.
This function assigns the value contained in another error_set that is a non-strict subset of the destination error_set. Each of the alternative types in the source error_set must also be an alternative type in the destination error_set, but they need not have the same index.
Move assignment operator.
Move assignment of an error_set can take one of two possible code paths.
If the source and destination error_set 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 error_set is destroyed in place, and the new alternative is move constructed.
The source error_set 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.
Move conversion assignment operator.
This function assigns the value contained in another error_set that is a non-strict subset of the destination error_set. Each of the alternative types in the source error_set must also be an alternative type in the destination error_set, but they need not have the same index.
|
inlineconstexpr |
Forwarding assignment operator with initializer list.
This function assigns a std::initializer_list
directoy to an alternative. If the error_set 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
.
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 error_set 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.
rhs | The value to be assigned to the alternative |
|
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 index must be a compile time value, the index is provided as an instance of type_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 type.
This function allows accessing alternatives by type using the square bracket operator. Because the index must be a compile time value, the index is provided as an instance of type_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 index must be a compile time value, the index is provided as an instance of type_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 type.
This function allows accessing alternatives by type using the square bracket operator. Because the index must be a compile time value, the index is provided as an instance of type_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 |
Swaps two error_set instances.
If the two error_set 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 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 error_set does not contain the alternative with the corresponding index. |
|
related |
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 error_set does not contain the alternative with the corresponding index. |
|
related |
Gets an alternative by type.
This function allows accessing alternatives by type, which is provided as a template argument.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the error_set does not contain the alternative with the corresponding type. |
|
related |
Gets an alternative by type.
This function allows accessing alternatives by type, which is provided as a template argument.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the error_set does not contain the alternative with the corresponding type. |
|
related |
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 error_set does not contain the alternative with the corresponding index. |
|
related |
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 error_set does not contain the alternative with the corresponding index. |
|
related |
Gets an alternative by type.
This function allows accessing alternatives by type, which is provided as a template argument.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the error_set does not contain the alternative with the corresponding type. |
|
related |
Gets an alternative by type.
This function allows accessing alternatives by type, which is provided as a template argument.
U | The type of the alternative to access. |
bad_variant_access | Thrown if the error_set does not contain the alternative with the corresponding type. |
|
related |
Gets an alternative pointer by index if the error_set holds it.
This functions tries to access an alternative by index. If the error_set contains the alternative, this function returns a pointer to the alternative, if applicable. If the error_set 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. |
|
related |
Gets an alternative pointer by type if the error_set holds it.
This functions tries to access an alternative by type. If the error_set contains the alternative, this function returns a pointer to the alternative, if applicable. If the error_set does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
T | The type of the alternative to access. |
|
related |
Gets an alternative pointer by index if the error_set holds it.
This functions tries to access an alternative by index. If the error_set contains the alternative, this function returns a pointer to the alternative, if applicable. If the error_set 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. |
|
related |
Gets an alternative pointer by type if the error_set holds it.
This functions tries to access an alternative by type. If the error_set contains the alternative, this function returns a pointer to the alternative, if applicable. If the error_set does not contain the alternative, this function returns null. In the case where the alternative is of type void
, this function does nothing.
T | The type of the alternative to access. |
|
related |
Checks if an error_set contains a particular alternative.
Given a type parameter, this function checks if the error_set currently holds an alternative that has the exact same type.
true
if the error_set holds an alternative of the given type. Swaps two error_set instances.
If the two error_set 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.