Given a class declaration
class A {
template <typename T> T foo();
};
I would like to specialize A::foo for various types (int, …) and type classes (POD, non-POD) of T. Unfortunately, I cannot seem to use std::enable_if for the latter. The following doesn’t compile:
template <> int A::foo<int>(); // OK
template <typename T>
typename std::enable_if<is_pod<T>::value, T>::type foo(); // <<<< NOT OK!
template <typename T>
typename std::enable_if<!is_pod<T>::value, T>::type foo(); // <<<< NOT OK!
The issue is probably due to the std::enable_if<...> stuff being part of the function signature, and that I did not declare any such member inside A. So how can I specialize a template member based on type traits?
I see no reason to specialize here, overloading the function seems to suffice in my mind.
When checking for POD or no POD, you only have these two choices, so a more generic function is not needed (and not allowed, because it would be ambiguous). You need more than that? You can check for explicit types without specialization with the help of
std::enable_if<std::is_same<int, T>::value, T>::type.