SpECTRE  v2024.08.03
variants Namespace Reference

TaggedVariant and related functionality. More...

Classes

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

Functions

template<typename Tag , typename... Tags>
constexpr bool holds_alternative (const TaggedVariant< Tags... > &variant)
 Check whether Tag is active.
 
template<typename... Tags>
constexpr bool operator== (const TaggedVariant< Tags... > &a, const TaggedVariant< Tags... > &b)
 
template<typename... Tags>
constexpr bool operator!= (const TaggedVariant< Tags... > &a, const TaggedVariant< Tags... > &b)
 
template<typename... Tags>
constexpr bool operator< (const TaggedVariant< Tags... > &a, const TaggedVariant< Tags... > &b)
 
template<typename... Tags>
constexpr bool operator> (const TaggedVariant< Tags... > &a, const TaggedVariant< Tags... > &b)
 
template<typename... Tags>
constexpr bool operator<= (const TaggedVariant< Tags... > &a, const TaggedVariant< Tags... > &b)
 
template<typename... Tags>
constexpr bool operator>= (const TaggedVariant< Tags... > &a, const TaggedVariant< Tags... > &b)
 
template<typename... Tags, Requires<(... and(std::is_move_constructible_v< typename Tags::type > and std::is_swappable_v< typename Tags::type >))> = nullptr>
constexpr void swap (TaggedVariant< Tags... > &a, TaggedVariant< Tags... > &b) noexcept(noexcept(a.swap(b)))
 
template<typename Visitor , typename... Variants, Requires<(... and TaggedVariant_detail::is_variant_or_derived(std::add_pointer_t< std::remove_reference_t< Variants > >{}))> = nullptr>
constexpr decltype(auto) visit (Visitor &&visitor, Variants &&... variants)
 Call visitor with the contents of one or more variants. More...
 
template<typename R , typename Visitor , typename... Variants, Requires<(... and TaggedVariant_detail::is_variant_or_derived(std::add_pointer_t< std::remove_reference_t< Variants > >{}))> = nullptr>
constexpr R visit (Visitor &&visitor, Variants &&... variants)
 Call visitor with the contents of one or more variants. More...
 
template<typename Tag , typename... Tags>
constexpr Tag::type & get (TaggedVariant< Tags... > &variant)
 Access the contained object. Throws std::bad_variant_access if Tag is not active.
 
template<typename Tag , typename... Tags>
constexpr const Tag::type & get (const TaggedVariant< Tags... > &variant)
 Access the contained object. Throws std::bad_variant_access if Tag is not active.
 
template<typename Tag , typename... Tags>
constexpr Tag::type && get (TaggedVariant< Tags... > &&variant)
 Access the contained object. Throws std::bad_variant_access if Tag is not active.
 
template<typename Tag , typename... Tags>
constexpr const Tag::type && get (const TaggedVariant< Tags... > &&variant)
 Access the contained object. Throws std::bad_variant_access if Tag is not active.
 
template<typename Tag , typename... Tags>
constexpr const Tag::type * get_if (const TaggedVariant< Tags... > *variant)
 Returns a pointer to the contained object if variant is a non-null pointer and Tag is active. Otherwise, returns nullptr.
 
template<typename Tag , typename... Tags>
constexpr Tag::type * get_if (TaggedVariant< Tags... > *variant)
 Returns a pointer to the contained object if variant is a non-null pointer and Tag is active. Otherwise, returns nullptr.
 

Detailed Description

TaggedVariant and related functionality.

Function Documentation

◆ visit() [1/2]

template<typename Visitor , typename... Variants, Requires<(... and TaggedVariant_detail::is_variant_or_derived(std::add_pointer_t< std::remove_reference_t< Variants > >{}))> = nullptr>
constexpr decltype(auto) variants::visit ( Visitor &&  visitor,
Variants &&...  variants 
)
constexpr

Call visitor with the contents of one or more variants.

Calls visitor with the contents of each variant as arguments, passed as std::pair<tmpl::type_<Tag>, typename Tag::type ref>, where Tag is the active tag of the variant and ref is a reference qualifier matching that of the passed variant.

If the template parameter R is supplied, the result is implicitly converted to that type (which may be void). Otherwise it is deduced from the return type of visitor, which must be the same for all tags in the variant.

Warning
Unlike visit for std::variant, the types of the visitor arguments do not allow for implicit conversions between reference types. If the visitor expects, for example, std::pair<tmpl::type_<Tag>, const typename Tag::type&>, the caller must ensure that the passed variant is a const lvalue.
visit_example(std::in_place_type<Tags::Int>, 5);
CHECK(visit(
[]<typename Tag>(
const std::pair<tmpl::type_<Tag>, const typename Tag::type&>&
entry) {
if constexpr (std::is_same_v<Tag, Tags::Int>) {
// Deduced return types must match for all tags if
// no template argument is given to visit.
return static_cast<double>(entry.second + 1);
} else {
static_assert(std::is_same_v<Tag, Tags::Templated<double>>);
return entry.second;
}
},
visit_example) == 6.0);
std::in_place_type<Tags::Templated<double>>, 1.23);
CHECK(visit<double>(
[]<typename Tag>(const std::pair<tmpl::type_<Tag>,
typename Tag::type&&>& entry) {
if constexpr (std::is_same_v<Tag, Tags::Int>) {
// Implicitly converted to double because of
// template argument of visit.
return entry.second + 1;
} else {
static_assert(std::is_same_v<Tag, Tags::Templated<double>>);
return entry.second;
}
},
std::move(visit_example2)) == 1.23);
A class similar to std::variant, but indexed by tag structs.
Definition: TaggedVariant.hpp:57
constexpr decltype(auto) visit(Visitor &&visitor, Variants &&... variants)
Call visitor with the contents of one or more variants.
Definition: TaggedVariant.hpp:309

◆ visit() [2/2]

template<typename R , typename Visitor , typename... Variants, Requires<(... and TaggedVariant_detail::is_variant_or_derived(std::add_pointer_t< std::remove_reference_t< Variants > >{}))> = nullptr>
constexpr R variants::visit ( Visitor &&  visitor,
Variants &&...  variants 
)
constexpr

Call visitor with the contents of one or more variants.

Calls visitor with the contents of each variant as arguments, passed as std::pair<tmpl::type_<Tag>, typename Tag::type ref>, where Tag is the active tag of the variant and ref is a reference qualifier matching that of the passed variant.

If the template parameter R is supplied, the result is implicitly converted to that type (which may be void). Otherwise it is deduced from the return type of visitor, which must be the same for all tags in the variant.

Warning
Unlike visit for std::variant, the types of the visitor arguments do not allow for implicit conversions between reference types. If the visitor expects, for example, std::pair<tmpl::type_<Tag>, const typename Tag::type&>, the caller must ensure that the passed variant is a const lvalue.
visit_example(std::in_place_type<Tags::Int>, 5);
CHECK(visit(
[]<typename Tag>(
const std::pair<tmpl::type_<Tag>, const typename Tag::type&>&
entry) {
if constexpr (std::is_same_v<Tag, Tags::Int>) {
// Deduced return types must match for all tags if
// no template argument is given to visit.
return static_cast<double>(entry.second + 1);
} else {
static_assert(std::is_same_v<Tag, Tags::Templated<double>>);
return entry.second;
}
},
visit_example) == 6.0);
std::in_place_type<Tags::Templated<double>>, 1.23);
CHECK(visit<double>(
[]<typename Tag>(const std::pair<tmpl::type_<Tag>,
typename Tag::type&&>& entry) {
if constexpr (std::is_same_v<Tag, Tags::Int>) {
// Implicitly converted to double because of
// template argument of visit.
return entry.second + 1;
} else {
static_assert(std::is_same_v<Tag, Tags::Templated<double>>);
return entry.second;
}
},
std::move(visit_example2)) == 1.23);