A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd

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

Typedefs

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

typedef etl::compare<Test> Compare;

// Define the 'less-than' operator.
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);
  }
};

typedef etl::compare<Test, LessThan> Compare;

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