Advantage of using the vs template function and type inference method

I read quite a few posts about type variable inference, both with and without auto. I think I have two questions.

Let's take a simple range function as an example. I can make it a template and name it:

template <class T, T min, T max> bool inRange(T value) {
   return min <= value && value <= max;
}

bool bbb = inRange<int, 5, 10>(7);

or i can do:

template <class T> bool inRange(T min, T max, T value) {
    return min <= value && value <= max;
}

bool bbb = inRange(5, 10, 7);

Questions

  • Is there a way (without creating multiple templates 1 for short, int, long, double, etc.) that the type can be inferred so that the template can be called using inRange<min, max>(value)

  • Are there any advantages inRange<min, max>(value)regardinginRange(min, max, value)

+4
source share
1 answer
+1

Source: https://habr.com/ru/post/1598586/


All Articles