std::visit (3) - Linux Manuals
std::visit: std::visit
NAME
Synopsis
Defined in header <variant>
template <class Visitor, class... Variants> (1) (since C++17)
constexpr /*see below*/ visit(Visitor&& vis, Variants&&... vars);
template <class R, class Visitor, class... Variants> (2) (since C++20)
constexpr R visit(Visitor&&, Variants&&...);
Applies the visitor vis to the variants vars
Effectively returns
std::invoke(std::forward<Visitor>(vis), std::get<is>(std::forward<Variants>(vars))...)
, where is... is vars.index()....
1) The return type is deduced from the returned expression as if by decltype. The call is ill-formed if the invocation above is not a valid expression of the same type and value category, for all combinations of alternative types of all variants.
2) The return type is R. If R is (possibly cv-qualified) void, the result of the invoke expression is discarded.
Parameters
vis - a Callable that accepts every possible alternative from every variant
vars - list of variants to pass to the visitor
Return value
1) The value returned by the selected invocation of the visitor.
2) Nothing if R is (possibly cv-qualified) void; otherwise the value returned by the selected invocation of the visitor, implicitly converted to R.
Exceptions
Throws std::bad_variant_access if any variant in vars is valueless_by_exception.
Complexity
When the number of variants is zero or one, the invocation of the callable object is implemented in constant time, i.e. it does not depend on sizeof...(Types).
If the number of variants is larger than 1, the invocation of the callable object has no complexity requirements.
Example
// Run this code