I have a std::list
and in my class I have myclass::operator<(myclass &other)
defined.
I use the std::list.sort
Several answers propose using a predicate that explicitly takes two pointers; this will work for your current case where you have a container of raw pointers, but it won't work for any other dereferenceable type, like smart pointers or iterators.
Why not go the more general route and match any type?
struct indirect_compare
{
template
bool operator()(const T& lhs, const T& rhs) const
{
return *lhs < *rhs;
}
}
While a const reference is unnecessary for a T*
, it is necessary for smart pointer types that are relatively expensive to copy (e.g. std::shared_ptr
) or impossible to copy (e.g. std::unique_ptr
).
Alternatively, you might consider using something like Boost's indirect_iterator, which moves the indirection into the iterator and can make for much cleaner code.