Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary comparison operator overloading

How would one implement a ternary comparison operator to determine, for example, the boolean value of a < b < c?

like image 495
Drise Avatar asked Dec 21 '22 20:12

Drise


2 Answers

Solution: When coding a comparison, have the return type be a comparison object that can chain additional comparisons, but is implicitly convertible to a bool. This can even (kind of) work with types that weren't coded with this intent, simply by casting them to the comparison type manually.

Implementation:

template<class T>
class comparison {
  const bool result;
  const T& last;
public:
  comparison(const T& l, bool r=true) :result(r), last(l) {}
  operator bool() const {return result;}
  comparison operator<(const T& rhs) const {return comparison(rhs, (result && last<rhs));}
  comparison operator<=(const T& rhs) const {return comparison(rhs, (result && last<=rhs));}
  comparison operator>(const T& rhs) const {return comparison(rhs, (result && last>rhs));}
  comparison operator>=(const T& rhs) const {return comparison(rhs, (result && last>=rhs));}
};

A useful example:

#include <iostream>
int main() {
  //testing of chained comparisons with int
  std::cout << (comparison<int>(0) < 1 < 2) << '\n';
  std::cout << (comparison<int>(0) < 1 > 2) << '\n';
  std::cout << (comparison<int>(0) > 1 < 2) << '\n';
  std::cout << (comparison<int>(0) > 1 > 2) << '\n';
}

Output:

1
0
0
0

Note: This was created by Mooing Duck, and a compiled, more robust example can be found on http://ideone.com/awrmK

like image 186
Drise Avatar answered Dec 23 '22 09:12

Drise


Why do you need an operator?

inline bool RangeCheck(int a, int b, int c)
{
  return a < b && b < c;
}

or:

#define RANGE_CHECK(a, b, c) (((a) < (b)) && ((b) < (c)))
like image 42
kol Avatar answered Dec 23 '22 08:12

kol