According to the definition of enable_if struct :
template<bool B, class T = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };
I am wondering how
template<class T>
T foo(T t, typename std::enable_if<std::is_integral<T>::value >::type* = 0)
{
return t;
}
particularly :
typename std::enable_if<std::is_integral<T>::value >::type
could be invoked without specified type T, in case std::is_integral<T>::value equal true. In this case the specialization of std::enable_if will be called and in this definition there is no default template parameter.
Is it due to deduce template parameter mechanism ? If yes why specified a default paramater for not specialization definition ?
Your question is a bit cryptic, but as I understand it:
It won’t; The
fooandenable_iftemplates will only be instantiated when the user needs their instantiations (implicitly or explicitly) with specific values forT. SoTis always specified.Default template parameters are only featured on “base” template definitions (ie. not specializations). That is why you only see it on the first declaration of
enable_if. However, they affect all specializations (basically, if you instantiateenable_if<X>, the compiler sees you have not provided one of the arguments for the base template and tries to match all specializations with parameter list<X, void>).BTW the second template argument is used to get a different type out of
enable_ifthanvoid:would be just
int f().