Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template specialization for float

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;
like image 307
Viatorus Avatar asked Sep 22 '26 01:09

Viatorus


1 Answers

Your implementation I think it screams for use of std::ratio. You could use two raw std::ratio for your min and max or some implementation like below:

template<typename U, std::intmax_t NumerMin = 0, std::intmax_t DenomMin = 1,
                     std::intmax_t NumerMax = 0, std::intmax_t DenomMax = 1>
struct Limit {
  static const std::ratio<NumerMin, DenomMin> min_;
  static const std::ratio<NumerMax, DenomMax> max_;
public:
  U const min = static_cast<U>(min_.num) / static_cast<U>(min_.den);
  U const max = static_cast<U>(max_.num) / static_cast<U>(max_.den);
};

Live Demo

like image 85
101010 Avatar answered Sep 24 '26 17:09

101010



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!