标准库头文件 <variant> (C++17)
来自 cppreference.com
此头文件是 通用实用工具 库的一部分。
包含 | ||
(C++20) |
三路比较运算符 支持 | |
类 | ||
(C++17) |
类型安全的区分联合 (类模板) | |
(C++17) |
占位符类型,用作非默认可构造类型 variant 中的第一个备选方案(类) | |
(C++17) |
在对variant 的值进行无效访问时抛出的异常(类) | |
(C++17) |
在编译时获取variant 的备选方案列表的大小(类模板) (变量模板) | |
在编译时获取由其索引指定的备选方案的类型 (类模板) (别名模板) | ||
(C++17) |
对std::variant的哈希支持 (类模板特化) | |
常量 | ||
(C++17) |
variant 处于无效状态时的索引(常量) | |
函数 | ||
(C++17) |
使用一个或多个variant 持有的参数调用提供的函数对象(函数模板) | |
(C++17) |
检查variant 当前是否持有给定的类型(函数模板) | |
(C++17) |
根据索引或类型(如果类型是唯一的)读取变体的值,在错误时抛出异常 (函数模板) | |
(C++17) |
根据索引或类型(如果唯一)获取指向指向的variant 的值的指针,在错误时返回 null(函数模板) | |
(C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20) |
将variant 对象与其包含的值进行比较(函数模板) | |
(C++17) |
特化了std::swap算法 (函数模板) |
[编辑] 概要
#include <compare> namespace std { // class template 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> 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> 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...>&); template<class... Types> requires (three_way_comparable<Types> && ...) constexpr common_comparison_category_t<compare_three_way_result_t<Types>...> operator<=>(const variant<Types...>&, const variant<Types...>&); // visitation template<class Visitor, class... Variants> constexpr /* see description */ visit(Visitor&&, Variants&&...); template<class R, class Visitor, class... Variants> constexpr R visit(Visitor&&, Variants&&...); // class monostate struct monostate; // monostate relational operators constexpr bool operator==(monostate, monostate) noexcept; constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // specialized algorithms template<class... Types> constexpr void swap(variant<Types...>&, variant<Types...>&) noexcept(/* see description */); // 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>; } // deprecated namespace std { template<class T> struct variant_size<volatile T>; template<class T> struct variant_size<const volatile 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>; }
[编辑] 类模板 std::variant
namespace std { template<class... Types> class variant { public: // constructors constexpr variant() noexcept(/* see description */); constexpr variant(const variant&); constexpr variant(variant&&) noexcept(/* see description */); template<class T> constexpr variant(T&&) noexcept(/* see description */); 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&&...); // destructor constexpr ~variant(); // assignment constexpr variant& operator=(const variant&); constexpr variant& operator=(variant&&) noexcept(/* see description */); template<class T> constexpr variant& operator=(T&&) noexcept(/* see description */); // modifiers template<class T, class... Args> constexpr T& emplace(Args&&...); template<class T, class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...); template<size_t I, class... Args> constexpr variant_alternative_t<I, variant<Types...>>& emplace(Args&&...); template<size_t I, class U, class... Args> constexpr variant_alternative_t<I, variant<Types...>>& emplace(initializer_list<U>, Args&&...); // value status constexpr bool valueless_by_exception() const noexcept; constexpr size_t index() const noexcept; // swap constexpr void swap(variant&) noexcept(/* see description */); }; }
[编辑] 类 std::monostate
struct monostate{};
[编辑] 类 std::bad_variant_access
class bad_variant_access : public exception { public: // see [exception] for the specification of the special member functions const char* what() const noexcept override; };