Search code examples
c++11c++14std-functionstdbind

chained std::bind compile error with VS2015


I'm using VS2015 and I'm playing with std::function and std::bind I found a strange errors.

I have a 2 chained bind operation:

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);


}

the error I got is related to line 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 &&...)'

Could you please help? What I miss related to std::bind?

Merci in advance.


Solution

  • If you add code, stolen from here: 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));
    }
    

    and modify your line to:

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

    the code works ( for me ).