vendredi 5 janvier 2018

Inline-variant-visitor visitor with lambdas

There is a classic visitor:

struct Visitor
{
    virtual ~Visitor() = default;
    virtual void visit(X& x) {}
    virtual void visit(Y& y) {}
    virtual void visit(Z& z) {}
};

and

struct Object
{
    virtual void accept(Visitor& visitor) { visitor.visit(*this); }
};

X,Y,Z are derived from Object.

I want to make the visitor right on the spot, with lambdas, like this:

auto visitor = make_visitor<Visitor>
(
    [](X& x) {do operations on x},
    //for Y do not want to do anything. default implementation is suitable.
    [](Z& z) {do operations on z}
);

object.accept(visitor);

are there any ideas how to implement make_visitor?

(I read http://ift.tt/2CLGCDh and it's great - I just wanna a shorter and more convenient form of make_visitor)

Aucun commentaire:

Enregistrer un commentaire