CVB++ 15.0
variant< TS > Class Template Referencefinal

This class is a replacement for C++17 std::variant. More...

#include <cvb/shims/variant.hpp>

Public Member Functions

 variant () noexcept
 Default ctor creating a variant with default value of the first alternative type.
 
 ~variant () noexcept
 Destructor.
 
 variant (const variant &rhs)
 Copy ctor. More...
 
 variant (variant &&rhs) noexcept
 Move ctor. More...
 
variantoperator= (const variant &rhs)
 Copy operator. More...
 
variantoperator= (variant &&rhs)
 Move operator. More...
 
template<class T , typename std::enable_if<!std::is_same< T, variant & >::value, int >::type = 0>
 variant (T &&value)
 Ctor from an object of supported type T. More...
 
template<class T , typename std::enable_if<!std::is_same< T, variant & >::value, int >::type = 0>
variantoperator= (T &&value)
 Assignment from an object of supported type T. More...
 
template<size_t I, class... ARGS>
variant_alternative< I, variant< TS... > >::type & emplace (ARGS &&... args)
 Emplaces an object as alternative at index I. More...
 
template<class T , class... ARGS>
T & emplace (ARGS &&... args)
 Emplaces an object as a type T. More...
 
size_t index () const noexcept
 Gets the zero-based index of the alternative held by this instance.
 
void swap (variant &other)
 Swaps the content of this variant with the other one. More...
 
bool valueless_by_exception () const noexcept
 Returns false only if this instance holds a value. More...
 

Related Functions

(Note that these are not member functions.)

template<class T >
static const constexpr size_t variant_alternatives_size_v = variant_size<T>::value
 Get the number of alternatives in a variant.
 
template<size_t I, class... TS>
const variant_alternative_t< I, variant< TS... > > & get (const variant< TS... > &var)
 
template<size_t I, class... TS>
variant_alternative_t< I, variant< TS... > > & get (variant< TS... > &var)
 
template<size_t I, class... TS>
variant_alternative_t< I, variant< TS... > > get (variant< TS... > &&var)
 
template<class T , class... TS>
const T & get (const variant< TS... > &var)
 
template<class T , class... TS>
T & get (variant< TS... > &var)
 
template<class T , class... TS>
get (variant< TS... > &&var)
 
template<size_t I, class... TS>
std::add_pointer< variant_alternative_t< I, variant< TS... > > >::type get_if (variant< TS... > *pvar) noexcept
 
template<class T , class... TS>
std::add_pointer< constT >::type get_if (const variant< TS... > *pvar) noexcept
 
template<class T , class... TS>
std::add_pointer< T >::type get_if (variant< TS... > *pvar) noexcept
 
template<class T , class... TS>
bool holds_alternative (const variant< TS... > &var) noexcept
 Gets whether the Shims::variant var holds an instance of type T. More...
 
template<class VISITOR , class VARIANT >
auto visit (VISITOR &&visitor, VARIANT &&var) -> decltype(visitor(get< 0 >(var)))
 Visits the given Shims::variant var. Cvb::Shims::visit can only visit one variant (not multiple like std::visit). More...
 
template<class... TS>
bool operator== (const variant< TS... > &lhs, const variant< TS... > &rhs)
 Equality operator for Shims::variant objects. More...
 
template<class... TS>
bool operator!= (const variant< TS... > &lhs, const variant< TS... > &rhs)
 Inequality operator for Shims::variant objects. More...
 
template<class... TS>
bool operator< (const variant< TS... > &lhs, const variant< TS... > &rhs)
 Less than operator for Shims::variant objects. More...
 
template<class... TS>
bool operator<= (const variant< TS... > &lhs, const variant< TS... > &rhs)
 Less-than or equal operator for Shims::variant object. More...
 
template<class... TS>
bool operator> (const variant< TS... > &lhs, const variant< TS... > &rhs)
 Greater-than operator for Shims::variant object. More...
 
template<class... TS>
bool operator>= (const variant< TS... > &lhs, const variant< TS... > &rhs)
 Greater-than or equal operator for Shims::variant object. More...
 

Detailed Description

template<class... TS>
class Cvb::Shims::variant< TS >

This class is a replacement for C++17 std::variant.

Constructor & Destructor Documentation

◆ variant() [1/3]

variant ( const variant< TS > &  rhs)

Copy ctor.

Parameters
[in]rhsObject to copy.

◆ variant() [2/3]

variant ( variant< TS > &&  rhs)
noexcept

Move ctor.

Parameters
[in]rhsObject to move.

◆ variant() [3/3]

variant ( T &&  value)
inline

Ctor from an object of supported type T.

Template Parameters
TType of value to set.
Parameters
[in]valueThe value to store in this variant.

Member Function Documentation

◆ emplace() [1/2]

variant_alternative< I, variant< TS... > >::type & emplace ( ARGS &&...  args)

Emplaces an object as alternative at index I.

Template Parameters
IIndex of alternative to use.
Parameters
[in]argsArguments to be forwarded to alternative's ctor.
Returns
Reference to the newly created type.

◆ emplace() [2/2]

T & emplace ( ARGS &&...  args)
inline

Emplaces an object as a type T.

Template Parameters
TType of alternative to use.
Parameters
[in]argsArguments to be forwarded to alternative's ctor.
Returns
Reference to the newly created type.

◆ operator=() [1/3]

variant< TS... > & operator= ( const variant< TS > &  rhs)

Copy operator.

Parameters
[in]rhsObject to copy.
Returns
Reference to this object.

◆ operator=() [2/3]

variant & operator= ( T &&  value)
inline

Assignment from an object of supported type T.

Note
If assignment of the value fails this instance is in invalid state. I.e. valueless_by_exception() returns true.
Template Parameters
TType of value to set.
Parameters
[in]valueThe value to store in this variant.
Returns
Reference to this object.

◆ operator=() [3/3]

variant< TS... > & operator= ( variant< TS > &&  rhs)

Move operator.

Parameters
[in]rhsObject to move.
Returns
Reference to this object.

◆ swap()

void swap ( variant< TS > &  other)

Swaps the content of this variant with the other one.

Parameters
[in]otherOther object to swap content with.

◆ valueless_by_exception()

bool valueless_by_exception ( ) const
inlinenoexcept

Returns false only if this instance holds a value.

Returns
false if this instance holds a value; true otherwise.

Friends And Related Function Documentation

◆ get() [1/6]

const variant_alternative_t< I, variant< TS... > > & get ( const variant< TS... > &  var)
related

Gets the value of the given Shims::variant var as the alternative with index I.

Template Parameters
IIndex of alternative to extract.
Parameters
[in]varVariant to get the value of.
Returns
The value in var.
Exceptions
bad_variant_accessthrown if I is unequal variant::index().

◆ get() [2/6]

const T & get ( const variant< TS... > &  var)
related

Gets the value of the given Shims::variant var as the alternative with type T.

Template Parameters
TType to query.
Parameters
[in]varVariant to get the value of.
Returns
The value in var.
Exceptions
bad_variant_accessthrown if T is not a variant alternative.

◆ get() [3/6]

variant_alternative_t< I, variant< TS... > > get ( variant< TS... > &&  var)
related

Gets the value of the given Shims::variant var as the alternative with index I.

Template Parameters
IIndex of alternative to extract.
Parameters
[in]varVariant to get the value of.
Returns
The value in var.
Exceptions
bad_variant_accessthrown if I is unequal variant::index().

◆ get() [4/6]

T get ( variant< TS... > &&  var)
related

Gets the value of the given Shims::variant var as the alternative with type T.

Template Parameters
TType to query.
Parameters
[in]varVariant to get the value of.
Returns
The value in var.
Exceptions
bad_variant_accessthrown if T is not a variant alternative.

◆ get() [5/6]

variant_alternative_t< I, variant< TS... > > & get ( variant< TS... > &  var)
related

Gets the value of the given Shims::variant var as the alternative with index I.

Template Parameters
IIndex of alternative to extract.
Parameters
[in]varVariant to get the value of.
Returns
The value in var.
Exceptions
bad_variant_accessthrown if I is unequal variant::index().

◆ get() [6/6]

T & get ( variant< TS... > &  var)
related

Gets the value of the given Shims::variant var as the alternative with type T.

Template Parameters
TType to query.
Parameters
[in]varVariant to get the value of.
Returns
The value in var.
Exceptions
bad_variant_accessthrown if T is not a variant alternative.

◆ get_if() [1/3]

std::add_pointer< constT >::type get_if ( const variant< TS... > *  pvar)
related

Tries to get the value of the given Shims::variant pointer pvar as the alternative with type T.

Template Parameters
TType to query.
Parameters
[in]pvarVariant to get the value of.
Returns
Pointer to the value in pvar; nullptr if pvar is nullptr or if the stored alternative is not of type T.

◆ get_if() [2/3]

std::add_pointer< variant_alternative_t< I, variant< TS... > > >::type get_if ( variant< TS... > *  pvar)
related

Tries to get the value of the given Shims::variant pointer pvar as the alternative with index I.

Template Parameters
IIndex of alternative to extract.
Parameters
[in]pvarVariant to get the value of.
Returns
Pointer to the value in pvar; nullptr if pvar is nullptr or I is unequal pvar object's index().

◆ get_if() [3/3]

std::add_pointer< T >::type get_if ( variant< TS... > *  pvar)
related

Tries to get the value of the given Shims::variant pointer pvar as the alternative with type T.

Template Parameters
TType to query.
Parameters
[in]pvarVariant to get the value of.
Returns
Pointer to the value in pvar; nullptr if pvar is nullptr or if the stored alternative is not of type T.

◆ holds_alternative()

bool holds_alternative ( const variant< TS... > &  var)
related

Gets whether the Shims::variant var holds an instance of type T.

Template Parameters
TType to test.
Parameters
[in]varVariant to check.
Returns
true if var currently holds a type T; false if not.

◆ operator!=()

bool operator!= ( const variant< TS... > &  lhs,
const variant< TS... > &  rhs 
)
related

Inequality operator for Shims::variant objects.

Parameters
[in]lhsLeft hand side object.
[in]rhsRight hand side object.
Returns
true if both object store the same variant::index() or if they are both not variant::valueless_by_exception and the actual values are unequal.

◆ operator<()

bool operator< ( const variant< TS... > &  lhs,
const variant< TS... > &  rhs 
)
related

Less than operator for Shims::variant objects.

Parameters
[in]lhsLeft hand side object.
[in]rhsRight hand side object.
Returns

◆ operator<=()

bool operator<= ( const variant< TS... > &  lhs,
const variant< TS... > &  rhs 
)
related

Less-than or equal operator for Shims::variant object.

Parameters
[in]lhsLeft hand side object.
[in]rhsRight hand side object.
Returns
Not rhs less-than lhs comparison.

◆ operator==()

bool operator== ( const variant< TS... > &  lhs,
const variant< TS... > &  rhs 
)
related

Equality operator for Shims::variant objects.

Parameters
[in]lhsLeft hand side object.
[in]rhsRight hand side object.
Returns
true if both objects store the same variant::index() and if they are both variant::valueless_by_exception() or the actual values are equal.

◆ operator>()

bool operator> ( const variant< TS... > &  lhs,
const variant< TS... > &  rhs 
)
related

Greater-than operator for Shims::variant object.

Parameters
[in]lhsLeft hand side object.
[in]rhsRight hand side object.
Returns
rhs less-than lhs comparison.

◆ operator>=()

bool operator>= ( const variant< TS... > &  lhs,
const variant< TS... > &  rhs 
)
related

Greater-than or equal operator for Shims::variant object.

Parameters
[in]lhsLeft hand side object.
[in]rhsRight hand side object.
Returns
Not lhs less-than rhs comparison.

◆ visit()

auto visit ( VISITOR &&  visitor,
VARIANT &&  var 
) -> decltype(visitor(get<0>(var)))
related

Visits the given Shims::variant var. Cvb::Shims::visit can only visit one variant (not multiple like std::visit).

Template Parameters
VISITORCallable that get the value of the variant as argument.
TSParameter pack of the Shims::variant alternatives.
Parameters
[in]visitorCallable to visit the variant var.
[in]varVariant to visit.
Returns
Type depends on visitor.