variant
标准库标头<variant>
班
variant (since C++17) | a type-safe discriminated union (class template) |
---|---|
monostate (C++17) | placeholder type for use as the first alternative in a variant of non-default-contructible types (class) |
bad_variant_access (C++17) | exception thrown on invalid accesses to the value of a variant (class) |
variant_sizevariant_size_v (C++17) | obtains the size of the variant's list of alternatives at compile time (class template) (variable template) |
variant_alternativevariant_alternative_t (C++17) | obtains the type of the alternative specified by its index, at compile time (class template) (alias template) |
std::hash<std::variant> (C++17) | specializes the std::hash algorithm (class template specialization) |
std::uses_allocator<std::variant> (C++17) | specializes the std::uses_allocator type trait (class template specialization) |
帮手
variant_npos (C++17) | index of the variant in the invalid state (constant) |
---|
功能
visit (C++17) | calls the provided functor with the arguments held by one or more variants (function template) |
---|---|
holds_alternative (C++17) | checks if a given type appears exactly once in a variant (function template) |
std::get(std::variant) (C++17) | reads the value of the variant given the index or the type (if the type is unique), throws on error (function template) |
get_if (C++17) | obtains a pointer to the value of a pointed-to variant given the index or the type (if unique), returns null on error (function template) |
operator==operator!=operator<operator<=operator>operator>= (C++17) | compares variant objects as their contained values (function template) |
std::swap(std::variant) (C++17) | specializes the std::swap algorithm (function) |
简介
二次
namespace std {
// variant
template <class... Types> class variant;
// variant helper classes
template <class T> struct variant_size; // not defined
template <class T> struct variant_size<const T>;
template <class T> struct variant_size<volatile T>;
template <class T> struct variant_size<const volatile T>;
template <class T> inline constexpr size_t variant_size_v = variant_size<T>::value;
template <class... Types>
struct variant_size<variant<Types...>>;
template <size_t I, class T> struct variant_alternative; // not defined
template <size_t I, class T> struct variant_alternative<I, const T>;
template <size_t I, class T> struct variant_alternative<I, volatile T>;
template <size_t I, class T> struct variant_alternative<I, const volatile T>;
template <size_t I, class T>
using variant_alternative_t = typename variant_alternative<I, T>::type;
template <size_t I, class... Types>
struct variant_alternative<I, variant<Types...>>;
inline constexpr size_t variant_npos = -1;
// value access
template <class T, class... Types>
constexpr bool holds_alternative(const variant<Types...>&) noexcept;
template <size_t I, class... Types>
constexpr variant_alternative_t<I, variant<Types...>>&
get(variant<Types...>&
template <size_t I, class... Types>
constexpr variant_alternative_t<I, variant<Types...>>&&
get(variant<Types...>&&
template <size_t I, class... Types>
constexpr const variant_alternative_t<I, variant<Types...>>&
get(const variant<Types...>&
template <size_t I, class... Types>
constexpr const variant_alternative_t<I, variant<Types...>>&&
get(const variant<Types...>&&
template <class T, class... Types>
constexpr T& get(variant<Types...>&
template <class T, class... Types>
constexpr T&& get(variant<Types...>&&
template <class T, class... Types>
constexpr const T& get(const variant<Types...>&
template <class T, class... Types>
constexpr const T&& get(const variant<Types...>&&
template <size_t I, class... Types>
constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
get_if(variant<Types...>*) noexcept;
template <size_t I, class... Types>
constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
get_if(const variant<Types...>*) noexcept;
template <class T, class... Types>
constexpr add_pointer_t<T> get_if(variant<Types...>*) noexcept;
template <class T, class... Types>
constexpr add_pointer_t<const T> get_if(const variant<Types...>*) noexcept;
// relational operators
template <class... Types>
constexpr bool operator==(const variant<Types...>&,
const variant<Types...>&
template <class... Types>
constexpr bool operator!=(const variant<Types...>&,
const variant<Types...>&
template <class... Types>
constexpr bool operator<(const variant<Types...>&,
const variant<Types...>&
template <class... Types>
constexpr bool operator>(const variant<Types...>&,
const variant<Types...>&
template <class... Types>
constexpr bool operator<=(const variant<Types...>&,
const variant<Types...>&
template <class... Types>
constexpr bool operator>=(const variant<Types...>&,
const variant<Types...>&
// visitation
template <class Visitor, class... Variants>
constexpr /*see definition*/ visit(Visitor&&, Variants&&...
// class monostate
struct monostate;
// monostate relational operators
constexpr bool operator<(monostate, monostate) noexcept;
constexpr bool operator>(monostate, monostate) noexcept;
constexpr bool operator<=(monostate, monostate) noexcept;
constexpr bool operator>=(monostate, monostate) noexcept;
constexpr bool operator==(monostate, monostate) noexcept;
constexpr bool operator!=(monostate, monostate) noexcept;
// specialized algorithms
template <class... Types>
void swap(variant<Types...>&, variant<Types...>&) noexcept(/*see definition*/
// class bad_variant_access
class bad_variant_access;
// hash support
template <class T> struct hash;
template <class... Types> struct hash<variant<Types...>>;
template <> struct hash<monostate>;
// allocator-related traits
template <class T, class Alloc> struct uses_allocator;
template <class... Types, class Alloc>
struct uses_allocator<variant<Types...>, Alloc>;
}
二次
类模板std::variant
二次
template <class... Types>
class variant {
public:
// constructors
constexpr variant() noexcept(/*see definition*/
variant(const variant&
variant(variant&&) noexcept(/*see definition*/
template <class T> constexpr variant(T&&) noexcept(/*see definition*/
template <class T, class... Args>
constexpr explicit variant(in_place_type_t<T>, Args&&...
template <class T, class U, class... Args>
constexpr explicit variant(in_place_type_t<T>, initializer_list<U>, Args&&...
template <size_t I, class... Args>
constexpr explicit variant(in_place_index_t<I>, Args&&...
template <size_t I, class U, class... Args>
constexpr explicit variant(in_place_index_t<I>, initializer_list<U>, Args&&...
// allocator-extended constructors
template <class Alloc>
variant(allocator_arg_t, const Alloc&
template <class Alloc>
variant(allocator_arg_t, const Alloc&, const variant&
template <class Alloc>
variant(allocator_arg_t, const Alloc&, variant&&
template <class Alloc, class T>
variant(allocator_arg_t, const Alloc&, T&&
template <class Alloc, class T, class... Args>
variant(allocator_arg_t, const Alloc&, in_place_type_t<T>, Args&&...
template <class Alloc, class T, class U, class... Args>
variant(allocator_arg_t, const Alloc&, in_place_type_t<T>,
initializer_list<U>, Args&&...
template <class Alloc, size_t I, class... Args>
variant(allocator_arg_t, const Alloc&, in_place_index_t<I>, Args&&...
template <class Alloc, size_t I, class U, class... Args>
variant(allocator_arg_t, const Alloc&, in_place_index_t<I>,
initializer_list<U>, Args&&...
// destructor
~variant(
// assignment
variant& operator=(const variant&
variant& operator=(variant&&) noexcept(/*see definition*/
template <class T> variant& operator=(T&&) noexcept(/*see definition*/
// modifiers
template <class T, class... Args> void emplace(Args&&...
template <class T, class U, class... Args>
void emplace(initializer_list<U>, Args&&...
template <size_t I, class... Args> void emplace(Args&&...
template <size_t I, class U, class... Args>
void emplace(initializer_list<U>, Args&&...
// value status
constexpr bool valueless_by_exception() const noexcept;
constexpr size_t index() const noexcept;
// swap
void swap(variant&) noexcept(/*see definition*/
};
二次
类std::monostate
二次
struct monostate{};
二次
类std::bad_variant_access
二次
class bad_variant_access : public exception {
public:
bad_variant_access() noexcept;
virtual const char* what() const noexcept;
};
二次
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。