i wanna define a function whose behavior depends on whether its parameter is pod type, i do this in two ways:
first
template <typename T, typename = typename std::enable_if<std::is_pod<T>::value>::type>
void f(const T&) {
//...
}
template <typename T, typename = typename std::enable_if<!std::is_pod<T>::value>::type>>
void f(const T&) {
//...
}
second
template <typename T>
typename std::enable_if<std::is_pod<T>::value>::type f(const T&) {
//...
}
template <typename T>
typename std::enable_if<!std::is_pod<T>::value>::type f(const T&) {
//...
}
the second works well while the first is error. compiler complains redefine f in the first case. I wanna know the difference between them. and why the first is error.
thanks for reading, please help me!.
Default arguments on a template parameter list are not part of a function signature, thus, the two overloads have equal signatures from the compiler's perspective.
Instead, you can make the result of std::enable_if
a template parameter itself:
template <typename T, typename std::enable_if<std::is_pod<T>::value, int>::type = 0>
void f(const T&) {}
template <typename T, typename std::enable_if<!std::is_pod<T>::value, int>::type = 0>
void f(const T&) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With