Variant
Type safe sum type (union), where all types of the alternatives are distinct.
Caution
Imports <concepts> and <type_traits>.
Tip
To import use #include "atl/types/variant.hpp".
Implementation details
abl::variant is implemented using a recursive union type and holds an index of the current alternative as std::size_t. Because of a large type for holding the alternative index, the size of the variant is at least 16 bytes compares to 8 bytes of a regular std::variant.
Constructors
abl::variant provides the following constructors:
abl::variant(): Default constructor, holds no value.abl::variant(const T& value): Constructs with a value of typeT.abl::variant(T&& value): Constructs with a value of typeTusing move semantics.abl::variant(const abl::variant& other): Copy constructor.abl::variant(abl::variant&& other): Move constructor.abl::variant(const abl::variant<Us...>& other): Copy constructor from the variant of typeUs..., whereUs...Ts....abl::variant(abl::variant<Us...>&& other): Move constructor from the variant of typeUs..., whereUs...Ts....
Assignment operators
abl::variant provides the following assignment operators:
abl::variant& operator=(const T& data): Assigns a value of typeTusing copy semantics.abl::variant& operator=(T&& data): Assigns a value of typeTusing move semantics.abl::variant& operator=(const abl::variant& other): Assigns a value from another variant using copy semantics.abl::variant& operator=(abl::variant&& other): Assigns a value from another variant using move semantics.abl::variant& operator=(const abl::variant<Us...>& other): Assigns a value from the variant of typeUs..., whereUs...Ts...using copy semantics.abl::variant& operator=(abl::variant<Us...>&& other): Assigns a value from the variant of typeUs..., whereUs...Ts...using move semantics.
Modifiers
abl::variant provides the following modifiers:
void swap(abl::variant& other): Swaps the contents of this variant with another variant.T& emplace<T>(Args&&... args): Constructs a value of typeTin-place using the provided arguments.T& emplace<I>(Args&&... args): Constructs a value of typeTs[I]in-place using the provided arguments.void reset(): Resets thevariantto its default state.
Accessors
abl::variant provides the following accessors:
T& get<I>() &: Returns a reference to the value of typeTs[I].const T& get<I>() const &: Returns a const reference to the value of typeTs[I].T&& get<I>() &&: Returns a rvalue reference to the value of typeTs[I].const T&& get<I>() const &&: Returns a const rvalue reference to the value of typeTs[I].T& get<T>() &: Returns a reference to the value of typeT.const T& get<T>() const &: Returns a const reference to the value of typeT.T&& get<T>() &&: Returns a rvalue reference to the value of typeT.const T&& get<T>() const &&: Returns a const rvalue reference to the value of typeT.
Note
These are the methods available for accessing the value of the variant, i.e. they can be called as v.get<T>() and v.get<I>().
Observers
abl::variant provides the following observers:
bool holds_alternative<T>(): Returnstrueif the variant holds a value of typeT.std::size_t index(): Returns the index of the currently held alternative.operator bool(): Returnstrueif the variant holds a value.
Non-member functions
abl::variant provides the following non-member functions:
bool holds_alternative<T>(const variant& v): Returnstrueif the variantvholds a value of typeT.void swap(variant& a, variant& b): Swaps the contents ofaandb.T& get<T>(variant& v): Returns a reference to the value of typeTinv.const T& get<T>(const variant& v): Returns a const reference to the value of typeTinv.T&& get<T>(variant&& v): Returns a rvalue reference to the value of typeTinv.const T&& get<T>(const variant&& v): Returns a const rvalue reference to the value of typeT.T& get<I>(variant& v): Returns a reference to the value of typeTs[I]inv.const T& get<I>(const variant& v): Returns a const reference to the value of typeTs[I]inv.T&& get<I>(variant&& v): Returns a rvalue reference to the value of typeTs[I]inv.const T&& get<I>(const variant&& v): Returns a const rvalue reference to the value of typeTs[I].
Note
Thus, abl::variant provides accessors in both standard functional style and as methods. Check out the examples below.