Skip to content

[FR]: Add MatchVariant helper for std::visit + Overload #1898

Open
@hocheung-chromium

Description

@hocheung-chromium

Describe the issue

Motivation

When using absl::Overload with std::visit, the syntax requires constructing an explicit Overload object:

std::visit(absl::Overload{ 
    [](int) { ... },
    [](std::string) { ... }
}, my_variant);

Adding a MatchVariant helper would simplify this to:

absl::MatchVariant(my_variant,
    [](int) { ... },
    [](std::string) { ... });

Proposed implementation

template <typename Variant, typename... T>
auto MatchVariant(const Variant& v, T... args) {
    return std::visit(Overload{args...}, v);
}

Steps to reproduce the problem

N/A

What version of Abseil are you using?

Latest version

What operating system and version are you using?

Debian testing

What compiler and version are you using?

clang

What build system are you using?

Ninja

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions