vendredi 21 août 2020

how to generate a compare function from a pointer to member function?

Writing compare functions for STL algorithms & containers is tedious.

For simple objects such as this,

struct Person
{
    int id;
    string name;
    string surname;
    string get_full_name(); // name then surname
};

I have to type the same function call for both lhs and rhs:

vector<Person> v = { ... };
std::sort( v.begin(), v.end(), [](const Object &lhs, const Object &rhs) {
    return lhs.get_full_name() < rhs.get_full_name();   // tedious
} );

I would like to be able to compose a compare function/functor using a pointer-to-member-function or pointer-to-member-data. Maybe like so:

std::sort( v.begin(), v.end(), make_compare_functor( &Object::get_full_name ));
std::sort( v.begin(), v.end(), make_compare_functor( &Object::surname ));

I've tried to implement it like so:

template <class R, class T>
struct CompareFunctor {
    R (T::*fp)();
    bool operator() (const T& lhs, const T& rhs) { 
           return lhs->*fp() < rhs->*fp();
    }
};

// helper function
template <class R, class T>
CompareFunctor<R,T> make_compare_functor(R(T::*fp)()) {
    return CompareFunctor<R,T> { fp };
}

but it fails to instantiate T for any of these cases

auto functor1 = make_compare_functor( &Person::get_full_name );
auto functor2 = make_compare_functor( &std::string::size );

(the error message is like: 'make_compare_functor': no matching overloaded function found ... note: could be 'const Person' or 'Person')

can anybody guide me how to to compose a compare function/functor using a pointer-to-member-function please?

It should preferably be STL based -- no Boost or another library.

Aucun commentaire:

Enregistrer un commentaire