is_callable emulation in C++14: update on old question

65 Views Asked by At

Looking a trait to tell if a class can be used as a callable, whatever the call parameters, I found this interesting post: Find out whether a C++ object is callable.

I found this trait more powerful than std::is_invocable, in some use-cases, because it does not need assumptions on the function call operator parameters.

Yet the accepted answer, inspired from Member detection idiom requires that the tested type can be inherited.

Is it possible to extend the solution to non-inheritable types?

EDIT

My try:

template <typename T, typename U = void>
struct is_callable : std::false_type {};
template <typename T>
struct is_callable<
    T,
    std::enable_if_t<
        std::is_member_function_pointer<decltype(&T::operator())>::value, void>>
    : std::true_type {};

But if this classic SFINAE solution seems OK with a single operator() (Live OK) it cannot work of there is several overload of the operator (Live KO) (whereas the answer of the linked post can): &T::operator() is ambiguous in the presence of overloads.

0

There are 0 best solutions below