Last active
December 2, 2016 18:25
-
-
Save justinvh/2d6d77846ec2f314f663452e6bcc2c66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These are just notes. Bugs in here, but the idea holds.