可以在C++ 11中模拟std :: is_invocable吗?

jla*_*nik 9 c++ c++11 c++17

我想使用std :: is_invocable,但我们使用的是c ++ 11标准,而is_invocable只能从c ++ 17获得.

有没有办法使用c ++ 11模拟功能?

谢谢

Moh*_*hit 13

你可以试试这个实现:)来自boost C++库.我用VS2017用标准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>
    >
{
};
Run Code Online (Sandbox Code Playgroud)