I would like to use SFINAE to overload a function template based on whether the template argument declares a type T
. This is where I was able to get so far:
struct C1 {
using T = int;
};
struct C2 {
using T = void; // but I would really like
// to not require T at all
};
// For classes that declare T
template <class C>
void f(C &c, typename std::enable_if<!std::is_same<typename C::T, void>::value,
int>::type = 0) {
(void)c;
std::cout << "With T" << std::endl;
}
// For classes that do not declare T (for now, T must be declared void)
template <class C>
void f(C &c, typename std::enable_if<std::is_same<typename C::T, void>::value,
int>::type = 0) {
(void)c;
std::cout << "Without T" << std::endl;
}
int main() {
C2 c;
f(c);
return 0;
}
How (if at all) is it possible to change this solution in a way that C2
would not need to declare T
at all? I.e. I would like to have two overloads: one for classes that declare T
and one for classes that do not.