I am writing a generic C ++ container generic class that can optionally maintain its contents in a well-defined order. He previously used function pointers to arrange its contents into a reasonable type, but I'm trying to change it to use boilerplate argument arguments.
Since it often happens that a user of a class may want to store objects of the same type differently in different containers, the container class accepts an optional template argument, which allows the user to additionally specify his own comparison method:
template <class ItemType, class CompareFunctorType = CompareFunctor<ItemType> > class MyContainer
{
[...]
};
If the class user does not specify the type of the user functor, it uses the following CompareFunctor definition by default:
template <typename ItemType> class CompareFunctor
{
public:
bool IsItemLessThan(const ItemType & a, const ItemType & b) const
{
return (a<b);
}
};
This is great for built-in types, as well as for custom types in which an operator is defined less than an operator. However, I would like it to also work automatically for types where there is no inline or explicitly defined less operator. For these types, the order of the elements in the container is not important.
, , , ... "dummy" , , , ... custom "dummy" CompareFunctor , , .
, ( - ), CompareFunctor ( ), , CompareFunctor , ++ "dummy" FallbackCompareFunctor, ? , , - ?
template <typename ItemType> class FallbackCompareFunctor
{
public:
bool IsItemLessThan(const ItemType & a, const ItemType & b) const
{
return ((&a)<(&b));
}
};