Search code examples
c++lambdac++14functortype-traits

How can I Tell if a Type is a Functor?


In I have is_invocable to match function pointers, lambdas, and functors.

But what if I'm trapped on ? Do I have a type trait, or can I write one, which will match all of these?

I've tried is_function but that only works on function pointers.


Solution

  • Yes you can, std::is_invocable is a library function which requires no compiler support. You can just rip the implementation from an STL of your choice.

    For example, you can find LLVM implementation of __invokable (to which std::is_invocable forwards all the logic in LLVM's STL) here: https://android.googlesource.com/platform/ndk/+/5b3a49bdbd08775d0e6f9727221fe98946f6db44/sources/cxx-stl/llvm-libc++/libcxx/include/type_traits

    (I was thinking of extracting it and posting here, but it seems to be too big for a post. On a lighter note, I find difference in spelling - invocable vs invokable - amusing.)