首页 > 解决方案 > 可以在 C++11 中模拟 std::is_invocable 吗?

问题描述

我想使用 std::is_invocable,但是我们使用的是 c++11 标准,而 is_invocable 仅适用于 c++17。

有什么方法可以模拟使用 c++11 的功能吗?

谢谢

标签: c++c++11c++17

解决方案


你可以试试这个实现:) 取自 boost C++ 库。我已经使用带有标准 C++14 的 VS2017 对其进行了测试。

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

推荐阅读