compare
A helper class that defines <=, >, >= in terms of std::less or a user supplied compare type.
template <typename T, typename TLess = std::less<T> >
struct compare
____________________________________________________________________________________________________
Types
first_argument_type;
second_argument_type;
result_type;
____________________________________________________________________________________________________
Members
static result_type lt(first_argument_type lhs, second_argument_type rhs)
Returns true if lhs < rhs, otherwise false.
static result_type lte(first_argument_type lhs, second_argument_type rhs)
Returns true if lhs <= rhs, otherwise false.
static result_type gt(first_argument_type lhs, second_argument_type rhs)
Returns true if lhs > rhs, otherwise false.
static result_type gte(first_argument_type lhs, second_argument_type rhs)
Returns true if lhs >= rhs, otherwise false.
____________________________________________________________________________________________________
Example 1
Using std::less
struct Test
{
int a;
int b;
};
using Compare = etl::compare<Test>;
bool operator <(const Test& lhs, const Test& rhs)
{
return (lhs.a + lhs.b) < (rhs.a + rhs.b);
}
bool operator <=(const Test& lhs, const Test& rhs)
{
return Compare::lte(lhs, rhs);
}
bool operator >(const Test& lhs, const Test& rhs)
{
return Compare::gt(lhs, rhs);
}
bool operator >=(const Test& lhs, const Test& rhs)
{
return Compare::gte(lhs, rhs);
}
____________________________________________________________________________________________________
Example 2
Separate 'less-than' class.
struct Test
{
int a;
int b;
};
struct LessThan
{
bool operator()(const Test& lhs, const Test& rhs) const
{
return (lhs.a + lhs.b) < (rhs.a + rhs.b);
}
};
using Compare = etl::compare<Test, LessThan>;
bool operator <(const Test& lhs, const Test& rhs)
{
return Compare::lt(lhs, rhs);
}
bool operator <=(const Test& lhs, const Test& rhs)
{
return Compare::lte(lhs, rhs);
}
bool operator >(const Test& lhs, const Test& rhs)
{
return Compare::gt(lhs, rhs);
}
bool operator >=(const Test& lhs, const Test& rhs)
{
return Compare::gte(lhs, rhs);
}
____________________________________________________________________________________________________
Example 3
Inheritance
struct Test : public etl::compare<Test>
{
int a;
int b;
};
bool operator <(const Test& lhs, const Test& rhs)
{
return (lhs.a + lhs.b) < (rhs.a + rhs.b);
}
bool operator <=(const Test& lhs, const Test& rhs)
{
return Test::lte(lhs, rhs);
}
bool operator >(const Test& lhs, const Test& rhs)
{
return Test::gt(lhs, rhs);
}
bool operator >=(const Test& lhs, const Test& rhs)
{
return Test::gte(lhs, rhs);
}