sumty  0.1.0
Better sum types for C++
error_set< T > Class Template Reference

A discriminated union of unique error types. More...

#include <sumty/error_set.hpp>

Inheritance diagram for error_set< T >:
Collaboration diagram for error_set< T >:

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_setoperator= (const error_set &) CONDITIONALLY_NOEXCEPT
 Copy assignment operator. More...
 
constexpr error_setoperator= (error_set &&) CONDITIONALLY_NOEXCEPT
 Move assignment operator. More...
 
template<typename U >
constexpr error_setoperator= (U &&rhs)
 Forwarding assignment operator. More...
 
template<typename U >
constexpr error_setoperator= (std::initializer_list< U > rhs)
 Forwarding assignment operator with initializer list. More...
 
template<typename... U>
constexpr error_setoperator= (const error_set< U... > &rhs)
 Copy conversion assignment operator. More...
 
template<typename... U>
constexpr error_setoperator= (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...
 

Detailed Description

template<typename... T>
class sumty::error_set< T >

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):

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.

Below is an example of how error_set might be used in practice.

// specific error types
struct neg_int_error {};
struct int_parse_error {};
struct int_overflow_error {};
// union of the above error types
using my_errors = error_set<
neg_int_error,
int_parse_error,
int_overflow_error
>;
// parse an integer from a string
result<int, int_parse_error> parse_int(std::string_view str);
// calculate the absolute value of an integer
result<int, int_overflow_error> abs_value(int x);
// calculate the integer square root
result<int, neg_int_error> isqrt(int x);
result<int, my_errors> my_algorithm(std::string_view str) {
auto parsed = parse_int(str);
if (parsed.is_error()) { return parsed; }
auto positive = abs_value(*parsed);
if (positive.is_error()) { return positive; }
return isqrt(*positive);
}

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.

Constructor & Destructor Documentation

◆ error_set() [1/11]

constexpr error_set ( )
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.

Example

error_set<error1, error2> set;
assert(holds_alternative<error1>(set));
assert(set.index() == 0);

◆ error_set() [2/11]

constexpr error_set ( const error_set< T > &  )
constexpr

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.

Example

error_set<error1, error2> e1{std::in_place_index<1>};
error_set<error1, error2> e2{e1};
assert(holds_alternative<error2>(e2));
assert(e2.index() == 1);

◆ error_set() [3/11]

constexpr error_set ( error_set< T > &&  )
constexpr

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.

Example

error_set<error1, error2> e1{std::in_place_index<1>};
error_set<error1, error2> e2{std::move(e1)};
assert(holds_alternative<error2>(e2));
assert(e2.index() == 1);

◆ error_set() [4/11]

constexpr CONDITIONALLY_EXPLICIT error_set ( std::in_place_index_t< IDX >  inplace,
Args &&...  args 
)
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)....

Example

error_set<error, std::string> e{std::in_place_index<1>, 5, 'a'};
assert(holds_alternative<std::string>(e));
assert(e.index() == 1);
assert(get<1>(e) == "aaaaa");
Parameters
inplaceConstructor tag that specifies the alternative index.
argsArguments used to construct the alternative.

◆ error_set() [5/11]

constexpr error_set ( std::in_place_index_t< IDX >  inplace,
std::initializer_list< U >  init,
Args &&...  args 
)
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)....

Example

error_set<error, std::vector<int>> e{
std::in_place_index<1>,
{1, 2, 3, 4, 5}};
assert(holds_alternative<std::vector<int>>(e));
assert(e.index() == 1);
assert(get<1>(e).size() == 5);
constexpr bool holds_alternative() const noexcept
Checks if an error_set contains a particular alternative.
Definition: error_set.hpp:1585
Parameters
inplaceConstructor tag that specifies the alternative index.
initInitializer list forwarded to the alternative constructor
argsAdditional arguments used to construct the alternative.

◆ error_set() [6/11]

constexpr CONDITIONALLY_EXPLICIT error_set ( [[maybe_unused] ] std::in_place_type_t< U >  inplace,
Args &&...  args 
)
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.

Example

error_set<error, std::string> e{
std::in_place_type<std::string>,
5, 'a'};
assert(holds_alternative<std::string>(e));
assert(e.index() == 1);
assert(get<1>(e) == "aaaaa");

◆ error_set() [7/11]

constexpr error_set ( [[maybe_unused] ] std::in_place_type_t< U >  inplace,
std::initializer_list< V >  init,
Args &&...  args 
)
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.

Example

error_set<error, std::vector<int>> e{
std::in_place_type<std::vector<int>>,
{1, 2, 3, 4, 5}};
assert(holds_alternative<std::vector<int>>(e));
assert(e.index() == 1);
assert(get<1>(e).size() == 5);

◆ error_set() [8/11]

constexpr CONDITIONALLY_EXPLICIT error_set ( U &&  value)
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.

Example

error_set<error, std::string> e{"oh no"};
assert(e.index() == 1);
assert(get<1>(e) == "oh no");
Parameters
valueThe value that is used to construct the alternative

◆ error_set() [9/11]

constexpr CONDITIONALLY_EXPLICIT error_set ( std::initializer_list< U >  init)
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.

Example

error_set<error, std::vector<int>> e({1, 2, 3, 4, 5});
assert(e.index() == 1);
assert(get<1>(e).size() == 5);
Parameters
initThe std::initializer_list that is used to construct the alternative

◆ error_set() [10/11]

constexpr error_set ( const error_set< U... > &  other)
inlineconstexpr

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.

Example

const error_set<error1, error2> e1{std::in_place_index<1>};
const error_set<error2, error3, error1> e2{e1};
assert(holds_alternative<error2>(e2));
assert(e2.index() == 0);

◆ error_set() [11/11]

constexpr error_set ( error_set< U... > &&  other)
inlineconstexpr

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.

Example

error_set<error1, error2> e1{std::in_place_index<1>};
error_set<error2, error3, error1> e2{std::move(e1)};
assert(holds_alternative<error2>(e2));
assert(e2.index() == 0);

◆ ~error_set()

constexpr ~error_set ( )
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.

Member Function Documentation

◆ emplace() [1/4]

constexpr REFERENCE emplace ( Args &&...  args)
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.

Example

error_set<error, std::string> e;
e.emplace<1>(5, 'a');
assert(holds_alternative<std::string>(e));
assert(e.index() == 1);
assert(get<1>(e) == "aaaaa");
Parameters
argsConstructor arguments forwarded to the new alternative
Returns
A reference to the new alternative, if applicable

◆ emplace() [2/4]

constexpr REFERENCE emplace ( Args &&...  args)
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.

Example

error_set<error, std::string> e;
e.emplace<std::string>(5, 'a');
assert(holds_alternative<std::string>(e));
assert(e.index() == 1);
assert(get<1>(e) == "aaaaa");
Parameters
argsConstructor arguments forwarded to the new alternative
Returns
A reference to the new alternative, if applicable

◆ emplace() [3/4]

constexpr REFERENCE emplace ( std::initializer_list< U >  ilist,
Args &&...  args 
)
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.

Example

error_set<error, std::vector<int>> e;
e.emplace<1>({1, 2, 3, 4, 5});
assert(holds_alternative<std::vector<int>>(e));
assert(e.index() == 1);
assert(get<1>(e).size() == 5);
Parameters
ilistInitializer list forward to the new alternative
argsConstructor arguments forwarded to the new alternative
Returns
A reference to the new alternative, if applicable

◆ emplace() [4/4]

constexpr REFERENCE emplace ( std::initializer_list< V >  ilist,
Args &&...  args 
)
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.

Example

error_set<error, std::vector<int>> e;
e.emplace<std::vector<int>>({1, 2, 3, 4, 5});
assert(holds_alternative<std::vector<int>>(e));
assert(e.index() == 1);
assert(get<1>(e).size() == 5);
Parameters
ilistInitializer list forward to the new alternative
argsConstructor arguments forwarded to the new alternative
Returns
A reference to the new alternative, if applicable

◆ get() [1/8]

constexpr REFERENCE get ( ) &
inlineconstexpr

Gets an alternative by index.

This function allows accessing alternatives by index, which is provided as a template argument.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(e.get<1>() == "oh no");
e.get<1>() = "no problem";
assert(e.get<1>() == "no problem");
Template Parameters
IThe index of the alternative to access.
Returns
A reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding index.

◆ get() [2/8]

constexpr REFERENCE get ( ) &
inlineconstexpr

Gets an alternative by type.

This function allows accessing alternatives by type, which is provided as a template argument.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(e.get<std::string>() == "oh no");
e.get<std::string>() = "no problem";
assert(e.get<std::string>() == "no problem");
Template Parameters
UThe type of the alternative to access.
Returns
A reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding type.

◆ get() [3/8]

constexpr RVALUE_REFERENCE get ( ) &&
inlineconstexpr

Gets an alternative by index.

This function allows accessing alternatives by index, which is provided as a template argument.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(std::move(e).get<1>() == "oh no");
Template Parameters
IThe index of the alternative to access.
Returns
An rvalue reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding index.

◆ get() [4/8]

constexpr RVALUE_REFERENCE get ( ) &&
inlineconstexpr

Gets an alternative by type.

This function allows accessing alternatives by type, which is provided as a template argument.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(std::move(e).get<std::string>() == "oh no");
Template Parameters
UThe type of the alternative to access.
Returns
An rvalue reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding type.

◆ get() [5/8]

constexpr CONST_REFERENCE get ( ) const &
inlineconstexpr

Gets an alternative by index.

This function allows accessing alternatives by index, which is provided as a template argument.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(e.get<1>() == "oh no");
Template Parameters
IThe index of the alternative to access.
Returns
A const reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding index.

◆ get() [6/8]

constexpr CONST_REFERENCE get ( ) const &
inlineconstexpr

Gets an alternative by type.

This function allows accessing alternatives by type, which is provided as a template argument.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(e.get<std::string>() == "oh no");
Template Parameters
UThe type of the alternative to access.
Returns
A const reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding type.

◆ get() [7/8]

constexpr CONST_RVALUE_REFERENCE get ( ) const &&
inlineconstexpr

Gets an alternative by index.

This function allows accessing alternatives by index, which is provided as a template argument.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(std::move(e).get<1>() == "oh no");
Template Parameters
IThe index of the alternative to access.
Returns
A const rvalue reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding index.

◆ get() [8/8]

constexpr CONST_RVALUE_REFERENCE get ( ) const &&
inlineconstexpr

Gets an alternative by type.

This function allows accessing alternatives by type, which is provided as a template argument.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(std::move(e).get<std::string>() == "oh no");
Template Parameters
UThe type of the alternative to access.
Returns
A const rvalue reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding type.

◆ get_if() [1/4]

constexpr CONST_POINTER get_if ( ) const
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.

Example

const error_set<error1, error2, error3> e{std::in_place_index<1>};
assert(e.get_if<1>() != nullptr);
assert(e.get_if<0>() == nullptr);
Template Parameters
IThe index of the alternative to access.
Returns
A const pointer to the accessed alternative, if applicable, or null

◆ get_if() [2/4]

constexpr CONST_POINTER get_if ( ) const
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.

Example

const error_set<error1, error2, error3> e{std::in_place_index<1>};
assert(e.get_if<error2>() != nullptr);
assert(e.get_if<error1>() == nullptr);
Template Parameters
TThe type of the alternative to access.
Returns
A const pointer to the accessed alternative, if applicable, or null

◆ get_if() [3/4]

constexpr POINTER get_if ( )
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.

Example

error_set<error1, error2, error3> e{std::in_place_index<1>};
assert(e.get_if<1>() != nullptr);
assert(e.get_if<0>() == nullptr);
Template Parameters
IThe index of the alternative to access.
Returns
A pointer to the accessed alternative, if applicable, or null

◆ get_if() [4/4]

constexpr POINTER get_if ( )
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.

Example

error_set<error1, error2, error3> e{std::in_place_index<1>};
assert(e.get_if<error2>() != nullptr);
assert(e.get_if<error1>() == nullptr);
Template Parameters
TThe type of the alternative to access.
Returns
A pointer to the accessed alternative, if applicable, or null

◆ holds_alternative()

constexpr bool holds_alternative ( ) const
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.

Example

error_set<error1, error2, error3> e1{std::in_place_index<0>};
assert(e1.holds_alternative<error1>());
error_set<error1, error2, error3> e2{std::in_place_index<2>};
assert(e2.holds_alternative<error1>());
Returns
true if the error_set holds an alternative of the given type.

◆ index()

constexpr size_t index ( ) const
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.

Example

error_set<error1, error2, error3, error4> e{std::in_place_index<2>};
assert(e.index() == 2);
Returns
The index of the contained alternative.

◆ operator=() [1/6]

constexpr error_set& operator= ( const error_set< T > &  )
constexpr

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.

Example

error_set<error1, error2> e1{std::in_place_index<0>};
variant<error2, error2> e2{std::in_place_index<1>};
e1 = e2;
assert(holds_alternative<error2>(e1));
assert(e1.index() == 1);

◆ operator=() [2/6]

constexpr error_set& operator= ( const error_set< U... > &  rhs)
inlineconstexpr

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.

Example

const error_set<error1, error2> e1{std::in_place_index<1>};
error_set<error2, error3, error1> e2{};
e2 = e1;
assert(holds_alternative<error2>(e2));
assert(e2.index() == 0);

◆ operator=() [3/6]

constexpr error_set& operator= ( error_set< T > &&  )
constexpr

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.

Example

error_set<error1, error2> e1{std::in_place_index<0>};
error_set<error1, error2> e2{std::in_place_index<1>};
e1 = std::move(e2);
assert(holds_alternative<error2>(e1));
assert(e1.index() == 1);

◆ operator=() [4/6]

constexpr error_set& operator= ( error_set< U... > &&  rhs)
inlineconstexpr

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.

Example

error_set<error1, error2> e1{std::in_place_index<1>};
error_set<error2, error3, error1> e2{};
e2 = std::move(e1);
assert(holds_alternative<error2>(e2));
assert(e2.index() == 0);

◆ operator=() [5/6]

constexpr error_set& operator= ( std::initializer_list< U >  rhs)
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.

Example

variant<error, std::vector<int>> e{};
e = {1, 2, 3, 4, 5};
assert(holds_alternative<std::vector<int>>(e));
assert(e.index() == 1);
Parameters
rhsThe std::initializer_list to be assigned to the alternative

◆ operator=() [6/6]

constexpr error_set& operator= ( U &&  rhs)
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.

Example

error_set<error1, error2> e{};
e = error2{};
assert(holds_alternative<error2>(e));
assert(e.index() == 1);
Parameters
rhsThe value to be assigned to the alternative

◆ operator[]() [1/8]

constexpr RVALUE_REFERENCE operator[] ( [[maybe_unused] ] index_t< I >  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.

Example

error_set<error1, std::string, error2> e{std::in_place_index<1>, "oh no"};
assert(std::move(e)[index<1>] == "oh no");
Parameters
indexA tag value that communicates a compile time index
Returns
A reference to the accessed alternative, if applicable

◆ operator[]() [2/8]

constexpr REFERENCE operator[] ( [[maybe_unused] ] index_t< I >  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.

Example

error_set<error1, std::string, error2> e{std::in_place_index<1>, "oh no"};
assert(e[index<1>] == "oh no");
v[index<1>] = "no problem";
assert(get<1>(v) == "no problem");
Parameters
indexA tag value that communicates a compile time index
Returns
A reference to the accessed alternative, if applicable

◆ operator[]() [3/8]

constexpr CONST_RVALUE_REFERENCE operator[] ( [[maybe_unused] ] index_t< I >  index) const &&
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.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(std::move(e)[index<1>] == "oh no");
Parameters
indexA tag value that communicates a compile time index
Returns
A reference to the accessed alternative, if applicable

◆ operator[]() [4/8]

constexpr CONST_REFERENCE operator[] ( [[maybe_unused] ] index_t< I >  index) const &
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.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(e[index<1>] == "oh no");
Parameters
indexA tag value that communicates a compile time index
Returns
A reference to the accessed alternative, if applicable

◆ operator[]() [5/8]

constexpr RVALUE_REFERENCE operator[] ( [[maybe_unused] ] type_t< U >  type) &&
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.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(std::move(e)[type<std::string>] == "oh no");
Parameters
indexA tag value that communicates a compile time index
Returns
The accessed alternative value, if applicable

◆ operator[]() [6/8]

constexpr REFERENCE operator[] ( [[maybe_unused] ] type_t< U >  type) &
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.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(e[type<std::string>] == "oh no");
v[type<std::string>] = "no problem";
assert(get<std::string>(v) == "no problem");
Parameters
indexA tag value that communicates a compile time index
Returns
The accessed alternative value, if applicable

◆ operator[]() [7/8]

constexpr CONST_RVALUE_REFERENCE operator[] ( [[maybe_unused] ] type_t< U >  type) const &&
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.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(std::move(e)[type<std::string>] == "oh no");
Parameters
indexA tag value that communicates a compile time index
Returns
The accessed alternative value, if applicable

◆ operator[]() [8/8]

constexpr CONST_REFERENCE operator[] ( [[maybe_unused] ] type_t< U >  type) const &
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.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(e[type<std::string>] == "oh no");
Parameters
indexA tag value that communicates a compile time index
Returns
The accessed alternative value, if applicable

◆ swap()

constexpr void swap ( error_set< T > &  other)
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.

Example

error_set<error1, error2, error3> e1{std::in_place_index<0>};
error_set<error1, error2, error3> e2{std::in_place_index<1>};
e1.swap(e2);
assert(e1.index() == 1);
assert(e2.index() == 0);
Parameters
otherThe "other" error_set to swap with this error_set

◆ visit() [1/4]

constexpr DEDUCED visit ( V &&  visitor) &
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.

Example

error_set<error1, error2, error3> e1{std::in_place_index<1>};
e1.visit(overload(
[](error1& e) { assert(false); },
[](error2& e) { assert(true); },
[](error3& e) { assert(false); }
));
Parameters
visitorThe callable object that will be passed an alternative.
Returns
The return value of the visitor, if any.

◆ visit() [2/4]

constexpr DEDUCED visit ( V &&  visitor) &&
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.

Example

error_set<error1, error2, error3> e1{std::in_place_index<1>};
std::move(e1).visit(overload(
[](error1&& e) { assert(false); },
[](error2&& e) { assert(true); },
[](error3&& e) { assert(false); }
));
Parameters
visitorThe callable object that will be passed an alternative.
Returns
The return value of the visitor, if any.

◆ visit() [3/4]

constexpr DEDUCED visit ( V &&  visitor) const &
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.

Example

const error_set<error1, error2, error3> e1{std::in_place_index<1>};
e1.visit(overload(
[](const error1& e) { assert(false); },
[](const error2& e) { assert(true); },
[](const error3& e) { assert(false); }
));
Parameters
visitorThe callable object that will be passed an alternative.
Returns
The return value of the visitor, if any.

◆ visit() [4/4]

constexpr DEDUCED visit ( V &&  visitor) const &&
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.

Example

const error_set<error1, error2, error3> e1{std::in_place_index<1>};
std::move(e1).visit(overload(
[](const error1&& e) { assert(false); },
[](const error2&& e) { assert(true); },
[](const error3&& e) { assert(false); }
));
Parameters
visitorThe callable object that will be passed an alternative.
Returns
The return value of the visitor, if any.

◆ visit_informed() [1/4]

constexpr DEDUCED visit_informed ( V &&  visitor) &
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.

struct alternative_info {
// index of the alternative in the source error_set
static inline constexpr size_t index = ...;
// type of the alternative as declared in the source error_set
using type = ...;
// helper function for forwarding non-const alternative values
// without needing to provide a template argument.
static constexpr decltype(auto) forward(...);
};
constexpr size_t index() const noexcept
Gets the index of the contained alternative.
Definition: error_set.hpp:777

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.

Example

error_set<error1, error2, error3> e1{std::in_place_index<1>};
e1.visit_informed([](auto& value, auto info) {
if constexpr (info.index == 0) {
assert(false);
} else if constexpr (info.index == 1) {
assert(true);
} else if constexpr (info.index == 2) {
assert(false);
}
});
Parameters
visitorThe callable object that will be passed an alternative.
Returns
The return value of the visitor, if any.

◆ visit_informed() [2/4]

constexpr DEDUCED visit_informed ( V &&  visitor) &&
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.

struct alternative_info {
// index of the alternative in the source error_set
static inline constexpr size_t index = ...;
// type of the alternative as declared in the source error_set
using type = ...;
// helper function for forwarding non-const alternative values
// without needing to provide a template argument.
static constexpr decltype(auto) forward(...);
};

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.

Example

error_set<error1, error2, error3> e1{std::in_place_index<1>};
std::move(e1).visit_informed([](auto&& value, auto info) {
if constexpr (info.index == 0) {
assert(false);
} else if constexpr (info.index == 1) {
assert(true);
} else if constexpr (info.index == 2) {
assert(false);
}
});
Parameters
visitorThe callable object that will be passed an alternative.
Returns
The return value of the visitor, if any.

◆ visit_informed() [3/4]

constexpr DEDUCED visit_informed ( V &&  visitor) const &
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.

struct alternative_info {
// index of the alternative in the source error_set
static inline constexpr size_t index = ...;
// type of the alternative as declared in the source error_set
using type = ...;
// helper function for forwarding non-const alternative values
// without needing to provide a template argument.
static constexpr decltype(auto) forward(...);
};

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.

Example

const error_set<error1, error2, error3> e1{std::in_place_index<1>};
e1.visit_informed([](const auto& value, auto info) {
if constexpr (info.index == 0) {
assert(false);
} else if constexpr (info.index == 1) {
assert(true);
} else if constexpr (info.index == 2) {
assert(false);
}
});
Parameters
visitorThe callable object that will be passed an alternative.
Returns
The return value of the visitor, if any.

◆ visit_informed() [4/4]

constexpr DEDUCED visit_informed ( V &&  visitor) const &&
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.

struct alternative_info {
// index of the alternative in the source error_set
static inline constexpr size_t index = ...;
// type of the alternative as declared in the source error_set
using type = ...;
// helper function for forwarding non-const alternative values
// without needing to provide a template argument.
static constexpr decltype(auto) forward(...);
};

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.

Example

const error_set<error1, error2, error3> e1{std::in_place_index<1>};
std::move(e1).visit_informed([](const auto&& value, auto info) {
if constexpr (info.index == 0) {
assert(false);
} else if constexpr (info.index == 1) {
assert(true);
} else if constexpr (info.index == 2) {
assert(false);
}
});
Parameters
visitorThe callable object that will be passed an alternative.
Returns
The return value of the visitor, if any.

Friends And Related Function Documentation

◆ get() [1/8]

constexpr CONST_RVALUE_REFERENCE get ( const error_set< T... > &&  e)
related

Gets an alternative by index.

This function allows accessing alternatives by index, which is provided as a template argument.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(get<1>(std::move(e)) == "oh no");
Template Parameters
IThe index of the alternative to access.
Returns
A const rvalue reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding index.

◆ get() [2/8]

constexpr CONST_REFERENCE get ( const error_set< T... > &  e)
related

Gets an alternative by index.

This function allows accessing alternatives by index, which is provided as a template argument.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(get<1>(e) == "oh no");
Template Parameters
IThe index of the alternative to access.
Returns
A const reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding index.

◆ get() [3/8]

constexpr CONST_RVALUE_REFERENCE get ( const error_set< U... > &&  e)
related

Gets an alternative by type.

This function allows accessing alternatives by type, which is provided as a template argument.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(std::move(e).get<std::string>() == "oh no");
Template Parameters
UThe type of the alternative to access.
Returns
A const rvalue reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding type.

◆ get() [4/8]

constexpr CONST_REFERENCE get ( const error_set< U... > &  e)
related

Gets an alternative by type.

This function allows accessing alternatives by type, which is provided as a template argument.

Example

const error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(get<std::string>(e) == "oh no");
Template Parameters
UThe type of the alternative to access.
Returns
A const reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding type.

◆ get() [5/8]

constexpr RVALUE_REFERENCE get ( error_set< T... > &&  e)
related

Gets an alternative by index.

This function allows accessing alternatives by index, which is provided as a template argument.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(get<1>(std::move(e)) == "oh no");
Template Parameters
IThe index of the alternative to access.
Returns
An rvalue reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding index.

◆ get() [6/8]

constexpr REFERENCE get ( error_set< T... > &  e)
related

Gets an alternative by index.

This function allows accessing alternatives by index, which is provided as a template argument.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(get<1>(e) == "oh no");
get<1>(e) = "no problem";
assert(get<1>(e) == "no problem");
Template Parameters
IThe index of the alternative to access.
Returns
A reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding index.

◆ get() [7/8]

constexpr RVALUE_REFERENCE get ( error_set< U... > &&  e)
related

Gets an alternative by type.

This function allows accessing alternatives by type, which is provided as a template argument.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(get<std::string>(std::move(e)) == "oh no");
Template Parameters
UThe type of the alternative to access.
Returns
An rvalue reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding type.

◆ get() [8/8]

constexpr REFERENCE get ( error_set< U... > &  e)
related

Gets an alternative by type.

This function allows accessing alternatives by type, which is provided as a template argument.

Example

error_set<error1, std::string, error2> e{
std::in_place_index<1>, "oh no"};
assert(get<std::string>(e) == "oh no");
get<std::string>(e) = "no problem";
assert(get<std::string>(e) == "no problem");
Template Parameters
UThe type of the alternative to access.
Returns
A reference to the accessed alternative, if applicable.
Exceptions
bad_variant_accessThrown if the error_set does not contain the alternative with the corresponding type.

◆ get_if() [1/4]

constexpr CONST_POINTER get_if ( const error_set< T... > &  e)
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.

Example

const error_set<error1, error2, error3> e{std::in_place_index<1>};
assert(get_if<1>(e) != nullptr);
assert(get_if<0>(e) == nullptr);
Template Parameters
IThe index of the alternative to access.
Returns
A const pointer to the accessed alternative, if applicable, or null

◆ get_if() [2/4]

constexpr CONST_POINTER get_if ( const error_set< U... > &  e)
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.

Example

const error_set<error1, error2, error3> e{std::in_place_index<1>};
assert(get_if<error2>(e) != nullptr);
assert(get_if<error1>(e) == nullptr);
Template Parameters
TThe type of the alternative to access.
Returns
A const pointer to the accessed alternative, if applicable, or null

◆ get_if() [3/4]

constexpr POINTER get_if ( error_set< T... > &  e)
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.

Example

error_set<error1, error2, error3> e{std::in_place_index<1>};
assert(get_if<1>(e) != nullptr);
assert(get_if<0>(e) == nullptr);
Template Parameters
IThe index of the alternative to access.
Returns
A pointer to the accessed alternative, if applicable, or null

◆ get_if() [4/4]

constexpr POINTER get_if ( error_set< U... > &  e)
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.

Example

error_set<error1, error2, error3> e{std::in_place_index<1>};
assert(get_if<error2>(e) != nullptr);
assert(get_if<error1>(e) == nullptr);
Template Parameters
TThe type of the alternative to access.
Returns
A pointer to the accessed alternative, if applicable, or null

◆ holds_alternative()

constexpr bool holds_alternative ( const error_set< U... > &  e)
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.

Example

error_set<error1, error2, error3> e1{std::in_place_index<0>};
assert(holds_alternative<error1>(e1));
error_set<error1, error2, error3> e2{std::in_place_index<2>};
assert(holds_alternative<error1>(e2));
Returns
true if the error_set holds an alternative of the given type.

◆ swap()

constexpr void swap ( error_set< T... > &  a,
error_set< T... > &  b 
)
related

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.

Example

error_set<error1, error2, error3> e1{std::in_place_index<0>};
error_set<error1, error2, error3> e2{std::in_place_index<1>};
swap(e1, e2);
assert(e1.index() == 1);
assert(e2.index() == 0);
constexpr void swap(error_set &other) CONDITIONALLY_NOEXCEPT
Swaps two error_set instances.
Definition: error_set.hpp:2055
Parameters
otherThe "other" error_set to swap with this error_set

The documentation for this class was generated from the following files: