std::visit
定义于头文件 <variant> |
||
template< class Visitor, class... Variants > constexpr /* see below */ visit( Visitor&& v, Variants&&... values ); |
(1) | (since C++17) (始于 C++17) |
template< class R, class Visitor, class... Variants > constexpr R visit( Visitor&& v, Variants&&... values ); |
(2) | (since C++20) (始于 C++20) |
辅助模板 |
||
template< class... Ts > auto&& as-variant( std::variant<Ts...>& value ); |
(3) | (仅为说明目的而提供的定义*) |
template< class... Ts > auto&& as-variant( const std::variant<Ts...>& value ); |
(4) | (仅为说明目的而提供的定义*) |
template< class... Ts > auto&& as-variant( std::variant<Ts...>&& value ); |
(5) | (仅为说明目的而提供的定义*) |
template< class... Ts > auto&& as-variant( const std::variant<Ts...>&& value ); |
(6) | (仅为说明目的而提供的定义*) |
将访问器 v (一个 可调用对象 (Callable),可以接受来自 Variants 的类型组合调用)应用于 Variants values。
给定 VariantBases
为 decltype(as-variant
(std::forward<Variants>(values))... (sizeof...(Variants) 类型的包)
INVOKE(std::forward<Visitor>(v),
std::get<indices>(std::forward<VariantBases>(values))...),
as-variant
(values).index()...。INVOKE<R>(std::forward<Visitor>(v),
std::get<indices>(std::forward<VariantBases>(values))...),
as-variant
(values).index()...。这些重载仅在 VariantBases
中的每个类型都是有效类型时才参与重载解析。如果由 INVOKE 或 INVOKE<R>(始于 C++20) 表示的表达式无效,或者 INVOKE 或 INVOKE<R>(始于 C++20) 的结果对于不同的 indices 具有不同的类型或值类别,则程序是非良构的。
as-variant
函数模板接受一个值,其类型可以为 std::variant<Ts...> (即,std::variant<Ts...> 或从 std::variant<Ts...> 派生的类型),并返回具有相同 const 限定符和值类别的 std::variant 值。内容 |
[编辑] 参数
v | - | 一个 可调用对象 (Callable),接受来自 Variants 中每个变体的每个可能的备选项 |
values | - | 要传递给访问器的变体列表 |
[编辑] 返回值
[编辑] 异常
如果 as-variant
(value_i).valueless_by_exception() 对于 values 中的任何变体 value_i 为 true,则抛出 std::bad_variant_access。
[编辑] 复杂度
当变体的数量为零或一时,可调用对象的调用以恒定时间实现;即,它不依赖于可以存储在变体中的类型数量。
如果变体的数量大于一,则可调用对象的调用没有复杂度要求。
[编辑] 注解
设 n 为 (1 * ... * std::variant_size_v<std::remove_reference_t<VariantBases>>),实现通常为 std::visit
的每个特化生成一个等效于 n 个函数指针的(可能是多维的)数组的表,这类似于 虚函数 的实现。
实现也可能为 std::visit
生成一个具有 n 个分支的 switch 语句(例如,MSVC STL 实现在使用 switch 语句时,n 不大于 256)。
在典型的实现中,v 调用的时间复杂度可以认为等于访问(可能是多维)数组中的元素或执行 switch 语句的时间复杂度。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_variant |
202102L |
(C++23) (DR17) |
为从 std::variant 派生的类提供 std::visit |
[编辑] 示例
#include <iomanip> #include <iostream> #include <string> #include <type_traits> #include <variant> #include <vector> // the variant to visit using value_t = std::variant<int, long, double, std::string>; // helper type for the visitor #4 template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; // explicit deduction guide (not needed as of C++20) template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; int main() { std::vector<value_t> vec = {10, 15l, 1.5, "hello"}; for (auto& v: vec) { // 1. void visitor, only called for side-effects (here, for I/O) std::visit([](auto&& arg){ std::cout << arg; }, v); // 2. value-returning visitor, demonstrates the idiom of returning another variant value_t w = std::visit([](auto&& arg) -> value_t { return arg + arg; }, v); // 3. type-matching visitor: a lambda that handles each type differently std::cout << ". After doubling, variant holds "; std::visit([](auto&& arg) { using T = std::decay_t<decltype(arg)>; if constexpr (std::is_same_v<T, int>) std::cout << "int with value " << arg << '\n'; else if constexpr (std::is_same_v<T, long>) std::cout << "long with value " << arg << '\n'; else if constexpr (std::is_same_v<T, double>) std::cout << "double with value " << arg << '\n'; else if constexpr (std::is_same_v<T, std::string>) std::cout << "std::string with value " << std::quoted(arg) << '\n'; else static_assert(false, "non-exhaustive visitor!"); }, w); } for (auto& v: vec) { // 4. another type-matching visitor: a class with 3 overloaded operator()'s // Note: The `(auto arg)` template operator() will bind to `int` and `long` // in this case, but in its absence the `(double arg)` operator() // *will also* bind to `int` and `long` because both are implicitly // convertible to double. When using this form, care has to be taken // that implicit conversions are handled correctly. std::visit(overloaded{ [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; } }, v); } }
输出
10. After doubling, variant holds int with value 20 15. After doubling, variant holds long with value 30 1.5. After doubling, variant holds double with value 3 hello. After doubling, variant holds std::string with value "hellohello" 10 15 1.500000 "hello"
[编辑] 缺陷报告
以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2970 | C++17 | 重载 (1) 的返回类型未保留INVOKE 操作结果的值类别 |
保留 |
LWG 3052 (P2162R2) |
C++17 | 如果 Variants 中的任何类型不是 std::variant,则效果未指定在 Variants 中有任何类型不是 std::variant 的情况下效果未指定 |
已指定 |
[编辑] 参见
(C++26) |
使用 variant 持有的参数调用提供的仿函数(公共成员函数) |
与另一个 variant 交换(公共成员函数) |