std::variant<Types...>::variant
构造一个新的 variant 对象。
声明
// 1)
constexpr variant() noexcept(/* see below */);
// 2)
constexpr variant( const variant& other );
// 3)
constexpr variant( variant&& other ) noexcept(/* see below */);
// 4)
template< class T >
constexpr variant( T&& t ) noexcept(/* see below */);
// 5)
template< class T,
class... Args >
constexpr explicit variant( std::in_place_type_t<T>,
Args&&... args );
// 6)
template< class T,
class U,
class... Args >
constexpr explicit variant( std::in_place_type_t<T>,
std::initializer_list<U> il,
Args&&... args );
// 7)
template< std::size_t I,
class... Args >
constexpr explicit variant( std::in_place_index_t<I>,
Args&&... args );
// 8)
template< std::size_t I,
class U,
class... Args >
constexpr explicit variant( std::in_place_index_t<I>,
std::initializer_list<U> il,
Args&&... args );
默认构造函数。构造一个 variant,其中包含第一个备选项(index()
为零)的值初始化值。
- 当且仅当备选类型
T_0
的值初始化满足 constexpr 函数的要求时,此构造函数是 constexpr。 - 此重载仅在
std::is_default_constructible_v<T_0>
为 true 时参与重载决议。
复制构造函数。如果 other 不是 valueless_by_exception,则构造一个 variant,其包含与 other 相同的备选项,并通过 std::get<other.index()>(other)
直接初始化包含的值。否则,初始化一个 valueless_by_exception variant。
- 除非
Types...
中所有T_i
的std::is_copy_constructible_v<T_i>
为 true,否则此构造函数被定义为删除。 - 如果
Types...
中所有T_i
的std::is_trivially_copy_constructible_v<T_i>
为 true,则它是平凡的。
移动构造函数。如果 other 不是 valueless_by_exception,则构造一个 variant,其包含与 other 相同的备选项,并通过 std::get<other.index()>(std::move(other))
直接初始化包含的值。否则,初始化一个 valueless_by_exception variant。
- 此重载仅在
Types...
中所有T_i
的std::is_move_constructible_v<T_i>
为 true 时参与重载决议。 - 如果
Types...
中所有T_i
的std::is_trivially_move_constructible_v<T_i>
为 true,则它是平凡的。
转换构造函数。构造一个 variant,其包含备选类型 T_j
,如果存在一个虚函数 F(T_i)
的重载(针对 Types...
中每个 T_i
同时在作用域内),则该类型将通过表达式 F(std::forward<T>(t))
的重载决议选择,除了:
- 仅当声明
T_i x[] = { std::forward<T>(t) };
对某个虚构变量x
有效时,才考虑重载F(T_i)
。
直接初始化包含的值,如同通过 std::forward<T>(t)
的直接非列表初始化。
- 此重载仅在以下情况下参与重载解析
sizeof...(Types) > 0
,std::decay_t<T>
(直到 C++20) /std::remove_cvref_t<T>
(自 C++20 起) 既不是与 variant 相同的类型,也不是std::in_place_type_t
的特化,也不是std::in_place_index_t
的特化,std::is_constructible_v<T_j, T>
为 true,- 并且表达式
F(std::forward<T>(t))
(其中F
是上述虚构函数集) 形式良好。
- 如果
T_j
的选定构造函数是 constexpr 构造函数,则此构造函数也是 constexpr 构造函数。
std::variant<std::string> v("abc"); // OK
std::variant<std::string, std::string> w("abc"); // ill-formed
std::variant<std::string, const char*> x("abc"); // OK, chooses const char*
std::variant<std::string, bool> y("abc"); // OK, chooses string; bool is not a candidate
std::variant<float, long, double> z = 0; // OK, holds long
// float and double are not candidates
构造一个 variant,其包含指定的备选类型 T
,并使用参数 std::forward<Args>(args)...
初始化包含的值。
- 如果
T
的选定构造函数是 constexpr 构造函数,则此构造函数也是 constexpr 构造函数。 - 此重载仅在
T
在Types...
中恰好出现一次且std::is_constructible_v<T, Args...>
为 true 时参与重载决议。
构造一个 variant,其包含指定的备选类型 T
,并使用参数 il, std::forward<Args>(args)...
初始化包含的值。
- 如果
T
的选定构造函数是 constexpr 构造函数,则此构造函数也是 constexpr 构造函数。 - 此重载仅在
T
在Types...
中恰好出现一次且std::is_constructible_v<T, initializer_list<U>&, Args...>
为 true 时参与重载决议。
构造一个 variant,其包含由索引 I 指定的备选类型 T_i
,并使用参数 std::forward<Args>(args)...
初始化包含的值。
- 如果
T_i
的选定构造函数是 constexpr 构造函数,则此构造函数也是 constexpr 构造函数。 - 此重载仅在 I < sizeof...(Types) 且
std::is_constructible_v<T_i, Args...>
为 true 时参与重载决议。
构造一个 variant,其包含由索引 I 指定的备选类型 T_i
,并使用参数 il, std::forward<Args>(args)...
初始化包含的值。
- 如果
T_i
的选定构造函数是 constexpr 构造函数,则此构造函数也是 constexpr 构造函数。 - 此重载仅在 I < sizeof...(Types) 且
std::is_constructible_v<T_i, std::initializer_list<U>&, Args...>
为 true 时参与重载决议。
参数
other
- 另一个 variant 对象,其包含的值用于复制/移动 t
- 用于初始化包含的值的值 args...
- 用于初始化包含的值的参数 il
- 用于初始化包含的值的初始化列表
异常
-
可能会抛出第一个备选项的值初始化所抛出的任何异常。noexcept 规范
noexcept(std::is_nothrow_default_constructible_v<T_0>)
-
可能会抛出通过直接初始化
Types...
中任何T_i
所抛出的任何异常 -
可能会抛出通过移动构造
Types...
中任何T_i
所抛出的任何异常。noexcept 规范
noexcept((std::is_nothrow_move_constructible_v<Types> && ...))
-
可能会抛出所选备选项
T_j
的初始化所抛出的任何异常。noexcept 规范
noexcept(std::is_nothrow_constructible_v<T_j, T>)
5-8) 可能会抛出通过调用所选备选项的选定构造函数所抛出的任何异常。
示例
#include <cassert>
#include <iostream>
#include <string>
#include <variant>
#include <vector>
using vector_t = std::vector<int>;
auto& operator<<(auto& out, const vector_t& v)
{
out << "{ ";
for (int e: v)
out << e << ' ';
return out << "}";
}
int main()
{
// value-initializes first alternative
std::variant<int, std::string> var0;
assert(std::holds_alternative<int>(var0) and
var0.index() == 0 and
std::get<int>(var0) == 0);
// initializes first alternative with std::string{"STR"};
std::variant<std::string, int> var1{"STR"};
assert(var1.index() == 0);
std::cout << "1) " << std::get<std::string>(var1) << '\n';
// initializes second alternative with int == 42;
std::variant<std::string, int> var2{42};
assert(std::holds_alternative<int>(var2));
std::cout << "2) " << std::get<int>(var2) << '\n';
// initializes first alternative with std::string{4, 'A'};
std::variant<std::string, vector_t, float> var3
{
std::in_place_type<std::string>, 4, 'A'
};
assert(var3.index() == 0);
std::cout << "3) " << std::get<std::string>(var3) << '\n';
// initializes second alternative with std::vector{1,2,3,4,5};
std::variant<std::string, vector_t, char> var4
{
std::in_place_type<vector_t>, {1,2,3,4,5}
};
assert(var4.index() == 1);
std::cout << "4) " << std::get<vector_t>(var4) << '\n';
// initializes first alternative with std::string{"ABCDE", 3};
std::variant<std::string, vector_t, bool> var5 {std::in_place_index<0>, "ABCDE", 3};
assert(var5.index() == 0);
std::cout << "5) " << std::get<std::string>(var5) << '\n';
// initializes second alternative with std::vector(4, 42);
std::variant<std::string, vector_t, char> var6 {std::in_place_index<1>, 4, 42};
assert(std::holds_alternative<vector_t>(var6));
std::cout << "6) " << std::get<vector_t>(var6) << '\n';
}
1) STR
2) 42
3) AAAA
4) { 1 2 3 4 5 }
5) ABC
6) { 42 42 42 42 }
缺陷报告
以下改变行为的缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 发布时的行为 | 正确行为 |
---|---|---|---|
LWG 2901 | C++17 | 提供了分配器感知构造函数,但 variant 无法正确支持分配器 | 构造函数已移除 |
P0739R0 | C++17 | 转换构造函数模板与类模板参数推导交互不佳 | 添加了约束 |
LWG 3024 | C++17 | 如果任何成员类型不可复制,则复制构造函数不参与重载决议 | 改为定义为已删除 |
P0602R4 | C++17 | 复制/移动构造函数即使底层构造函数是平凡的,也可能不是平凡的 | 要求传播平凡性 |
P0608R3 | C++17 | 转换构造函数盲目地组装重载集,导致意外转换 | 不考虑窄化和布尔转换 |
P1957R2 | C++17 | bool 的转换构造函数不允许隐式转换 | 指针到 bool 的转换是窄化的,并且转换构造函数没有 bool 的异常 |