Home >Backend Development >C++ >Why Am I Getting a Redefinition Error with `enable_if_t` in Template Arguments?
Redefinition Error in enable_if_t in Template Arguments
In C , the new syntax for enable_if using enable_if_t encountered a redefinition error when porting code to the new syntax. The following code demonstrates the issue:
template<typename T, typename std::enable_if<std::is_same<int, T>::value>::type* = nullptr> void f() { } template<typename T, typename = std::enable_if_t<std::is_same<int, T>::value>> void g() { }
Compiling with GCC (5.2) yields the error:
"error: redefinition of 'template
Cause of the Error
The error arises because the two template functions f and g have the same template type void(). Even though their second template arguments have different default values, they still have the same type.
Solution
To resolve the issue, update the syntax to use std::enable_if_t with a different second argument type. For example:
template<typename T, std::enable_if_t<std::is_same<int, T>::value, int>* = nullptr> void f() { } template<typename T, std::enable_if_t<std::is_same<double, T>::value, double>* = nullptr> void g() { }
This modification ensures that the two template functions have distinct template types and eliminates the redefinition error.
The above is the detailed content of Why Am I Getting a Redefinition Error with `enable_if_t` in Template Arguments?. For more information, please follow other related articles on the PHP Chinese website!