Skip to content

Instantly share code, notes, and snippets.

@mwisnicki
Created April 6, 2010 22:58
Show Gist options
  • Save mwisnicki/358237 to your computer and use it in GitHub Desktop.
Save mwisnicki/358237 to your computer and use it in GitHub Desktop.
TODO: C++ STL comparator adapter
// external
struct Object;
bool compare(Object& l, Object& r);
std::vector<Object*> objects;
// <TODO>
template<class T, class Comparator>
Comparator<T*> adaptToPtr(Comparator<T&> cmp) {
// adapt comparator of T& to T*
// must work for both functions and functors
}
// </TODO>
// this should work
std::sort(objects.begin(), objects.end(), adaptToPtr(compare));
// and so should this
struct Compare {
bool operator()(Object& l, Object& r);
};
std::sort(objects.begin(), objects.end(), adaptToPtr(Compare));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment