I\'m asking for a template trick to detect if a class has a specific member function of a given signature.
The problem is similar to the one cited here http://www.go
Here is a simpler take on Mike Kinghan's answer. This will detect inherited methods. It will also check for the exact signature (unlike jrok's approach which allows argument conversions).
template
class HasGreetMethod
{
template
static std::true_type testSignature(void (T::*)(const char*) const);
template
static decltype(testSignature(&T::greet)) test(std::nullptr_t);
template
static std::false_type test(...);
public:
using type = decltype(test(nullptr));
static const bool value = type::value;
};
struct A { void greet(const char* name) const; };
struct Derived : A { };
static_assert(HasGreetMethod::value, "");
Runnable example