Add a wrapper around std::visit that accepts multiple overloads#10771
Open
jschmidt-icinga wants to merge 3 commits into
Open
Add a wrapper around std::visit that accepts multiple overloads#10771jschmidt-icinga wants to merge 3 commits into
std::visit that accepts multiple overloads#10771jschmidt-icinga wants to merge 3 commits into
Conversation
242e393 to
af96f5a
Compare
Al2Klimov
previously approved these changes
Jul 1, 2026
julianbrost
requested changes
Jul 1, 2026
Member
There was a problem hiding this comment.
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().
Contributor
Author
There was a problem hiding this comment.
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.
af96f5a to
bd016d8
Compare
bd016d8 to
1432786
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a wrapper around
std::visitthat 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 fromclang-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
PerfdataWriterConnectionto use it to dispatch on itsm_Streamvariant.Testing
I've added two test-cases that showcase operation of
Visitboth with a singleauto&typed lambda and multiple for explicit type pattern matching.Strictly speaking, these tests aren't really necessary, since the compiler ensures that:
doublein the variant, overloads forintandfloat)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.