From 7300679f563611e50b388f9870f98719d3c168a9 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 14 Sep 2025 17:22:10 +0300 Subject: [PATCH] Properly initialize math::interval --- libs/math/include/psemek/math/interval.hpp | 42 +++++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) 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 {