I know, I cannot use float as non-type parameter in templates. So I would like to use a numerator and a denominator for my limit class.
My limit class gets a type and the min and max value. To use Limit for int and for float I currently use this:
template <typename U, typename T = U, T A1 = 0, T A2 = 0, T A3 = 0, T A4 = 0>
struct Limit {
static constexpr const U min = A1;
static constexpr const U max = A2;
};
template <intmax_t A1, intmax_t A2, intmax_t A3, intmax_t A4>
struct Limit<float, intmax_t, A1, A2, A3, A4> {
static constexpr const float min = A1 / (float) A2;
static constexpr const float max = A3 / (float) A4;
};
Limit<float, intmax_t, 40, 20, 30, 40> a;
Limit<int, 10, 20> b;
Is there any way to optimize this implementation? And maybe enable default values for the float implementation?
Thank you in advance!
Edit: I would like to have an template declaration like this one (not working):
// For int, char, short, uint32_t etc...
template <typename U, U MIN, U MAX>
struct Limit {
static constexpr const U min = MIN;
static constexpr const U max = MAX;
};
// For float/double
template <typename T /*float or double*/, typename MIN, typename MAX>
struct Limit {
static constexpr const T min = MIN.num / (T) MIN.dem; // static cast...
static constexpr const T max = MAX.num / (T) MAX.dem; // static cast...
};
Limit<int, int, 10, 20> a;
Limit<float, std::ratio<4,5>, std::ratio<10,15>> b;