4

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

7
  • 2
    Er, did you post this question just so you could post someone else's answer..?
    – ildjarn
    Jun 13, 2012 at 22:56
  • 6
    I think it will be more readable in C++ to write something like Range(a, c).contains(b). Jun 13, 2012 at 22:56
  • 1
    @ildjarn Yes, however this was posted in the Lounge<C++> and I asked permission to post it on the main site.
    – Drise
    Jun 13, 2012 at 22:57
  • 1
    @Drise: So the answer to his question is yes, but you had permission anyway :D Jun 13, 2012 at 22:58
  • 6
    Don't do this. It's completely unidiomatic, and so will confuse anyone who's reading your code. The rule of thumb with operator overloads is: always do as the primitives do. Jun 14, 2012 at 0:14

2 Answers 2

7

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

1
  • 12
    Now that you've answered "can it be done?", "should it be done?" is left as an exercise for the reader.
    – msw
    Jun 13, 2012 at 23:01
4

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)))
4
  • 1
    That function won't work with more complex comparisons, like a < b > c > d < e <= f.
    – user1203803
    Jun 13, 2012 at 23:04
  • I wanted to upvote this but then I saw the pre-processor abuse (where most every use of #define is considered harmful in this century)
    – msw
    Jun 13, 2012 at 23:05
  • @msw That's why I put the inline function first :)
    – kol
    Jun 13, 2012 at 23:06
  • 1
    @RadekSlupik: True it doesn't work with expressions like that, but on the other hand, that expression is ambiguous. It doesn't really make sense in the first place. Jun 14, 2012 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.