Created
August 31, 2016 08:37
-
-
Save jdh8/260608a71197928321fcc07012e1d2f4 to your computer and use it in GitHub Desktop.
Barton–Nackman trick
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
template<typename T> | |
class Comparable | |
{ | |
friend bool operator!=(const T& x, const T& y) { return !(x == y); } | |
friend bool operator>(const T& x, const T& y) { return y < x; } | |
friend bool operator<=(const T& x, const T& y) { return !(y < x); } | |
friend bool operator>=(const T& x, const T& y) { return !(x < y); } | |
}; | |
template<typename T> | |
class Arithmetic | |
{ | |
friend T operator+(T x, const T& y) { return x += y; } | |
friend T operator-(T x, const T& y) { return x -= y; } | |
friend T operator*(T x, const T& y) { return x *= y; } | |
friend T operator/(T x, const T& y) { return x /= y; } | |
friend T operator%(T x, const T& y) { return x %= y; } | |
friend T operator++(T x, int) { return ++x; } | |
friend T operator--(T x, int) { return --x; } | |
}; | |
template<typename T> | |
class Bitset | |
{ | |
friend T operator<<(T x, int shift) { return x <<= shift; } | |
friend T operator>>(T x, int shift) { return x >>= shift; } | |
friend T operator&(T x, const T& y) { return x &= y; } | |
friend T operator|(T x, const T& y) { return x |= y; } | |
friend T operator^(T x, const T& y) { return x ^= y; } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment