fix: ranged_representation refactored to work with older compilers

This commit is contained in:
Mateusz Pusz
2022-04-22 13:07:01 +02:00
parent bded48987d
commit c598201d11
2 changed files with 12 additions and 15 deletions

View File

@@ -28,6 +28,7 @@
#include <units/isq/si/length.h>
#include <units/quantity_kind.h>
#include <limits>
#include <numbers>
#include <ostream>
// IWYU pragma: begin_exports
@@ -38,10 +39,10 @@ namespace geographic {
// TODO Change to `angle` dimension in degree unit when the work on magnitudes is done
template<typename T = double>
using latitude = units::dimensionless<units::one, ranged_representation<T, T(-90), T(90)>>;
using latitude = units::dimensionless<units::one, ranged_representation<T, -90, 90>>;
template<typename T = double>
using longitude = units::dimensionless<units::one, ranged_representation<T, T(-180), T(180)>>;
using longitude = units::dimensionless<units::one, ranged_representation<T, -180, 180>>;
template<class CharT, class Traits, typename T>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const latitude<T>& lat)

View File

@@ -26,27 +26,23 @@
#include <algorithm>
#include <type_traits>
template<auto Min, auto Max>
inline constexpr auto is_in_range = [](const auto& v) { return std::clamp(v, Min, Max) == v; };
template<typename T, auto Min, auto Max>
inline constexpr auto is_in_range = [](const auto& v) { return std::clamp(v, T{Min}, T{Max}) == v; };
template<typename T, T Min, T Max>
class ranged_representation : public validated_type<T, decltype(is_in_range<Min, Max>)> {
template<typename T, auto Min, auto Max>
class ranged_representation : public validated_type<T, decltype(is_in_range<T, Min, Max>)> {
public:
using base = validated_type<T, decltype(is_in_range<Min, Max>)>;
using base = validated_type<T, decltype(is_in_range<T, Min, Max>)>;
using base::validated_type;
constexpr ranged_representation() : base(T{}) {}
[[nodiscard]] constexpr ranged_representation operator-() const { return ranged_representation(-this->value()); }
};
template<typename T, T Min, T Max>
template<typename T, auto Min, auto Max>
struct std::common_type<std::intmax_t, ranged_representation<T, Min, Max>> :
std::type_identity<
ranged_representation<std::common_type_t<std::intmax_t, T>, std::common_type_t<std::intmax_t, T>{Min},
std::common_type_t<std::intmax_t, T>{Max}>> {};
std::type_identity<ranged_representation<std::common_type_t<std::intmax_t, T>, Min, Max>> {};
template<typename T, T Min, T Max>
template<typename T, auto Min, auto Max>
struct std::common_type<ranged_representation<T, Min, Max>, std::intmax_t> :
std::type_identity<
ranged_representation<std::common_type_t<T, std::intmax_t>, std::common_type_t<T, std::intmax_t>{Min},
std::common_type_t<T, std::intmax_t>{Max}>> {};
std::type_identity<ranged_representation<std::common_type_t<T, std::intmax_t>, Min, Max>> {};