Skip to content

Add a wrapper around std::visit that accepts multiple overloads#10771

Open
jschmidt-icinga wants to merge 3 commits into
masterfrom
icinga-visit-for-magic-pattern-matching
Open

Add a wrapper around std::visit that accepts multiple overloads#10771
jschmidt-icinga wants to merge 3 commits into
masterfrom
icinga-visit-for-magic-pattern-matching

Conversation

@jschmidt-icinga

@jschmidt-icinga jschmidt-icinga commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

This adds a wrapper around std::visit that accepts only a single variant, but multiple overload alternatives that cover all the types in the variant. As an added benefit, having the function object as the last parameter will lead to nicer formatting from clang-format, in case only one generic overload is provided, with only a single level of indentation.

In addition, to showcase the function, this PR refactors PerfdataWriterConnection to use it to dispatch on its m_Stream variant.

Testing

I've added two test-cases that showcase operation of Visit both with a single auto& typed lambda and multiple for explicit type pattern matching.

Strictly speaking, these tests aren't really necessary, since the compiler ensures that:

  • there is an overload in the set for each variant member type
  • that there are no ambiguous overloads (for example, double in the variant, overloads for int and float)
  • the return-types match

Since the function is not picked by dark template-meta-programming magic that might force conversions or pick the wrong overload by chance, but with standard overload resolution, that should always be fine. Or at least I can't come up with a situation where this wouldn't lead to a compiler error but something unexpected off the top of my head.

@cla-bot cla-bot Bot added the cla/signed label Mar 26, 2026
@jschmidt-icinga jschmidt-icinga force-pushed the icinga-visit-for-magic-pattern-matching branch from 242e393 to af96f5a Compare June 25, 2026 12:25
@Al2Klimov Al2Klimov added this to the 2.17.0 milestone Jul 1, 2026
Al2Klimov
Al2Klimov previously approved these changes Jul 1, 2026
Comment thread lib/base/visit.hpp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick test case demonstrating that this works as expected would be pretty nice. Doesn't need to be super fancy, something like running [](auto x) { return x.size(); } on two instances of variant<string, vector>, and something similar for the case where multiple overloads are passed to Visit().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added a section about the tests to the PR description. Note: I still think it's worth having these tests; Assumptions should be verified, compiler bugs exists etc.

This allows to dispatch multiple overloads that do different things
based on the underlying type and also nicer formatting with clang-format
in case there is only one overload, because the lambda is the second/last
parameter.
This is just to show how icinga::Visit improves readability and
makes some (not all) if constexpr statements inside std::visit
unnecessary.
@jschmidt-icinga jschmidt-icinga force-pushed the icinga-visit-for-magic-pattern-matching branch from af96f5a to bd016d8 Compare July 1, 2026 12:43
@jschmidt-icinga jschmidt-icinga force-pushed the icinga-visit-for-magic-pattern-matching branch from bd016d8 to 1432786 Compare July 1, 2026 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants