17

I'd like to use std::is_invocable, however we are using c++11 standard, while is_invocable is available only from c++17.

Is there any way to emulate the functionality using c++11?

Thank you

jlanik
  • 859
  • 5
  • 12
  • 1
    There seems to be an equivalent in the [gcc implementation](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/type_traits#L2672). – Ron Jul 05 '18 at 09:44
  • 1
    Did you try the `std::__is_invocable`? – Ron Jul 05 '18 at 09:49
  • 2
    did you check Boost library, https://www.boost.org/doc/libs/master/libs/hof/doc/html/include/boost/hof/is_invocable.html – Cheers and hth. - Alf Jul 05 '18 at 09:50
  • @Ron -- `std:__is_invocable` is not part of C++11, nor of any other version of the C++ standard. It looks like an interanal detail for a particular library implementation. – Pete Becker Jul 05 '18 at 11:49
  • @PeteBecker I see. The comment [few lines back](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/type_traits#L2661) suggests it is some sort of equivalent for the C++11. Or that was my understanding. – Ron Jul 05 '18 at 12:20

1 Answers1

24

You can try this implementation:) Taken from boost C++ libraries. I've tested it with VS2017 with standard C++14.

template <typename F, typename... Args>
struct is_invocable :
    std::is_constructible<
        std::function<void(Args ...)>,
        std::reference_wrapper<typename std::remove_reference<F>::type>
    >
{
};

template <typename R, typename F, typename... Args>
struct is_invocable_r :
    std::is_constructible<
        std::function<R(Args ...)>,
        std::reference_wrapper<typename std::remove_reference<F>::type>
    >
{
};
Mohit
  • 1,225
  • 11
  • 28