C++ テンプレート引数で与えられる正の整数2つの比較
C++ で、正の整数2つをテンプレート引数で渡して is_greater_eq<lhs, rhs>::value
みたいなことがしたくて、
数週間悩んだ挙げ句、下記のようなインチキなコードを思いついたのですが、
もっとマシな方法ってあったりするんだろうか…。
#include <iostream> template <int lhs, int rhs> struct is_greater_eq : public is_greater_eq<lhs - 1, rhs - 1> { }; template <int rhs> struct is_greater_eq<0, rhs> { constexpr static bool value = false; }; template <int lhs> struct is_greater_eq<lhs, 0> { constexpr static bool value = true; }; template <> struct is_greater_eq<0, 0> { constexpr static bool value = true; }; int main() { std::cout << is_greater_eq<5, 4>::value << std::endl; std::cout << is_greater_eq<5, 5>::value << std::endl; std::cout << is_greater_eq<4, 5>::value << std::endl; }