How would one implement a ternary comparison operator to determine, for example, the boolean value of a < b < c
?
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
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With