GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::Variant< Ts > Class Template Reference

Tagged union value type that holds a value of one of the given types. More...

#include <GREM/core/data/Variant.hpp>

Inheritance diagram for grem::Variant< Ts >:

Public Types

using index_type
 Index type used to encode the active alternative type.

Public Member Functions

constexpr Variant () noexcept(std::is_nothrow_default_constructible_v< variant_alternative_t< 0, Variant > >)
 Default-construct a variant with the first variant alternative, if it is default-constructible.
template<typename U>
requires (!std::is_same_v<std::remove_cvref_t<U>, Variant> && variant_has_alternative_v<decltype(F(std::forward<U>(value))), Variant> && std::is_constructible_v<decltype(F(std::forward<U>(value))), U>)
constexpr Variant (U &&value) noexcept(std::is_nothrow_constructible_v< decltype(F(std::forward< U >(value))), U >)
 Converting constructor.
template<typename T, typename... Args>
requires (variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, Args...>)
constexpr Variant (std::in_place_type_t< T > type, Args &&... args)
 Construct a variant alternative in-place given its type.
template<typename T, typename U, typename... Args>
requires (variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, std::initializer_list<U>&, Args...>)
constexpr Variant (std::in_place_type_t< T > type, std::initializer_list< U > ilist, Args &&... args)
 Construct a variant alternative in-place given its type, with an initializer list as the first constructor argument.
template<std::size_t Index, typename... Args>
requires (Index < sizeof...(Ts) && std::is_constructible_v<variant_alternative_t<Index, Variant>, Args...>)
constexpr Variant (std::in_place_index_t< Index > index, Args &&... args)
 Construct a variant alternative in-place given its index.
template<std::size_t Index, typename U, typename... Args>
requires (Index < sizeof...(Ts) && std::is_constructible_v<variant_alternative_t<Index, Variant>, std::initializer_list<U>&, Args...>)
constexpr Variant (std::in_place_index_t< Index > index, std::initializer_list< U > ilist, Args &&... args)
 Construct a variant alternative in-place given its index, with an initializer list as the first constructor argument.
constexpr ~Variant ()
 Destructor.
constexpr ~Variant ()=default
 Trivial destructor.
constexpr Variant (const Variant &other)=delete
 Deleted copy constructor.
constexpr Variant (const Variant &other)=default
 Trivial copy constructor.
constexpr Variant (const Variant &other)
 Copy constructor.
constexpr Variant (Variant &&other) noexcept=default
 Trivial move constructor.
constexpr Variant (Variant &&other) noexcept((std::is_nothrow_move_constructible_v< Ts > &&...))
 Move constructor.
constexpr Variant & operator= (const Variant &other)=delete
 Deleted copy assignment.
constexpr Variant & operator= (const Variant &other)=default
 Trivial copy assignment.
constexpr Variant & operator= (const Variant &other)
 Copy assignment.
constexpr Variant & operator= (Variant &&other) noexcept=default
 Trivial move assignment.
constexpr Variant & operator= (Variant &&other) noexcept(((std::is_nothrow_move_constructible_v< Ts > &&//NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations) std::is_nothrow_move_assignable_v< Ts >) &&...))
 Move assignment.
template<typename U>
requires (!std::is_same_v<std::remove_cvref_t<U>, Variant> && variant_has_alternative_v<decltype(F(std::forward<U>(value))), Variant> && std::is_assignable_v<decltype(F(std::forward<U>(value))), U>)
constexpr Variant & operator= (U &&value) noexcept(std::is_nothrow_assignable_v< decltype(F(std::forward< U >(value)))&, U > &&std::is_nothrow_constructible_v< decltype(F(std::forward< U >(value))), U >)
 Converting assignment.
template<typename T, typename... Args>
requires (variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, Args...>)
constexpr T & emplace (Args &&... args)
 Construct an alternative given its type, destroying the old value.
template<typename T, typename U, typename... Args>
requires (variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, std::initializer_list<U>&, Args...>)
constexpr T & emplace (std::initializer_list< U > ilist, Args &&... args)
 Construct an alternative given its type, with an initializer list as the first constructor argument, destroying the old value.
template<std::size_t Index, typename... Args>
requires (std::is_constructible_v<variant_alternative_t<Index, Variant>, Args...>)
constexpr auto & emplace (Args &&... args)
 Construct an alternative given its index, destroying the old value.
template<std::size_t Index, typename U, typename... Args>
requires (std::is_constructible_v<variant_alternative_t<Index, Variant>, std::initializer_list<U>&, Args...>)
constexpr auto & emplace (std::initializer_list< U > ilist, Args &&... args)
 Construct an alternative given its index, with an initializer list as the first constructor argument, destroying the old value.
constexpr void swap (Variant &other) noexcept(((std::is_nothrow_move_constructible_v< Ts > &&std::is_nothrow_swappable_v< Ts >) &&...))
 Swap this variant's value with that of another.
constexpr index_type index () const noexcept
 Get the alternative index of the currently active value held by the variant.
constexpr bool valueless_by_exception () const noexcept
 Check if the variant is in the valueless by exception state.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr bool is () const noexcept
 Check if the variant currently holds the alternative with the given type.
template<std::size_t Index>
constexpr bool is () const noexcept
 Check if the variant currently holds the alternative with the given index.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr T & as () &noexcept
 Access the underlying value with the given type without a safety check.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr const T & as () const &noexcept
 Access the underlying value with the given type without a safety check.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr T && as () &&noexcept
 Access the underlying value with the given type without a safety check.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr const T && as () const &&noexcept
 Access the underlying value with the given type without a safety check.
template<std::size_t Index>
constexpr auto & as () &noexcept
 Access the underlying value with the given index without a safety check.
template<std::size_t Index>
constexpr const auto & as () const &noexcept
 Access the underlying value with the given index without a safety check.
template<std::size_t Index>
constexpr auto && as () &&noexcept
 Access the underlying value with the given index without a safety check.
template<std::size_t Index>
constexpr const auto && as () const &&noexcept
 Access the underlying value with the given index without a safety check.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr T & get () &
 Access the underlying value with the given type.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr const T & get () const &
 Access the underlying value with the given type.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr T && get () &&
 Access the underlying value with the given type.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr const T && get () const &&
 Access the underlying value with the given type.
template<std::size_t Index>
constexpr auto & get () &
 Access the underlying value with the given index.
template<std::size_t Index>
constexpr const auto & get () const &
 Access the underlying value with the given index.
template<std::size_t Index>
constexpr auto && get () &&
 Access the underlying value with the given index.
template<std::size_t Index>
constexpr const auto && get () const &&
 Access the underlying value with the given index.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr T * get_if () noexcept
 Access the underlying value with the given type if it is the currently active alternative.
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
constexpr const T * get_if () const noexcept
 Access the underlying value with the given type if it is the currently active alternative.
template<std::size_t Index>
constexpr auto * get_if () noexcept
 Access the underlying value with the given index if it is the currently active alternative.
template<std::size_t Index>
constexpr const auto * get_if () const noexcept
 Access the underlying value with the given index if it is the currently active alternative.

Static Public Member Functions

template<typename Visitor, typename V>
static constexpr decltype(auto) visit (Visitor &&visitor, V &&variant)
 Call a visitor functor with the currently active underlying value of a variant.
template<typename Visitor>
static constexpr decltype(auto) visitIndex (index_type index, Visitor &&visitor)
 Call a visitor functor with the type corresponding to a variant index.
template<typename Function>
static constexpr decltype(auto) apply (Function &&function)
 Call a functor with all alternative types.

Static Public Attributes

static constexpr index_type npos = sizeof...(Ts)
 Invalid alternative index, representing the valueless by exception state.

Friends

template<typename... Us>
class Variant
 JSON value type using the default allocator.
constexpr void swap (Variant &a, Variant &b) noexcept(noexcept(a.swap(b)))
 Swap the values of two variants.

Detailed Description

template<typename... Ts>
class grem::Variant< Ts >

Tagged union value type that holds a value of one of the given types.

Its API mimics that of std::variant, with some added convenience functions such as Variant::is(), Variant::as() and match().

Template Parameters
Tstype alternatives that can be held by the variant.

Member Typedef Documentation

◆ index_type

template<typename... Ts>
using grem::Variant< Ts >::index_type
Initial value:
std::conditional_t<sizeof...(Ts) < 255ull, std::uint8_t,
std::conditional_t<sizeof...(Ts) < 65535ull, std::uint16_t,
std::conditional_t<sizeof...(Ts) < 4294967295ull, std::uint32_t,
std::uint64_t>>>

Index type used to encode the active alternative type.

This is automatically sized to the smallest possible standard unsigned integer type that can represent all of the possible variant alternatives, including the valueless by exception state.

Constructor & Destructor Documentation

◆ Variant() [1/11]

template<typename... Ts>
grem::Variant< Ts >::Variant ( )
inlineconstexprnoexcept

Default-construct a variant with the first variant alternative, if it is default-constructible.

Exceptions
anyexception thrown by the underlying constructor of the first alternative type.

◆ Variant() [2/11]

template<typename... Ts>
template<typename U>
requires (!std::is_same_v<std::remove_cvref_t<U>, Variant> && variant_has_alternative_v<decltype(F(std::forward<U>(value))), Variant> && std::is_constructible_v<decltype(F(std::forward<U>(value))), U>)
grem::Variant< Ts >::Variant ( U && value)
inlineconstexprnoexcept

Converting constructor.

Chooses the variant alternative type to construct based on the overloading rules specified by std::variant.

Parameters
valueunderlying value to construct the variant from. Must be convertible to one of the variant's listed alternative types.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.

◆ Variant() [3/11]

template<typename... Ts>
template<typename T, typename... Args>
requires (variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, Args...>)
grem::Variant< Ts >::Variant ( std::in_place_type_t< T > type,
Args &&... args )
inlineexplicitconstexpr

Construct a variant alternative in-place given its type.

Parameters
typestd::in_place_type<T>, where T is the alternative type to construct, which must be one of the variant's listed alternative types.
argsarguments to pass to the underlying value's constructor.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.

◆ Variant() [4/11]

template<typename... Ts>
template<typename T, typename U, typename... Args>
requires (variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, std::initializer_list<U>&, Args...>)
grem::Variant< Ts >::Variant ( std::in_place_type_t< T > type,
std::initializer_list< U > ilist,
Args &&... args )
inlineexplicitconstexpr

Construct a variant alternative in-place given its type, with an initializer list as the first constructor argument.

Parameters
typestd::in_place_type<T>, where T is the alternative type to construct, which must be one of the variant's listed alternative types.
ilistfirst argument to pass to the underlying value's constructor.
argssubsequent arguments to pass to the underlying value's constructor.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.

◆ Variant() [5/11]

template<typename... Ts>
template<std::size_t Index, typename... Args>
requires (Index < sizeof...(Ts) && std::is_constructible_v<variant_alternative_t<Index, Variant>, Args...>)
grem::Variant< Ts >::Variant ( std::in_place_index_t< Index > index,
Args &&... args )
inlineexplicitconstexpr

Construct a variant alternative in-place given its index.

Parameters
indexstd::in_place_index<Index>, where Index is the alternative index to construct, which must be within the range of the variant's list of alternative types.
argsarguments to pass to the underlying value's constructor.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.

◆ Variant() [6/11]

template<typename... Ts>
template<std::size_t Index, typename U, typename... Args>
requires (Index < sizeof...(Ts) && std::is_constructible_v<variant_alternative_t<Index, Variant>, std::initializer_list<U>&, Args...>)
grem::Variant< Ts >::Variant ( std::in_place_index_t< Index > index,
std::initializer_list< U > ilist,
Args &&... args )
inlineexplicitconstexpr

Construct a variant alternative in-place given its index, with an initializer list as the first constructor argument.

Parameters
indexstd::in_place_index<Index>, where Index is the alternative index to construct, which must be within the range of the variant's list of alternative types.
ilistfirst argument to pass to the underlying value's constructor.
argssubsequent arguments to pass to the underlying value's constructor.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.

◆ ~Variant() [1/2]

template<typename... Ts>
grem::Variant< Ts >::~Variant ( )
inlineconstexpr

Destructor.

◆ ~Variant() [2/2]

template<typename... Ts>
grem::Variant< Ts >::~Variant ( )
constexprdefault

Trivial destructor.

◆ Variant() [7/11]

template<typename... Ts>
grem::Variant< Ts >::Variant ( const Variant< Ts > & other)
constexprdelete

Deleted copy constructor.

◆ Variant() [8/11]

template<typename... Ts>
grem::Variant< Ts >::Variant ( const Variant< Ts > & other)
constexprdefault

Trivial copy constructor.

◆ Variant() [9/11]

template<typename... Ts>
grem::Variant< Ts >::Variant ( const Variant< Ts > & other)
inlineconstexpr

Copy constructor.

◆ Variant() [10/11]

template<typename... Ts>
grem::Variant< Ts >::Variant ( Variant< Ts > && other)
constexprdefaultnoexcept

Trivial move constructor.

◆ Variant() [11/11]

template<typename... Ts>
grem::Variant< Ts >::Variant ( Variant< Ts > && other)
inlineconstexprnoexcept

Move constructor.

Member Function Documentation

◆ operator=() [1/6]

template<typename... Ts>
Variant & grem::Variant< Ts >::operator= ( const Variant< Ts > & other)
constexprdelete

Deleted copy assignment.

◆ operator=() [2/6]

template<typename... Ts>
Variant & grem::Variant< Ts >::operator= ( const Variant< Ts > & other)
constexprdefault

Trivial copy assignment.

◆ operator=() [3/6]

template<typename... Ts>
Variant & grem::Variant< Ts >::operator= ( const Variant< Ts > & other)
inlineconstexpr

Copy assignment.

Note
If an exception is thrown after the old value has been destroyed, the variant ends up in the valueless by exception state.

◆ operator=() [4/6]

template<typename... Ts>
Variant & grem::Variant< Ts >::operator= ( Variant< Ts > && other)
constexprdefaultnoexcept

Trivial move assignment.

◆ operator=() [5/6]

template<typename... Ts>
Variant & grem::Variant< Ts >::operator= ( Variant< Ts > && other)
inlineconstexprnoexcept

Move assignment.

Note
If an exception is thrown after the old value has been destroyed, the variant ends up in the valueless by exception state.

◆ operator=() [6/6]

template<typename... Ts>
template<typename U>
requires (!std::is_same_v<std::remove_cvref_t<U>, Variant> && variant_has_alternative_v<decltype(F(std::forward<U>(value))), Variant> && std::is_assignable_v<decltype(F(std::forward<U>(value))), U>)
Variant & grem::Variant< Ts >::operator= ( U && value)
inlineconstexprnoexcept

Converting assignment.

Chooses the variant alternative type to construct or assign based on the overloading rules specified by std::variant.

Parameters
valueunderlying value to construct the new value from. Must be convertible to one of the variant's listed alternative types.
Exceptions
anyexception thrown by the underlying constructor or assignment of the relevant alternative type.
Note
If an exception is thrown after the old value has been destroyed, the variant ends up in the valueless by exception state.

◆ emplace() [1/4]

template<typename... Ts>
template<typename T, typename... Args>
requires (variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, Args...>)
T & grem::Variant< Ts >::emplace ( Args &&... args)
inlineconstexpr

Construct an alternative given its type, destroying the old value.

Template Parameters
Tthe alternative type to construct. Must be one of the variant's listed alternative types.
Parameters
argsarguments to pass to the new value's constructor.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.
Note
If an exception is thrown after the old value has been destroyed, the variant ends up in the valueless by exception state.

◆ emplace() [2/4]

template<typename... Ts>
template<typename T, typename U, typename... Args>
requires (variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, std::initializer_list<U>&, Args...>)
T & grem::Variant< Ts >::emplace ( std::initializer_list< U > ilist,
Args &&... args )
inlineconstexpr

Construct an alternative given its type, with an initializer list as the first constructor argument, destroying the old value.

Template Parameters
Tthe alternative type to construct. Must be one of the variant's listed alternative types.
Parameters
ilistfirst argument to pass to the new value's constructor.
argssubsequent arguments to pass to the new value's constructor.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.
Note
If an exception is thrown after the old value has been destroyed, the variant ends up in the valueless by exception state.

◆ emplace() [3/4]

template<typename... Ts>
template<std::size_t Index, typename... Args>
requires (std::is_constructible_v<variant_alternative_t<Index, Variant>, Args...>)
auto & grem::Variant< Ts >::emplace ( Args &&... args)
inlineconstexpr

Construct an alternative given its index, destroying the old value.

Template Parameters
Indexthe alternative index to construct. Must be within the range of the variant's list of alternative types.
Parameters
argsarguments to pass to the new value's constructor.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.
Note
If an exception is thrown after the old value has been destroyed, the variant ends up in the valueless by exception state.

◆ emplace() [4/4]

template<typename... Ts>
template<std::size_t Index, typename U, typename... Args>
requires (std::is_constructible_v<variant_alternative_t<Index, Variant>, std::initializer_list<U>&, Args...>)
auto & grem::Variant< Ts >::emplace ( std::initializer_list< U > ilist,
Args &&... args )
inlineconstexpr

Construct an alternative given its index, with an initializer list as the first constructor argument, destroying the old value.

Template Parameters
Indexthe alternative index to construct. Must be within the range of the variant's list of alternative types.
Parameters
ilistfirst argument to pass to the new value's constructor.
argssubsequent arguments to pass to the new value's constructor.
Exceptions
anyexception thrown by the underlying constructor of the relevant alternative type.
Note
If an exception is thrown after the old value has been destroyed, the variant ends up in the valueless by exception state.

◆ swap()

template<typename... Ts>
void grem::Variant< Ts >::swap ( Variant< Ts > & other)
inlineconstexprnoexcept

Swap this variant's value with that of another.

Parameters
othervariant to swap with.
Exceptions
anyexception thrown by the underlying constructor, assignment or swap implementation of the relevant alternative types.
Note
If an exception is thrown after any value has been destroyed, and before it has been replaced with a new value, the associated variant ends up in the valueless by exception state.

◆ index()

template<typename... Ts>
index_type grem::Variant< Ts >::index ( ) const
inlinenodiscardconstexprnoexcept

Get the alternative index of the currently active value held by the variant.

Returns
the active alternative index, or npos if the variant is in the valueless by exception state.

◆ valueless_by_exception()

template<typename... Ts>
bool grem::Variant< Ts >::valueless_by_exception ( ) const
inlinenodiscardconstexprnoexcept

Check if the variant is in the valueless by exception state.

Returns
true if the variant is in the valueless by exception state, false otherwise.

◆ is() [1/2]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
bool grem::Variant< Ts >::is ( ) const
inlinenodiscardconstexprnoexcept

Check if the variant currently holds the alternative with the given type.

Template Parameters
Talternative type to check for. Must be one of the variant's listed alternative types.
Returns
true if the variant holds a value of the given type, false otherwise.

◆ is() [2/2]

template<typename... Ts>
template<std::size_t Index>
bool grem::Variant< Ts >::is ( ) const
inlinenodiscardconstexprnoexcept

Check if the variant currently holds the alternative with the given index.

Template Parameters
Indexalternative index to check for. Must be within the range of the variant's list of alternative types.
Returns
true if the variant holds a value of the given alternative index, false otherwise.

◆ as() [1/8]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
T & grem::Variant< Ts >::as ( ) &
inlinenodiscardconstexprnoexcept

Access the underlying value with the given type without a safety check.

Template Parameters
Ttype of the currently active value to get. Must be one of the variant's listed alternative types.
Returns
a reference to the value held by the variant.
Warning
The behavior of accessing the underlying value using the incorrect alternative type which is not currently active is undefined. The active alternative type must be known in advance in order to use this function safely, for example by checking if the variant contains the expected alternative using the is() function.
Remarks
Rather than using this function in conjunction with is(), it is usually more appropriate to use get() or to call get_if() and make sure that the returned pointer is not nullptr. This function is only meant for the cases where it is absolutely certain which alternative the variant currently holds.
See also
get()
get_if()

◆ as() [2/8]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
const T & grem::Variant< Ts >::as ( ) const &
inlinenodiscardconstexprnoexcept

Access the underlying value with the given type without a safety check.

Template Parameters
Ttype of the currently active value to get. Must be one of the variant's listed alternative types.
Returns
a read-only reference to the value held by the variant.
Warning
The behavior of accessing the underlying value using the incorrect alternative type which is not currently active is undefined. The active alternative type must be known in advance in order to use this function safely, for example by checking if the variant contains the expected alternative using the is() function.
Remarks
Rather than using this function in conjunction with is(), it is usually more appropriate to use get() or to call get_if() and make sure that the returned pointer is not nullptr. This function is only meant for the cases where it is absolutely certain which alternative the variant currently holds.
See also
get()
get_if()

◆ as() [3/8]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
T && grem::Variant< Ts >::as ( ) &&
inlinenodiscardconstexprnoexcept

Access the underlying value with the given type without a safety check.

Template Parameters
Ttype of the currently active value to get. Must be one of the variant's listed alternative types.
Returns
an rvalue reference to the value held by the variant.
Warning
The behavior of accessing the underlying value using the incorrect alternative type which is not currently active is undefined. The active alternative type must be known in advance in order to use this function safely, for example by checking if the variant contains the expected alternative using the is() function.
Remarks
Rather than using this function in conjunction with is(), it is usually more appropriate to use get() or to call get_if() and make sure that the returned pointer is not nullptr. This function is only meant for the cases where it is absolutely certain which alternative the variant currently holds.
See also
get()
get_if()

◆ as() [4/8]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
const T && grem::Variant< Ts >::as ( ) const &&
inlinenodiscardconstexprnoexcept

Access the underlying value with the given type without a safety check.

Template Parameters
Ttype of the currently active value to get. Must be one of the variant's listed alternative types.
Returns
a read-only rvalue reference to the value held by the variant.
Warning
The behavior of accessing the underlying value using the incorrect alternative type which is not currently active is undefined. The active alternative type must be known in advance in order to use this function safely, for example by checking if the variant contains the expected alternative using the is() function.
Remarks
Rather than using this function in conjunction with is(), it is usually more appropriate to use get() or to call get_if() and make sure that the returned pointer is not nullptr. This function is only meant for the cases where it is absolutely certain which alternative the variant currently holds.
See also
get()
get_if()

◆ as() [5/8]

template<typename... Ts>
template<std::size_t Index>
auto & grem::Variant< Ts >::as ( ) &
inlinenodiscardconstexprnoexcept

Access the underlying value with the given index without a safety check.

Template Parameters
Indexalternative index of the currently active value to get. Must be within the range of the variant's list of alternative types.
Returns
a reference to the value held by the variant.
Warning
The behavior of accessing the underlying value using the incorrect alternative type which is not currently active is undefined. The active alternative type must be known in advance in order to use this function safely, for example by checking if the variant contains the expected alternative using the is() function.
Remarks
Rather than using this function in conjunction with is(), it is usually more appropriate to use get() or to call get_if() and make sure that the returned pointer is not nullptr. This function is only meant for the cases where it is absolutely certain which alternative the variant currently holds.
See also
get()
get_if()

◆ as() [6/8]

template<typename... Ts>
template<std::size_t Index>
const auto & grem::Variant< Ts >::as ( ) const &
inlinenodiscardconstexprnoexcept

Access the underlying value with the given index without a safety check.

Template Parameters
Indexalternative index of the currently active value to get. Must be within the range of the variant's list of alternative types.
Returns
a read-only reference to the value held by the variant.
Warning
The behavior of accessing the underlying value using the incorrect alternative type which is not currently active is undefined. The active alternative type must be known in advance in order to use this function safely, for example by checking if the variant contains the expected alternative using the is() function.
Remarks
Rather than using this function in conjunction with is(), it is usually more appropriate to use get() or to call get_if() and make sure that the returned pointer is not nullptr. This function is only meant for the cases where it is absolutely certain which alternative the variant currently holds.
See also
get()
get_if()

◆ as() [7/8]

template<typename... Ts>
template<std::size_t Index>
auto && grem::Variant< Ts >::as ( ) &&
inlinenodiscardconstexprnoexcept

Access the underlying value with the given index without a safety check.

Template Parameters
Indexalternative index of the currently active value to get. Must be within the range of the variant's list of alternative types.
Returns
an rvalue reference to the value held by the variant.
Warning
The behavior of accessing the underlying value using the incorrect alternative type which is not currently active is undefined. The active alternative type must be known in advance in order to use this function safely, for example by checking if the variant contains the expected alternative using the is() function.
Remarks
Rather than using this function in conjunction with is(), it is usually more appropriate to use get() or to call get_if() and make sure that the returned pointer is not nullptr. This function is only meant for the cases where it is absolutely certain which alternative the variant currently holds.
See also
get()
get_if()

◆ as() [8/8]

template<typename... Ts>
template<std::size_t Index>
const auto && grem::Variant< Ts >::as ( ) const &&
inlinenodiscardconstexprnoexcept

Access the underlying value with the given index without a safety check.

Template Parameters
Indexalternative index of the currently active value to get. Must be within the range of the variant's list of alternative types.
Returns
a read-only rvalue reference to the value held by the variant.
Warning
The behavior of accessing the underlying value using the incorrect alternative type which is not currently active is undefined. The active alternative type must be known in advance in order to use this function safely, for example by checking if the variant contains the expected alternative using the is() function.
Remarks
Rather than using this function in conjunction with is(), it is usually more appropriate to use get() or to call get_if() and make sure that the returned pointer is not nullptr. This function is only meant for the cases where it is absolutely certain which alternative the variant currently holds.
See also
get()
get_if()

◆ get() [1/8]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
T & grem::Variant< Ts >::get ( ) &
inlinenodiscardconstexpr

Access the underlying value with the given type.

Template Parameters
Ttype of the currently active value to get. Must be one of the variant's listed alternative types.
Returns
a reference to the value held by the variant.
Exceptions
BadVariantAccessif the variant does not currently hold a value of the given type.
See also
as()
get_if()

◆ get() [2/8]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
const T & grem::Variant< Ts >::get ( ) const &
inlinenodiscardconstexpr

Access the underlying value with the given type.

Template Parameters
Ttype of the currently active value to get. Must be one of the variant's listed alternative types.
Returns
a read-only reference to the value held by the variant.
Exceptions
BadVariantAccessif the variant does not currently hold a value of the given type.
See also
as()
get_if()

◆ get() [3/8]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
T && grem::Variant< Ts >::get ( ) &&
inlinenodiscardconstexpr

Access the underlying value with the given type.

Template Parameters
Ttype of the currently active value to get. Must be one of the variant's listed alternative types.
Returns
an rvalue reference to the value held by the variant.
Exceptions
BadVariantAccessif the variant does not currently hold a value of the given type.
See also
as()
get_if()

◆ get() [4/8]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
const T && grem::Variant< Ts >::get ( ) const &&
inlinenodiscardconstexpr

Access the underlying value with the given type.

Template Parameters
Ttype of the currently active value to get. Must be one of the variant's listed alternative types.
Returns
a read-only rvalue reference to the value held by the variant.
Exceptions
BadVariantAccessif the variant does not currently hold a value of the given type.
See also
as()
get_if()

◆ get() [5/8]

template<typename... Ts>
template<std::size_t Index>
auto & grem::Variant< Ts >::get ( ) &
inlinenodiscardconstexpr

Access the underlying value with the given index.

Template Parameters
Indexalternative index of the currently active value to get. Must be within the range of the variant's list of alternative types.
Returns
a reference to the value held by the variant.
Exceptions
BadVariantAccessif the variant does not currently hold the given alternative.
See also
as()
get_if()

◆ get() [6/8]

template<typename... Ts>
template<std::size_t Index>
const auto & grem::Variant< Ts >::get ( ) const &
inlinenodiscardconstexpr

Access the underlying value with the given index.

Template Parameters
Indexalternative index of the currently active value to get. Must be within the range of the variant's list of alternative types.
Returns
a read-only reference to the value held by the variant.
Exceptions
BadVariantAccessif the variant does not currently hold the given alternative.
See also
as()
get_if()

◆ get() [7/8]

template<typename... Ts>
template<std::size_t Index>
auto && grem::Variant< Ts >::get ( ) &&
inlinenodiscardconstexpr

Access the underlying value with the given index.

Template Parameters
Indexalternative index of the currently active value to get. Must be within the range of the variant's list of alternative types.
Returns
an rvalue reference to the value held by the variant.
Exceptions
BadVariantAccessif the variant does not currently hold the given alternative.
See also
as()
get_if()

◆ get() [8/8]

template<typename... Ts>
template<std::size_t Index>
const auto && grem::Variant< Ts >::get ( ) const &&
inlinenodiscardconstexpr

Access the underlying value with the given index.

Template Parameters
Indexalternative index of the currently active value to get. Must be within the range of the variant's list of alternative types.
Returns
a read-only rvalue reference to the value held by the variant.
Exceptions
BadVariantAccessif the variant does not currently hold the given alternative.
See also
as()
get_if()

◆ get_if() [1/4]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
T * grem::Variant< Ts >::get_if ( )
inlinenodiscardconstexprnoexcept

Access the underlying value with the given type if it is the currently active alternative.

Template Parameters
Ttype of the value to get. Must be one of the variant's listed alternative types.
Returns
a non-owning pointer to the value held by the variant, or nullptr if the variant does not currently hold a value of the given type.
See also
as()
get()

◆ get_if() [2/4]

template<typename... Ts>
template<typename T>
requires (variant_has_alternative_v<T, Variant>)
const T * grem::Variant< Ts >::get_if ( ) const
inlinenodiscardconstexprnoexcept

Access the underlying value with the given type if it is the currently active alternative.

Template Parameters
Ttype of the value to get. Must be one of the variant's listed alternative types.
Returns
a non-owning read-only pointer to the value held by the variant, or nullptr if the variant does not currently hold a value of the given type.
See also
as()
get()

◆ get_if() [3/4]

template<typename... Ts>
template<std::size_t Index>
auto * grem::Variant< Ts >::get_if ( )
inlinenodiscardconstexprnoexcept

Access the underlying value with the given index if it is the currently active alternative.

Template Parameters
Indexalternative index of the value to get. Must be within the range of the variant's list of alternative types.
Returns
a non-owning pointer to the value held by the variant, or nullptr if the variant does not currently hold the given alternative.
See also
as()
get()

◆ get_if() [4/4]

template<typename... Ts>
template<std::size_t Index>
const auto * grem::Variant< Ts >::get_if ( ) const
inlinenodiscardconstexprnoexcept

Access the underlying value with the given index if it is the currently active alternative.

Template Parameters
Indexalternative index of the value to get. Must be within the range of the variant's list of alternative types.
Returns
a non-owning read-only pointer to the value held by the variant, or nullptr if the variant does not currently hold the given alternative.
See also
as()
get()

◆ visit()

template<typename... Ts>
template<typename Visitor, typename V>
constexpr decltype(auto) grem::Variant< Ts >::visit ( Visitor && visitor,
V && variant )
inlinestaticconstexpr

Call a visitor functor with the currently active underlying value of a variant.

Parameters
visitorcallable object that is overloaded to accept any of the variant alternatives as a parameter.
variantvariant whose underlying value to pass to the visitor.
Returns
the result of calling the visitor with the currently active variant alternative value.
Exceptions
BadVariantAccessif the variant is in the valueless by exception state.
anyexception thrown by the visitor.
See also
match()

◆ visitIndex()

template<typename... Ts>
template<typename Visitor>
constexpr decltype(auto) grem::Variant< Ts >::visitIndex ( index_type index,
Visitor && visitor )
inlinestaticconstexpr

Call a visitor functor with the type corresponding to a variant index.

Parameters
indexvariant alternative index of the visitor overload to call.
visitorcallable object that is overloaded to accept any of the variant alternative types wrapped in std::in_place_type_t as a parameter.
Returns
the result of calling the visitor with std::in_place_type<T>, where T is the variant alternative type corresponding to the given index.
Exceptions
BadVariantAccessif the given index is not a valid alternative index of the variant.
anyexception thrown by the visitor.

◆ apply()

template<typename... Ts>
template<typename Function>
constexpr decltype(auto) grem::Variant< Ts >::apply ( Function && function)
inlinestaticconstexpr

Call a functor with all alternative types.

Parameters
functioncallable object that accepts all of the variant alternative types wrapped in std::in_place_type_t as consecutive arguments.
Returns
the result of calling the function with the expansion of std::in_place_type<Ts>..., where Ts is the pack of variant alternative types.
Exceptions
anyexception thrown by the function.

◆ Variant

template<typename... Ts>
template<typename... Us>
friend class Variant
friend

JSON value type using the default allocator.

◆ swap

template<typename... Ts>
void swap ( Variant< Ts > & a,
Variant< Ts > & b )
friend

Swap the values of two variants.

Parameters
afirst variant.
bsecond variant.
Exceptions
anyexception thrown by the underlying constructor, assignment or swap implementation of the relevant alternative types.
Note
If an exception is thrown after any value has been destroyed, and before it has been replaced with a new value, the associated variant ends up in the valueless by exception state.

Member Data Documentation

◆ npos

template<typename... Ts>
index_type grem::Variant< Ts >::npos = sizeof...(Ts)
staticconstexpr

Invalid alternative index, representing the valueless by exception state.


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