You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
963 B

#pragma once
template <typename T1, typename T2, typename U, typename V>
bool areBothInRangeInclusive(T1 value1, T2 value2, U bound1, V bound2) {
static_assert(std::is_arithmetic<T1>::value &&
std::is_arithmetic<T2>::value &&
std::is_arithmetic<U>::value &&
std::is_arithmetic<V>::value,
"All parameters must be numeric types");
// C++14 方式计算公共类型
using CommonType = std::common_type_t<
std::common_type_t<T1, T2>,
std::common_type_t<U, V>
>;
// 自动确定min和max
const auto actual_min = std::min<CommonType>(
static_cast<CommonType>(bound1),
static_cast<CommonType>(bound2)
);
const auto actual_max = std::max<CommonType>(
static_cast<CommonType>(bound1),
static_cast<CommonType>(bound2)
);
const auto c_value1 = static_cast<CommonType>(value1);
const auto c_value2 = static_cast<CommonType>(value2);
return (c_value1 >= actual_min && c_value1 <= actual_max) &&
(c_value2 >= actual_min && c_value2 <= actual_max);
}