Visual Studio 2013 C++ - Passing std::unique_ptr to a bound function

前端 未结 3 1753
走了就别回头了
走了就别回头了 2020-12-21 03:53

Using Visual Studio 2013 RC and C++, I\'m trying to pass an std::unique_ptr to a function that has been bound using std::bind. However, I\'m having

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 04:14

    I had the same problem with VS 2012 recently. I believe this is a bug in MSVC; at least in MSVC++11 the pseudo-variadic expansion seems to forward the parameters by value to some internal function. Seems this hasn't been improved.
    As a workaround, I'm using lambdas instead, but another hack is required to make it work:

    std::function)> bound =
        [] (std::unique_ptr arg) { func(std::move(arg)); };
    

    still doesn't compile. But if you add any captured value (even one that isn't used), it compiles:

    int x;
    std::function)> bound =
        [x] (std::unique_ptr arg) { func(std::move(arg)); };
    

提交回复
热议问题