I have been using this utility below to detect if a member method has been defined in a class, which works in most of the cases in my framework.
template <typename C, typename TInput>
class HasHandle
{
template <class T>
static std::true_type testSignature(void(T::*)(const TInput&));
template <class T>
static decltype(testSignature(&T::HandleEvent)) test(std::nullptr_t);
template <class T>
static std::false_type test(...);
public:
using type = decltype(test<C>(nullptr));
static constexpr bool value = type::value;
};
But the problem happens when I try to move some shared EventHandle
into the base class, so I can share the same event handling logic in multiple classes.
After moving some EventHandle
into the base class and uses using EventHandle
to make the base class method visible, I noticed HasHandle
will fail to detect the EventHandle
I defined in the base class.
Here is a quick example:
struct ShareEventHandler
{
void HandleEvent(const int&) {}
};
struct Foo : public ShareEventHandler
{
using ShareEventHandler::HandleEvent;
void HandleEvent(const Foo&) {}
};
static_assert(HasHandle<Foo, Foo>::value, "failed to detect the method");
static_assert(HasHandle<Foo, int>::value, "failed to detect the method"); // this will fail
Does anyone have an idea about how to improve HasHandle
to resolve the failed static_assert above?
Aucun commentaire:
Enregistrer un commentaire