mercredi 25 février 2015

Accept visitor as universal reference (rvalue reference)?

Is it better to accept a visitor (visitor pattern) as universal reference such that the following is possible:



class Edge {
public:
template<typename Visitor>
void visit(Visitor && v){ // Universal reference (binds to temporaries and lvalues)
v(this);
}
int a = 1;
}

Edge e;
e.visit( [](Edge * p){ p->a++; } );


or is the following signature better void visit(Visitor v) (call by value, because the lambda is anyway movable?) or only by reference void visit(Visitor & v) which does not allow temporary visitor objects.


Whats the best consistent way to go?


Aucun commentaire:

Enregistrer un commentaire