繁体   English   中英

带有unique_ptr参数的std :: function

[英]std::function with unique_ptr argument

给定一个功能

void MyFunction(std::unique_ptr<int> arg);

不可能(MSVC 2012)创建像这样的仿函数

std::function<void(std::unique_ptr<int>)> f = std::bind(&MyFunction, std::placeholders::_1);

问题不在于绑定 - 使用auto f = std::bind(...)有效。 此外,使用shared_ptr也有效

  • 为什么不允许unique_ptr?
  • 这是MSVC问题还是一般的C ++ 11限制?
  • 有没有改变功能定义的解决方法?

下面的代码使用gcc 4.8进行编译。 您会注意到,如果未使用move调用“g”(将左值转换为右值),则代码无法编译。 如前所述,绑定成功,因为只有在调用operator()(...)时才会发生失败,因为unique_ptr不可复制。 调用“f”是允许的,因为shared_ptr有一个复制构造函数。

#include <functional>
#include <memory>


void foo1( std::shared_ptr<int> ){}
void foo2( std::unique_ptr<int> ){}

int main() 
{
    using namespace std::placeholders;

    std::function<void(std::shared_ptr<int>)> f = std::bind( foo1, _1 );
    std::function<void(std::unique_ptr<int>)> g = std::bind( foo2, _1 );

    std::unique_ptr<int> i( new int(5) );
    g( move( i ) ); //Requires the move

    std::shared_ptr<int> j( new int(5) );
    f( j ); //Works fine without the move
    return 0;
}

解决方法就是拥有

void MyFunction(std::unique_ptr<int>& arg)

但如果您无法更改功能定义,则无效。

暂无
暂无

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

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