Skip to content

Instantly share code, notes, and snippets.

@CITIZENDOT
Last active August 28, 2021 10:38
Show Gist options
  • Save CITIZENDOT/8a44af09fa63de675a15af9b152366a0 to your computer and use it in GitHub Desktop.
Save CITIZENDOT/8a44af09fa63de675a15af9b152366a0 to your computer and use it in GitHub Desktop.
Continuous Comparison for C++
#include <iostream>
template <typename T>
class Comparator {
bool result;
T last;
public:
Comparator(bool _result, T _last) : result(_result), last(_last) {}
operator bool() const {
return result;
}
Comparator operator<(const T &rhs) const {
return Comparator(result && (last < rhs), rhs);
}
Comparator operator>(const T &rhs) const {
return Comparator(result && (last > rhs), rhs);
}
};
class Int {
int val;
public:
Int(int _val) : val(_val) {}
operator int() const {
return val;
}
Comparator<Int> operator<(const Int &rhs) {
return Comparator<Int>(val < int(rhs), int(rhs));
}
Comparator<Int> operator>(const Int &rhs) {
return Comparator<Int>(val > int(rhs), int(rhs));
}
};
int main() {
Int a(2), b(3), c(1), d(4), e(6), f(5);
std::cout << (a < b > c < d < e) << '\n';
// 2 < 3 > 1 < 4 < 6 > 5
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment