Home > Article > Backend Development > Why Does Using `std::enable_if_t` With Default Values Result in a Redefinition Error?
Redefinition Issue with enable_if_t Template Arguments
In the provided code, the attempt to convert the std::enable_if type constraint using the new syntax (typename = std::enable_if_t...) results in a redefinition error. This is because the two template functions:
template<typename T, typename = std::enable_if_t<std::is_same<int, T>::value>>> void g() { }
and
template<typename T, typename = std::enable_if_t<std::is_same<double, T>::value>>> void g() { }
are both of type template
To address this issue, it is necessary to remove the default values from the enable_if_t constraint. This results in the following code:
template<typename T, std::enable_if_t<std::is_same<int, T>::value, int>*> void g() { } template<typename T, std::enable_if_t<std::is_same<double, T>::value, int>*> void g() { }
In this case, the second type argument is now a pointer, whose type is dependent on the first. The compiler can substitute in the type T to determine if there is a conflict between the two templates, and it will resolve that there is none.
The above is the detailed content of Why Does Using `std::enable_if_t` With Default Values Result in a Redefinition Error?. For more information, please follow other related articles on the PHP Chinese website!