std::visit supports multiple input variants. The code, however, should handle all combinations of the types from those variants.
Is there a way to skip not "meaningful" combinations?
for example:
template<class... Ts>
struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
int main() {
std::variant<int, float, char> v1 { 's' };
std::variant<int, float, char> v2 { 10 };
std::visit(overloaded{
[](int a, int b) { },
[](int a, float b) { },
[](int a, char b) { },
[](float a, int b) { },
[](float a, float b) { },
[](float a, char b) { },
[](char a, int b) { },
[](char a, float b) { },
[](char a, char b) { }
}, v1, v2);
return 0;
}
is there a chance to implement only several important combinations, and "leave" the rest? (of course right now the compiler will report nasty error if you forget to implement one combination...)
maybe generic lambdas?
std::visit(overloaded{
[](int a, int b) { },
[](int a, float b) { },
[](int a, char b) { },
[](float a, int b) { },
[](auto a, auto b) { }, // <<
}, v1, v2);
That works, but I wonder if there's some better solution?
Update: Here's the playground with the solutions mentioned in the answers: http://coliru.stacked-crooked.com/a/78d9f2f25789bad2