mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-02 03:44:27 +02:00
refactor(example): constraints added to ranged_representation
This commit is contained in:
@@ -25,21 +25,26 @@
|
|||||||
#include "validated_type.h"
|
#include "validated_type.h"
|
||||||
#include <units/bits/external/hacks.h>
|
#include <units/bits/external/hacks.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <concepts>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
template<typename T, auto Min, auto Max>
|
template<std::movable T, std::convertible_to<T> auto Min, std::convertible_to<T> auto Max>
|
||||||
inline constexpr auto is_in_range = [](const auto& v) { return std::clamp(v, T{Min}, T{Max}) == v; };
|
inline constexpr auto is_in_range = [](const auto& v) { return std::clamp(v, T{Min}, T{Max}) == v; };
|
||||||
|
|
||||||
template<typename T, auto Min, auto Max>
|
template<std::movable T, std::convertible_to<T> auto Min, std::convertible_to<T> auto Max>
|
||||||
using is_in_range_t = decltype(is_in_range<T, Min, Max>);
|
using is_in_range_t = decltype(is_in_range<T, Min, Max>);
|
||||||
|
|
||||||
template<typename T, auto Min, auto Max>
|
template<std::movable T, std::convertible_to<T> auto Min, std::convertible_to<T> auto Max>
|
||||||
class ranged_representation : public validated_type<T, is_in_range_t<T, Min, Max>> {
|
class ranged_representation : public validated_type<T, is_in_range_t<T, Min, Max>> {
|
||||||
public:
|
public:
|
||||||
using validated_type<T, is_in_range_t<T, Min, Max>>::validated_type;
|
using validated_type<T, is_in_range_t<T, Min, Max>>::validated_type;
|
||||||
constexpr ranged_representation() : validated_type<T, is_in_range_t<T, Min, Max>>(T{}) {}
|
constexpr ranged_representation() : validated_type<T, is_in_range_t<T, Min, Max>>(T{}) {}
|
||||||
|
|
||||||
[[nodiscard]] constexpr ranged_representation operator-() const { return ranged_representation(-this->value()); }
|
[[nodiscard]] constexpr ranged_representation operator-() const
|
||||||
|
requires requires(T t) { -t; }
|
||||||
|
{
|
||||||
|
return ranged_representation(-this->value());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, auto Min, auto Max>
|
template<typename T, auto Min, auto Max>
|
||||||
|
Reference in New Issue
Block a user