Related to, but not the same question as How does std::visit work with std::variant?
Implementing std::visit for a single variant conceptually looks like this (in C++ pseudocode):
template<class F, class... Ts>
void visit(F&& f, variant<Ts...>&& var) {
using caller_type = void(*)(void*);
caller_type dispatch[] = {dispatch_visitor(f, (INDEX_SEQUENCE))...};
dispatch[var.index()](var);
};
Basically, we set up a jump table that calls the correct dispatcher for our visitor.
For multiple variants, it seems to be more complicated, as for this approach you'd need to compute the Cartesian product of all the variants' alternatives and possibly template thousands of functions. Is this how it's done or is there a better way that standard libraries implement it?