5.0 KiB
[variant.visit]
22 General utilities library [utilities]
22.6 Variants [variant]
22.6.7 Visitation [variant.visit]
template<class Visitor, class... Variants> constexpr see below visit(Visitor&& vis, Variants&&... vars); template<class R, class Visitor, class... Variants> constexpr R visit(Visitor&& vis, Variants&&... vars);
Let as-variant denote the following exposition-only function templates:template<class... Ts>constexpr auto&& as-variant(variant<Ts...>& var) { return var; }template<class... Ts>constexpr auto&& as-variant(const variant<Ts...>& var) { return var; }template<class... Ts>constexpr auto&& as-variant(variant<Ts...>&& var) { return std::move(var); }template<class... Ts>constexpr auto&& as-variant(const variant<Ts...>&& var) { return std::move(var); }
Let n be sizeof...(Variants).
For each 0â¤i<n, letVi denote the type
decltype(as-variant(std::forward(varsi))).
Constraints: Vi is a valid type for all 0â¤i<n.
Let V denote the pack of types Vi.
Let m be a pack of n values of type size_t.
Such a pack is valid if
0â¤mi<variant_size_v<remove_reference_t> for all 0â¤i<n.
For each valid pack m, let e(m) denote the expression:INVOKE(std::forward(vis), GET(std::forward(vars))...) // see [func.require] for the first form andINVOKE(std::forward(vis), GET(std::forward(vars))...) // see [func.require] for the second form.
Mandates: For each valid pack m, e(m) is a valid expression.
All such expressions are of the same type and value category.
Returns: e(m), where m is the pack for whichmi is as-variant(varsi).index() for all 0â¤i<n.
The return type is decltype(e(m)) for the first form.
Throws: bad_variant_access if(as-variant(vars).valueless_by_exception() || ...) is true.
Complexity: For n ⤠1, the invocation of the callable object is implemented in constant time, i.e., for n=1, it does not depend on the number of alternative types of V0.
For n>1, the invocation of the callable object has no complexity requirements.
template<class Self, class Visitor> constexpr decltype(auto) visit(this Self&& self, Visitor&& vis);
Let V beOVERRIDE_REF(Self&&, COPY_CONST(remove_reference_t, variant)) ([forward]).
Constraints: The call to visit does not use an explicit template-argument-list that begins with a type template-argument.
Effects: Equivalent to: return std::visit(std::forward(vis), (V)self);
template<class R, class Self, class Visitor> constexpr R visit(this Self&& self, Visitor&& vis);
Let V beOVERRIDE_REF(Self&&, COPY_CONST(remove_reference_t, variant)) ([forward]).
Effects: Equivalent to: return std::visit(std::forward(vis), (V)self);