What is the difference between the following code, why does one work and the other not?

I want to define a function whose behavior depends on whether its pod parameter , I do this in two ways:

the 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, and the first is a mistake. the compiler complains about overriding f in the first case. I want to know the difference between the two. and why the first mistake.

Thanks for reading, please help me!

+4
source share
1 answer

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

Source: https://habr.com/ru/post/1609930/


All Articles