I am reading reference about enable_if on cppreference. In the example section. I have a question about the template declaration in the code:
// #4, enabled via a template parameter
template<class T,
typename std::enable_if<
!std::is_trivially_destructible<T>{} &&
(std::is_class<T>{} || std::is_union<T>{}),
int>::type = 0>
void destroy(T* t)
{
std::cout << "destroying non-trivially destructible T\n";
t->~T();
}
If the condition of the std::enable_if() is satisfied. The declaration can be rewrote as:
template<class T, typename std::enable_if_t<true,int> = 0> // #1
Since the possible implementation of enable_if is:
template<class T>
struct enable_if<true, T> { typedef T type; };
The #1 is equivalent to the following statement:
template<typename T, typename int = 0>
which is an ill-formed statement. The compiler spits an error message:
error: two or more data types in declaration of 'parameter'
template<typename T, typename int = 0>
My question is: What is the correctly way to understand the typename std::enable_if_t<true,int> = 0
in the template declaration.
And a similar question I have is the example followed by #4
The #5 :
// #5, enabled via a template parameter
template<class T,
typename = std::enable_if_t<std::is_array<T>::value> >
void destroy(T* t) // note, function signature is unmodified
{
for(std::size_t i = 0; i < std::extent<T>::value; ++i) {
destroy((*t)[i]);
}
}
When the condition of the enable_if() is satisfied, the declaration should be something like:
template<class T, typename = X > // X is a type name
How comes the typename=X
can be compiled.
Thanks,
Rong