1

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.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • It might be possible to use [existing ways to check the existence of a member function](https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-template-to-check-for-a-functions-existence) to detect whether `operator()` is defined for the given type. – alter_igel Apr 24 '19 at 18:04
  • I'm assuming you don't have `std::invoke` as well, and won't be using pointers to members, so you can have a simple "`is_callable`" trait [like this](https://godbolt.org/z/B2w9i8) (Shorter than those other answers) – Artyer Apr 24 '19 at 18:23

1 Answers1

1

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.)

SergeyA
  • 61,605
  • 5
  • 78
  • 137