Search code examples
c++c++11c++17type-traits

Can std::is_invocable be emulated within C++11?


I'd like to use std::is_invocable, but it is available only since C++17, and we are using C++11 Standard.

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


Solution

  • 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>
        >
    {
    };