[FR]: Add MatchVariant helper for std::visit + Overload
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
@jbandela
FYI I'm asking around for feedback on this proposal.
I am not sure it has enough upside to justify the cost of putting it in Abseil.
Match is somewhat of an overloaded term with pattern matching and may imply more than it does. The original use of Overload with std::visit is pretty self-explanatory with the std definition of visit.
Yes, after some time of investigation, I agree with your opinion. I think we can close this issue.
Thất bại là mẹ của thành công