与VS2015

时间:2017-07-01 13:44:43

标签: c++11 c++14 std-function stdbind

我正在使用VS2015而且我正在玩std::functionstd::bind我发现了一个奇怪的错误。

我有一个2链式绑定操作:

int main()
{


    auto func1 = [](int i) -> int {
        return i + 1;
    };

    auto func2 = [](float f, function<int(int)>&& func) -> float {
        return f + func(f);
    };


    auto func2_instance = std::bind(func2, std::placeholders::_1, func1);

    cout << func2_instance(0.2) << endl;

    auto func3 = [](double d, function<float(float)>&& func)->double {
        return d + func(d);
    };
   //doesn't work
auto func3_instance = std::bind(func3, std::placeholders::_1, std::move(func2_instance));
  //works
auto func3_instance = std::bind(func3, std::placeholders::_1, [funcmv = std::move(func2_instance)](float a)->float{
        return funcmv(a);
    });

    func3_instance(0.2);


}

我得到的错误与行func3_instance(0.2)

有关
D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(1468): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
你可以帮忙吗?我想念的是std::bind

Merci提前。

1 个答案:

答案 0 :(得分:2)

如果你添加代码,从这里偷来: Why is there no std::protect?

template<typename T>
struct protect_wrapper : T
{
    protect_wrapper(const T& t) : T(t) {}
    protect_wrapper(T&& t) : T(std::move(t)) {}
};

template<typename T>
typename std::enable_if< !std::is_bind_expression< typename std::decay<T>::type >::value,
    T&& >::type
protect(T&& t)
{
    return std::forward<T>(t);
}

template<typename T>
typename std::enable_if< std::is_bind_expression< typename std::decay<T>::type >::value,
    protect_wrapper<typename std::decay<T>::type > >::type
protect(T&& t)
{
    return protect_wrapper<typename std::decay<T>::type >(std::forward<T>(t));
}

并将您的行修改为:

auto func3_instance = std::bind(func3, std::placeholders::_1, protect( func2_instance));

代码适用(对我而言)。

相关问题