SpECTRE  v2024.08.03
variants::TaggedVariant< Tags > Class Template Reference

A class similar to std::variant, but indexed by tag structs. More...

#include <TaggedVariant.hpp>

Public Member Functions

 TaggedVariant ()=default
 A default constructed instance has the first tag active.
 
 TaggedVariant (const TaggedVariant &)=default
 
 TaggedVariant (TaggedVariant &&)=default
 
TaggedVariantoperator= (const TaggedVariant &)=default
 
TaggedVariantoperator= (TaggedVariant &&)=default
 
template<typename Tag , typename... Args, Requires<(... or std::is_same_v< Tag, Tags >) and std::is_constructible_v< typename Tag::type, Args... > > = nullptr>
constexpr TaggedVariant (std::in_place_type_t< Tag >, Args &&... args)
 Construct with Tag active, using args to construct the contained object. More...
 
template<typename... Args, Requires< sizeof...(Tags)==1 and std::is_constructible_v< typename tmpl::front< TaggedVariant >::type, Args... > > = nullptr>
constexpr TaggedVariant (Args &&... args)
 Construct the contained object from args. Only available if the TaggedVariant only has one tag. More...
 
constexpr size_t index () const
 The index into the Tags... of the active object.
 
constexpr bool valueless_by_exception () const
 See std::variant::valueless_by_exception.
 
template<typename Tag , typename... Args, Requires<(... or std::is_same_v< Tag, Tags >) and std::is_constructible_v< typename Tag::type, Args... > > = nullptr>
constexpr Tag::type & emplace (Args &&... args)
 Destroys the contained object and actives Tag, constructing a new value from args. More...
 
constexpr void swap (TaggedVariant &other) noexcept(noexcept((... and(std::is_nothrow_move_constructible_v< typename Tags::type > and std::is_nothrow_swappable_v< typename Tags::type >))))
 
void pup (PUP::er &p)
 
template<typename... OtherTags, Requires< tmpl::size< tmpl::list_difference< TaggedVariant< OtherTags... >, TaggedVariant< Tags... > > >::value==0 > >
constexpr TaggedVariant< Tags... > & operator= (TaggedVariant< OtherTags... > &&other)
 
template<typename... OtherTags, Requires< tmpl::size< tmpl::list_difference< TaggedVariant< OtherTags... >, TaggedVariant > >::value==0 > = nullptr>
constexpr TaggedVariant (TaggedVariant< OtherTags... > &&other)
 A TaggedVariant can be implicitly move-converted to another variant with a superset of the tags. More...
 
template<typename... OtherTags, Requires< tmpl::size< tmpl::list_difference< TaggedVariant< OtherTags... >, TaggedVariant > >::value==0 > = nullptr>
constexpr TaggedVariantoperator= (TaggedVariant< OtherTags... > &&other)
 A TaggedVariant can be implicitly move-converted to another variant with a superset of the tags. More...
 

Friends

template<typename Tag , typename... Tags2>
constexpr Tag::type & get (TaggedVariant< Tags2... > &variant)
 
template<typename Tag , typename... Tags2>
constexpr const Tag::type & get (const TaggedVariant< Tags2... > &variant)
 
template<typename Tag , typename... Tags2>
constexpr Tag::type && get (TaggedVariant< Tags2... > &&variant)
 
template<typename Tag , typename... Tags2>
constexpr const Tag::type && get (const TaggedVariant< Tags2... > &&variant)
 
constexpr bool operator== (const TaggedVariant< Tags... > &a, const TaggedVariant< Tags... > &b)
 
constexpr bool operator (const TaggedVariant< Tags... > &a, const TaggedVariant< Tags... > &b)
 
using options = tmpl::list< Options::Alternatives< tmpl::list< Tags >... > >
 A TaggedVariant over option tags can be parsed as any of them.
 
static constexpr Options::String help = "One of multiple options"
 A TaggedVariant over option tags can be parsed as any of them.
 
template<typename Tag >
 TaggedVariant (tmpl::list< Tag >, typename Tag::type value)
 A TaggedVariant over option tags can be parsed as any of them.
 

Detailed Description

template<typename... Tags>
class variants::TaggedVariant< Tags >

A class similar to std::variant, but indexed by tag structs.

See also
variants::get, variants::get_if, variants::holds_alternative, variants::visit

Constructor & Destructor Documentation

◆ TaggedVariant() [1/3]

template<typename... Tags>
template<typename Tag , typename... Args, Requires<(... or std::is_same_v< Tag, Tags >) and std::is_constructible_v< typename Tag::type, Args... > > = nullptr>
constexpr variants::TaggedVariant< Tags >::TaggedVariant ( std::in_place_type_t< Tag >  ,
Args &&...  args 
)
inlineexplicitconstexpr

Construct with Tag active, using args to construct the contained object.

int_variant(std::in_place_type<Tags::Int>, 1);
A class similar to std::variant, but indexed by tag structs.
Definition: TaggedVariant.hpp:57

◆ TaggedVariant() [2/3]

template<typename... Tags>
template<typename... Args, Requires< sizeof...(Tags)==1 and std::is_constructible_v< typename tmpl::front< TaggedVariant >::type, Args... > > = nullptr>
constexpr variants::TaggedVariant< Tags >::TaggedVariant ( Args &&...  args)
inlineexplicitconstexpr

Construct the contained object from args. Only available if the TaggedVariant only has one tag.

constexpr variants::TaggedVariant<Tags::Int> simple_variant2(4);

◆ TaggedVariant() [3/3]

template<typename... Tags>
template<typename... OtherTags, Requires< tmpl::size< tmpl::list_difference< TaggedVariant< OtherTags... >, TaggedVariant< Tags... > > >::value==0 > >
constexpr variants::TaggedVariant< Tags >::TaggedVariant ( TaggedVariant< OtherTags... > &&  other)
constexpr

A TaggedVariant can be implicitly move-converted to another variant with a superset of the tags.

std::in_place_type<Tags::Int>, 4);
Tags::Int>
complex_converted(std::move(input));
return get<Tags::Int>(complex_converted) == 4;

Member Function Documentation

◆ emplace()

template<typename... Tags>
template<typename Tag , typename... Args, Requires<(... or std::is_same_v< Tag, Tags >) and std::is_constructible_v< typename Tag::type, Args... > > = nullptr>
constexpr Tag::type & variants::TaggedVariant< Tags >::emplace ( Args &&...  args)
inlineconstexpr

Destroys the contained object and actives Tag, constructing a new value from args.

decltype(auto) emplace_result =
variant.emplace<Tags::Templated<double>>(1.23);
constexpr Tag::type & emplace(Args &&... args)
Destroys the contained object and actives Tag, constructing a new value from args.
Definition: TaggedVariant.hpp:138

◆ operator=()

template<typename... Tags>
template<typename... OtherTags, Requires< tmpl::size< tmpl::list_difference< TaggedVariant< OtherTags... >, TaggedVariant > >::value==0 > = nullptr>
constexpr TaggedVariant & variants::TaggedVariant< Tags >::operator= ( TaggedVariant< OtherTags... > &&  other)
constexpr

A TaggedVariant can be implicitly move-converted to another variant with a superset of the tags.

std::in_place_type<Tags::Int>, 4);
Tags::Int>
complex_converted(std::move(input));
return get<Tags::Int>(complex_converted) == 4;

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