Last active
August 28, 2021 10:38
-
-
Save CITIZENDOT/8a44af09fa63de675a15af9b152366a0 to your computer and use it in GitHub Desktop.
Continuous Comparison for C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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