std::variant::variant
| constexpr variant() noexcept(/* see below */); | (1) | (since C++17) | 
|---|---|---|
| variant(const variant& other); | (2) | (since C++17) | 
| variant(variant&& other) noexcept(/* see below */); | (3) | (since C++17) | 
| template< class T > constexpr variant(T&& t) noexcept(/* see below */); | (4) | (since C++17) | 
| template< class T, class... Args > constexpr explicit variant(std::in_place_type_t<T>, Args&&... args); | (5) | (since C++17) | 
| template< class T, class U, class... Args > constexpr explicit variant(std::in_place_type_t<T>, std::initializer_list<U> il, Args&&... args); | (6) | (since C++17) | 
| template< std::size_t I, class... Args > constexpr explicit variant(std::in_place_index_t<I>, Args&&... args); | (7) | (since C++17) | 
| template <size_t I, class U, class... Args> constexpr explicit variant(std::in_place_index_t<I>, std::initializer_list<U> il, Args&&... args); | (8) | (since C++17) | 
| template <class Alloc> variant(std::allocator_arg_t, const Alloc& a); | (9) | (since C++17) | 
| template <class Alloc> variant(std::allocator_arg_t, const Alloc& a, const variant& other); | (10) | (since C++17) | 
| template <class Alloc> variant(std::allocator_arg_t, const Alloc& a, variant&& other); | (11) | (since C++17) | 
| template <class Alloc, class T> variant(std::allocator_arg_t, const Alloc& a, T&& t); | (12) | (since C++17) | 
| template <class Alloc, class T, class... Args> variant(std::allocator_arg_t, const Alloc& a, std::in_place_type_t<T>, Args&&... args); | (13) | (since C++17) | 
| template <class Alloc, class T, class U, class... Args> variant(std::allocator_arg_t, const Alloc& a, std::in_place_type_t<T>, std::initializer_list<U> il, Args&&... args); | (14) | (since C++17) | 
| template <class Alloc, size_t I, class... Args> variant(std::allocator_arg_t, const Alloc& a, std::in_place_index_t<I>, Args&&... args); | (15) | (since C++17) | 
| template <class Alloc, size_t I, class U, class... Args> variant(std::allocator_arg_t, const Alloc& a, std::in_place_index_t<I>, std::initializer_list<U> il, Args&&... args); | (16) | (since C++17) | 
构造一个新的variant对象。
1%29默认构造函数。构造一个包含值初始化第一个备选方案的价值%28index()是零%29。此构造函数是constexpr当且仅当替代类型T的值初始化[医]0将满足常数函数。此重载只参与以下情况下的过载解决方案:std::is_default_constructible_v<T_0>是真的。
2%29复制构造函数。如果other不是无价值[医]通过[医]例外,构造一个包含与other和直接初始化包含的值std::get<other.index()>(other)否则,初始化无价值的[医]通过[医]异常变量。此重载只参与在下列情况下的重载解决方案:std::is_copy_constructible_v<T_i>对所有人来说都是真的T_i在Types......
3%29移动构造函数。如果other不是无价值[医]通过[医]例外,构造一个包含与other和直接初始化包含的值std::get<other.index()>(std::move(other))否则,初始化valueless_by_exception变体。此重载只参与在下列情况下的重载解决方案:std::is_move_constructible_v<T_i>对所有人来说都是真的T_i在Types...
4%29转换构造函数。构造一个包含可选类型的变量。T_j,它将由表达式的重载解析来选择。F(std::forward<T>(t)如果虚函数有过载,F%28t[医]每吨i%29[医]我来自类型...同时在范围内。直接初始化包含的值,好像由直接非列表初始化从std::forward<T>(t)。此重载只参与以下情况下的过载解决方案:std::is_same_v<std::decay_t<T>, variant>是false,,,std::decay_t<T>都不是std::in_place_type_t也不是专门的std::in_place_index_t,,,std::is_constructible_v<T_j, T>是true,以及表达F(std::forward<T>(t))%28与F为上述虚函数集%29是良好的形式。如果T,则此构造函数是一个conexpr构造函数。[医]J%27s选定的构造函数是一个conexpr构造函数。
二次
variant<string> v("abc"); // OK
variant<string, string> w("abc"); // ill-formed, can't select the alternative to convert to
variant<string, bool> x("abc"); // OK, but chooses bool二次
5%29用指定的选项构造一个变体。T并使用参数初始化包含的值。std::forward<Args>(args)...如果T%27s选定的构造函数是一个conexpr构造函数,则此构造函数也是一个conexpr构造函数。只有当T发生在Types...和std::is_constructible_v<T, Args...>是true...
6%29用指定的选项构造一个变体。T并使用参数初始化包含的值。il,std::forward<Args>(args)....如果T%27s选定的构造函数是一个conexpr构造函数,则此构造函数也是一个conexpr构造函数。只有当T发生在Types...和std::is_constructible_v<T, initializer_list<U>&, Args...>是true...
7%29构造一个具有替代T的变体[医]由索引指定的I并使用参数初始化包含的值。std::forward<Args>(args)....如果T[医]I%27s选定的构造函数是conexpr构造函数,此构造函数也是conexpr构造函数。此重载只参与在下列情况下的重载解决方案:I < sizeof...(Types)和std::is_constructible_v<T_i, Args...>是真的。
8%29构造一个具有替代T的变体[医]由索引指定的I并使用参数初始化包含的值。il,std::forward<Args>(args)....如果T[医]I%27s选定的构造函数是conexpr构造函数,此构造函数也是conexpr构造函数。此重载只参与在下列情况下的重载解决方案:I < sizeof...(Types)和std::is_constructible_v<T_i,std::initializer_list<U>&, Args...>是真的。
9-16%29与%281-8%29相同,但包含的值是在用途分配器协议
参数
| other | - | another variant object whose contained value to copy/move | 
|---|---|---|
| t | - | value to initialize the contained value with | 
| args... | - | arguments to initialize the contained value with | 
| il | - | initializer list to initialize the contained value with | 
| a | - | allocator to pass to the contained value | 
类型要求
-如要使用过载%289%29,必须符合分配程序的要求。
例外
1%5月29日,抛出第一个选项的值初始化引发的任何异常。
noexcept规格:
noexcept(std::is_nothrow_default_constructible_v<T_0>)
2%5月29日抛出任何由直接初始化T引发的异常。[医]我在Types...
3%,5月29日,抛出任何由移动-构造任何T抛出的异常[医]我在Types......
noexcept规格:
noexcept((std::is_nothrow_move_constructible_v<Types>&& ...))
4%5月29日抛出通过初始化所选选项引发的任何异常。T_j...
noexcept规格:
noexcept(std::is_nothrow_constructible_v<T_j, T>)
5-8%5月29日通过调用所选选项的选定构造函数抛出任何异常。
例
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

