diff --git a/libs/math/include/psemek/math/interval.hpp b/libs/math/include/psemek/math/interval.hpp index 80542481..adb8934d 100644 --- a/libs/math/include/psemek/math/interval.hpp +++ b/libs/math/include/psemek/math/interval.hpp @@ -9,36 +9,68 @@ namespace psemek::math { + namespace detail + { + + template + concept has_min = requires () + { + { T::min() } -> std::same_as; + }; + + template + concept has_max = requires () + { + { T::max() } -> std::same_as; + }; + + } + // Can be specialized in client code template struct limits { static constexpr T min() { - if constexpr (std::is_floating_point_v) + if constexpr (detail::has_min) + { + return T::min(); + } + else if constexpr (std::is_floating_point_v) { return -std::numeric_limits::infinity(); } - else + else if constexpr (std::is_integral_v) { return std::numeric_limits::min(); } + else + { + static_assert("unknown type"); + } } static constexpr T max() { - if constexpr (std::is_floating_point_v) + if constexpr (detail::has_max) + { + return T::max(); + } + else if constexpr (std::is_floating_point_v) { return std::numeric_limits::infinity(); } - else + else if constexpr (std::is_integral_v) { return std::numeric_limits::max(); } + else + { + static_assert("unknown type"); + } } }; - template struct interval_iterator {