Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://stackoverflow.com/questions/16752954/
- #include <type_traits>
- #include <limits>
- #include <utility>
- #include <cstdint>
- #include <cstdlib>
- template< typename L, typename R >
- inline constexpr
- typename std::enable_if< (std::is_signed< L >::value && !std::is_signed< R >::value), bool >::type
- less(L const & lhs, R const & rhs)
- {
- static_assert(std::is_integral< L >::value,
- "lhs value must be of integral type");
- static_assert(std::is_integral< R >::value,
- "rhs value must be of integral type");
- using T = typename std::common_type< L, R >::type;
- return (lhs < static_cast< L >(0)) || (static_cast< T const & >(lhs) < static_cast< T const & >(rhs));
- }
- template< typename L, typename R >
- inline constexpr
- typename std::enable_if< (!std::is_signed< L >::value && std::is_signed< R >::value), bool >::type
- less(L const & lhs, R const & rhs)
- {
- static_assert(std::is_integral< L >::value,
- "lhs value must be of integral type");
- static_assert(std::is_integral< R >::value,
- "rhs value must be of integral type");
- using T = typename std::common_type< L, R >::type;
- return !(rhs < static_cast< R >(0)) && (static_cast< T const & >(lhs) < static_cast< T const & >(rhs));
- }
- template< typename L, typename R >
- inline constexpr
- typename std::enable_if< (std::is_signed< L >::value == std::is_signed< R >::value), bool >::type
- less(L const & lhs, R const & rhs)
- {
- static_assert(std::is_integral< L >::value,
- "lhs value must be of integral type");
- static_assert(std::is_integral< R >::value,
- "rhs value must be of integral type");
- return lhs < rhs;
- }
- namespace
- {
- static_assert(less(1, 2), "0");
- static_assert(less(-1, std::numeric_limits< std::uintmax_t >::max()), "1");
- static_assert(less< std::int8_t, std::uintmax_t >(-1, std::numeric_limits< std::uintmax_t >::max()), "2");
- static_assert(less< std::intmax_t, std::uint8_t >(-1, std::numeric_limits< std::uint8_t >::max()), "3");
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wsign-compare"
- static_assert(!(-1 < std::numeric_limits< unsigned long >::max()), "4");
- #pragma GCC diagnostic pop
- static_assert(less(-1, std::numeric_limits< unsigned long >::max()), "5");
- }
- int main()
- {
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement