以下代码之间有什么区别,为什么一个有效而另一个无效?

what's the difference between following code, why one works and the other doesn't?

我想定义一个函数,其行为取决于其参数是否为 pod 类型,我通过两种方式实现:

第一

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&) {
    //...
}

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&) {
    //...
}

第二个运行良好,而第一个出错。编译器在第一种情况下抱怨 redefine f 。我想知道它们之间的区别。为什么第一个是错误的。

感谢阅读,请帮助我!

模板参数列表中的默认参数不是函数签名的一部分,因此,从编译器的角度来看,这两个重载具有相同的签名。 相反,您可以使 std::enable_if 的结果本身成为模板参数:

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&) {}