Files
cppdraft_translate/cppdraft/variant/visit.md
2025-10-25 03:02:53 +03:00

5.0 KiB
Raw Blame History

[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);

1

#

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))).

2

#

Constraints: Vi is a valid type for all 0≤i<n.

3

#

Let V denote the pack of types Vi.

4

#

Let m be a pack of n values of type size_t.

Such a pack is valid if
‰¤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.

5

#

Mandates: For each valid pack m, e(m) is a valid expression.

All such expressions are of the same type and value category.

6

#

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.

7

#

Throws: bad_variant_access if(as-variant(vars).valueless_by_exception() || ...) is true.

8

#

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);

9

#

Let V beOVERRIDE_REF(Self&&, COPY_CONST(remove_reference_t, variant)) ([forward]).

10

#

Constraints: The call to visit does not use an explicit template-argument-list that begins with a type template-argument.

11

#

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);

12

#

Let V beOVERRIDE_REF(Self&&, COPY_CONST(remove_reference_t, variant)) ([forward]).

13

#

Effects: Equivalent to: return std::visit(std::forward(vis), (V)self);