[ox/std] Make ox::min/max use values instead of refs for integrals, add CommonRefWith

This commit is contained in:
Gary Talent 2023-02-07 01:35:50 -06:00
parent 5de5eee215
commit eb55144211
2 changed files with 26 additions and 4 deletions

View File

@ -16,6 +16,10 @@ template<typename T, typename U>
concept CommonPtrWith = ox::is_same_v<typename ox::remove_pointer<const T*>::type,
typename ox::remove_pointer<const U*>::type>;
template<typename T, typename U>
concept CommonRefWith = ox::is_same_v<typename ox::remove_reference_t<const T&>,
typename ox::remove_reference_t<const U&>>;
template<typename T, typename U>
concept same_as = ox::is_same_v<T, T>;

View File

@ -14,19 +14,37 @@ namespace ox {
template<typename T>
[[nodiscard]]
constexpr const T &min(const T &a, const T &b) noexcept {
constexpr T min(T a, T b) noexcept requires(ox::is_integral_v<T>) {
return a < b ? a : b;
}
template<typename T>
[[nodiscard]]
constexpr const T &max(const T &a, const T &b) noexcept {
constexpr const T &min(const T &a, const T &b) noexcept requires(!ox::is_integral_v<T>) {
return a < b ? a : b;
}
template<typename T>
[[nodiscard]]
constexpr T max(T a, T b) noexcept requires(ox::is_integral_v<T>) {
return a > b ? a : b;
}
template<typename T>
[[nodiscard]]
constexpr const T &clamp(const T &v, const T &lo, const T &hi) noexcept {
constexpr const T &max(const T &a, const T &b) noexcept requires(!ox::is_integral_v<T>) {
return a > b ? a : b;
}
template<typename T>
[[nodiscard]]
constexpr T clamp(T v, T lo, T hi) noexcept requires(ox::is_integral_v<T>) {
return min(ox::max(v, lo), hi);
}
template<typename T>
[[nodiscard]]
constexpr const T &clamp(const T &v, const T &lo, const T &hi) noexcept requires(!ox::is_integral_v<T>) {
return min(ox::max(v, lo), hi);
}