Skip to content

Instantly share code, notes, and snippets.

@jdh8
Created August 31, 2016 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdh8/260608a71197928321fcc07012e1d2f4 to your computer and use it in GitHub Desktop.
Save jdh8/260608a71197928321fcc07012e1d2f4 to your computer and use it in GitHub Desktop.
Barton–Nackman trick
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