I want to avoid code duplication as much as possible. Suppose I have a class such as,
class T {
int val;
bool operator < (const T& right) const { return val < right.val; }
}
I want to be able to call std::sort() like this,
std::sort( SomeContainer.begin(), SomeContainer.end(), FuncAdaptedFromOp );
This is my very first question in StackOverflow. Please be forgiving.
EDIT
The problem is that the class may have multiple bool T::Compare (const T& right) functions. I would still like an adapter. Take this example,
class Edge {
Vertex u, v;
bool CompareSrc (const Edge& right) const { return u < right.u; }
bool CompareDest (const Edge& right) const { return v < right.v; }
}
Sometimes I want to sort by source Vertex and sometimes by destination Vertex. I just want to know if this is possible or not.
std::bindis C++11 though, so you may want to useboost::bind(in which case you shouldn’t use the preceding using directive) or thebindfrom TR1 if your implementation has that. Otherwise I’d advise you to handroll your own functor.