繁体   English   中英

Visual Studio 2013 C ++ - 将std :: unique_ptr传递给绑定函数

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

使用Visual Studio 2013 RC和C ++,我试图将std::unique_ptr传递给已使用std::bind的函数。 但是,我遇到了麻烦,因为当我尝试这个时,VS似乎并不喜欢它。 这是我正在尝试编译的内容:

#include <memory>
#include <iostream>
#include <functional>

void func(std::unique_ptr<int> arg)
{
    std::cout << *arg << std::endl;
}

int main()
{
    std::function<void (std::unique_ptr<int>)> bound =
        std::bind(&func, std::placeholders::_1);

    std::unique_ptr<int> ptr(new int(42));
    bound(std::move(ptr));

    return 0;
}

这在GCC 4.8.1中编译,但在VS2013 RC中不编译。 我总是遇到VS中移动语义的问题,但我真的很想使用std::unique_ptr而不是std::shared_ptr或原始指针。

我找到的一个解决方法是更改​​函数签名以接受std::unique_ptr& ,它在VS和GCC中编译,但不会使func的意图获得std::unique_ptr所有权特别清楚,并且防止我安全地异步调用函数,除非我做一些特别难看的事情:

#include <memory>
#include <iostream>
#include <functional>
#include <future>
#include <string>

void func(std::unique_ptr<int>& arg)
{
    std::cout << *arg << std::endl;
}

int main()
{
    std::function<void (std::unique_ptr<int>&)> bound =
        std::bind(&func, std::placeholders::_1);

    std::unique_ptr<int> ptr(new int(42));
    std::promise<void> prom;
    std::async(
        [&bound, &ptr, &prom]
        {
            std::unique_ptr<int> movedPtr = std::move(ptr);
            prom.set_value();

            bound(std::move(movedPtr));
        });

    prom.get_future().wait();

    // Wait here
    std::string dummy;
    std::cin >> dummy;
}

有没有办法绕过这个而不改变func的签名?

谢谢!

我最近和VS 2012有同样的问题。 我相信这是MSVC中的一个错误; 至少在MSVC ++ 11中,伪可变参数扩展似乎通过值将参数转发给某些内部函数。 似乎这没有得到改善。
作为一种解决方法,我正在使用lambdas,但需要另一个hack来使其工作:

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

仍然没有编译。 但是如果添加任何捕获的值(即使是未使用的值),它也会编译:

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

您还必须将参数移动到func的绑定调用中。 不仅在bound的调用

bound(std::move(ptr));

而且在绑定中:

std::function<void(std::unique_ptr<int>)> bound =
    std::bind(func,
              std::bind(std::move<std::unique_ptr<int>&>,
                        std::placeholders::_1));

这是为VS2013(更新4)编译的。

std::bind函数不转发参数,它将它们复制到函数中。 因此,从c ++ 11开始, std::bind不适用于仅移动类型。 这个问题是“更完美转发”提案背后的想法( 就像这个 )。 有一个更新的,但我现在似乎无法找到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM