I am confused about the second parameter of std::enable_if. In using of a return type of int, we can make it using:
template
typename std::en
It means that in case of
template::value>::type >
it becomes
template
if the condition std::is_integral
is true
, hence the function is allowed for the type T
and therefore participates in overload resolution.
If the condition is not met, it becomes illegal and the typename std::enable_if<...>::type
invalidates the function for the type T
. In your example, the first method allows all integral types (int
, unsigned
, long
, ...) but no classes, etc.
The second, int
-only version in your example would loose some information and convert values from unsigned to signed or narrow some values, which is why the first version can be really helpful in some cases.
Note that void
is actually the default for the second parameter of std::enable_if
, which is often sufficient to enable or disable templates, etc. as you don't really need a specific type. All you need to know/detect is, whether or not it is valid (void
) or invalid, in which case there is no valid substitution for the ::type
part.