Search code examples
c++c++17variant

Is there a clean way to turn an overload set into a visitor suitable for use with std::visit?


Suppose I have an overload set, like so:

class C
{
  public: 

    static void f(const A &);
    static void f(const B &);
};

I'd like to do something like

std::variant<A, B> v;

// ...

std::visit(C::f, v);

but this doesn't compile. Is there some way of taking an overload set and regarding it as, or converting it to, a Visitor?


Solution

  • Actually, after playing around with this further I've realized that I can do

    std::visit([](const auto & t) { C::f(t); }, v);
    

    so I'll leave this here as a potential solution for anyone else with the same problem.