mrdocs::visitIndexed

Visits a std::variant and calls the combined callable with the active index and the value.

Synopsis

Declared in <mrdocs/ADT/Overload.hpp>

template<
    class Variant,
    class... Ts>
constexpr
decltype(auto)
visitIndexed(
    Variant&& v,
    Ts&&... xs);

Description

Unlike match, visitIndexed passes an additional first parameter to your callable set: the runtime index of the active alternative (as a std::size_t).

This is useful when you need both the value and which alternative was selected, without relying on type‐unique alternatives.

The supplied callables are combined via makeOverload and are expected to accept a signature like (std::size_t index, T value) for the relevant T.

std::variant<int, double, std::string> v = 3.14;
fn::visitIndexed(v,
    [](std::size_t i, int x) { return i + x; },
    [](std::size_t i, double d) { return d + i; },
    [](std::size_t i, const std::string& s) { return s.size() + i; });

Return Value

The result of invoking the selected callable.

Parameters

Name Description

v

The variant to visit.

xs

The callables to be combined with makeOverload and invoked with (index, value).

Created with MrDocs