Skip to content

Instantly share code, notes, and snippets.

@justinvh
Last active December 2, 2016 18:25
Show Gist options
  • Save justinvh/2d6d77846ec2f314f663452e6bcc2c66 to your computer and use it in GitHub Desktop.
Save justinvh/2d6d77846ec2f314f663452e6bcc2c66 to your computer and use it in GitHub Desktop.
template <typename T>
struct extract_return_type: std::false_type {};
template<typename A, typename B>
struct extract_return_type<A(B::*)> { typedef A return_type; typedef B class_type; };
template <class Accessor>
struct AccessorComparator {
Accessor a1, a2;
typedef typename extract_return_type<Accessor>::class_type Object;
AccessorComparator(Accessor a1_, Accessor a2_) : a1(a1_), a2(a2_) { }
bool operator(const Object& lhs, const Object& rhs)
{
return (&lhs)->*a1 < (&rhs)->*a2;
}
};
template <class A>
AccessorComparator<A> make_comparator(A a1, A a2) { return AccessorComparator<A>(a1, a2); }
/**/
struct X { int start, end; }
vector<X> xs;
auto comparator = make_comparator(&X::start, &X::end);
std::sort(xs.begin(), xs.end(), comparator);
@justinvh
Copy link
Author

justinvh commented Dec 2, 2016

These are just notes. Bugs in here, but the idea holds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment