Naturally, the syntax you desire may be achieved too. You just have to provide overloads of sort that accept a pointer to member and member function.
It may be convenient sometimes but it is probably not generic enough for STL style (allows to select member/member function to base sort on but not choose sorting order).
Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//some helpers, since not using boost and don't bother to implement sorting myself
template <class T, class SubT>
class compare_member_t
{
SubT T::*member_pointer;
public:
compare_member_t(SubT T::*mp): member_pointer(mp) {}
bool operator()(const T& a, const T& b)
{
return (a.*member_pointer) < (b.*member_pointer);
}
};
template <class T, class SubT>
compare_member_t<T, SubT> compare_member(SubT T::*member_pointer)
{
return compare_member_t<T, SubT>(member_pointer);
}
template <class T, class SubT>
class compare_member_function_t
{
SubT (T::*member_function)()const;
public:
compare_member_function_t(SubT (T::*mf)()const): member_function(mf) {}
bool operator()(const T& a, const T& b)
{
return (a.*member_function)() < (b.*member_function)();
}
};
template <class T, class SubT>
compare_member_function_t<T, SubT> compare_member_function(SubT (T::*member_function)() const)
{
return compare_member_function_t<T, SubT>(member_function);
}
class Person
{
public:
std::string name;
private:
int age;
public:
Person(const std::string& its_name, int its_age):
name(its_name),
age(its_age)
{}
int get_age() const { return age; }
};
void print(const Person& person)
{
std::cout << person.name << " (" << person.get_age() << ")\n";
}
template <class T>
class MyContainer
{
std::vector<T> buffer;
public:
typedef typename std::vector<T>::iterator iterator;
typedef typename std::vector<T>::const_iterator const_iterator;
void push_back(const T& t) {
buffer.push_back(t);
}
iterator begin() { return buffer.begin(); }
iterator end() { return buffer.end(); }
const_iterator begin() const { return buffer.begin(); }
const_iterator end() const { return buffer.end(); }
//there may be other overloads of sort
template <class U>
void sort(U T::*member)
{
std::sort(buffer.begin(), buffer.end(), compare_member(member));
}
template <class U>
void sort(U (T::*member_function)() const)
{
std::sort(buffer.begin(), buffer.end(), compare_member_function(member_function));
}
};
int main()
{
MyContainer<Person> persons;
persons.push_back(Person("Joe", 42));
persons.push_back(Person("Jack", 55));
persons.push_back(Person("John", 33));
persons.sort(&Person::name);
std::cout << "Sorted by name:\n";
std::for_each(persons.begin(), persons.end(), print);
persons.sort(&Person::get_age);
std::cout << "\nSorted by age:\n";
std::for_each(persons.begin(), persons.end(), print);
}