A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove

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>;

// Define the 'less-than' operator.
bool operator <(const Test& lhs, const Test& rhs)
{
  return (lhs.a + lhs.b) < (rhs.a + rhs.b);
}

// Define the rest in terms of Compare.
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;
};

// Define the 'less-than' operator.
bool operator <(const Test& lhs, const Test& rhs)
{
  return (lhs.a + lhs.b) < (rhs.a + rhs.b);
}

// Define the rest.
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);
}

compare.h