I asked a question about a week ago inquiring how I would be able to simply instantiate a class template only if the type it took had a specific member function. In my answer I got sort of a complicated solution. But then I tried to do it on my own. I just wanted to know if this enough to figure out of a given type T
has a void function named f
taking 0 parameters.
#include <type_traits>
#include <utility>
template <typename T, typename = void>
struct has_f : std::false_type { };
template <typename T>
struct has_f<
T,
decltype(std::declval<T>().f(), void())> : std::true_type { };
template <typename T, typename = typename std::enable_if<has_f<T>::value>::type>
struct A { };
struct B
{
void f();
};
struct C { };
template class A<B>; // compiles
template class A<C>; // error: no type named ‘type’
// in ‘struct std::enable_if<false, void>’
If so, why are the other answers so complicated in this thread?