在线文档教程
C++
应用 | Utilities

std::variant

性病:变体

Defined in header
template <class... Types> class variant;(since C++17)

类模板std::variant表示类型安全。联合.一个实例std::variant在任何给定的时间内,要么持有其替代类型之一的值,要么不保存值%28,此状态很难实现,请参见valueless_by_exception29%。

与联合一样,如果变量持有某种对象类型的值。T的对象表示T直接在变体本身的对象表示中分配。变量不允许分配额外的%28动态%29内存。

不允许变体保存引用、数组或类型。void空变体也是格式错误的%28。std::variant<std::monostate>可以代替%29使用。

允许变体持有同一类型多次,并持有不同的cv-限定版本的同一类型。

和联合一样,默认初始化变量保存其第一个选项的值,除非该选项不是默认的可构造%28,在这种情况下,默认构造函数获得%27T编译:助手类。std::monostate可用于使此类变体默认为%29。

模板参数

Types-the types that may be stored in this variant. All types must be (possibly cv-qualified) non-array object types.

成员函数

(constructor)constructs the variant object (public member function)
(destructor)destroys the variant, along with its contained value (public member function)
operator=assigns a variant (public member function)

观察员

索引返回变体%28公共成员函数%29所持有的备选方案的基于零的索引。

无价值[医]通过[医]异常检查变量是否处于无效状态%28公共成员函数%29

修饰符

在变体中构造一个值,替换%28公共成员函数%29

与另一个变体%28公共成员函数%29的交换掉期

非会员职能

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)

帮助者类

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)

二次

#include <variant> #include <string> int main() { std::variant<int, float> v, w; v = 12; // v contains int int i = std::get<int>(v w = std::get<int>(v w = std::get<0>(v // same effect as the previous line w = v; // same effect as the previous line // std::get<double>(v // error: no double in [int, float] // std::get<3>(v // error: valid index values are 0 and 1 try { std::get<float>(w // w contains int, not float: will throw } catch (std::bad_variant_access&) {} std::variant<std::string> v("abc" // converting constructors work when unambiguous v = "def"; // converting assignment also works when unambiguous }

二次

另见

in_place in_place_type in_place_index in_place_t in_place_type_t in_place_index_t (C++17)in-place construction tag (class template)
optional (since C++17)a wrapper that may or may not hold an object (class template)
any (since C++17)Objects that hold instances of any CopyConstructible type. (class)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/实用程序/变体