将unique_ptr引用传递给boost :: bind? [英] Passing a unique_ptr reference to boost::bind?

查看:241
本文介绍了将unique_ptr引用传递给boost :: bind?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CentOS 6.6(gcc 4.4.7),并使用Boost.Asio(1.41)进行开发。我想让io_service在启动时在管理器对象 m 中调用成员函数 run()。我试图编译的代码如下所示:

  #include< memory> 
#include< boost / asio.hpp>
#include< boost / bind.hpp>

boost :: asio :: io_service io;
std :: unique_ptr< manager>米;
m = std :: make_unique< manager>;
io.post(boost :: bind(& manager :: run,& m));

gcc在 boost :: bind 语句,其中包括:

  /usr/include/boost/bind/mem_fn_template.hpp:40:error:pointer to 
成员类型'void(manager ::)()'与对象类型不兼容
'std :: unique_ptr< manager,std :: default_delete< manager> >'

我想在这里做什么?



经理对象只会了解计时器;知道io_service的单独对象将在稍后添加到其构造函数中。但是我们的想法是, manager :: run()会创建一组初始化的定时器来引导系统。



澄清:

我在这里想到的是,外部代码块管理 m 的生命周期,下一个语句将是 io.run()。当 io.run()返回时,外部代码将销毁 m 。因此,将 m 的原始引用传递给 io 是适当的。但是我是一个现代C ++新手,可能会在这里得到解决方案。

解决方案

您需要C ++ - 14和< a href =https://stackoverflow.com/questions/8640393/move-capture-in-lambda/20669290#20669290>广义lambda捕获使这项工作 - 你需要移动唯一指向lambda的指针。相反,只需使用 shared_ptr std :: bind 本身就可以理解:

  std :: shared_ptr< manager>米; 
m = std :: make_shared< manager>();
io.post(std :: bind(& manager :: run,std :: move(m)));

std :: move 是可选的,但确保 m 在不需要的时候不会让管理人员左右。


I'm on CentOS 6.6 (gcc 4.4.7) and developing with Boost.Asio (1.41). I'd like io_service to call member function run() in manger object m when it starts. The code I'm trying to compile looks like:

#include <memory>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

boost::asio::io_service io;
std::unique_ptr<manager> m;
m = std::make_unique<manager>;
io.post(boost::bind(&manager::run, &m));

gcc pitches a fit on the boost::bind statement, which includes:

/usr/include/boost/bind/mem_fn_template.hpp:40: error: pointer to
member type ‘void (manager::)()’ incompatible with object type
‘std::unique_ptr<manager, std::default_delete<manager> >’

What do I want to be doing here?

The manager object will only know about timers; a separate object that knows about io_service will get added to its constructor later. But the idea is that manager::run() will create an initial set of timers to bootstrap the system.

Clarification:

My thinking here is that the outer block of code manages the lifetime of m and that the next statement will be io.run(). The outer code will destroy m when io.run() returns. Hence, passing a raw reference for m to io is appropriate. But I'm a modern C++ novice and could be way off base here.

解决方案

You'd need C++-14 and generalized lambda capture to make this work -- you'd need to move the unique pointer into the lambda. Instead, just use a shared_ptr, which std::bind understands natively:

std::shared_ptr<manager> m;
m = std::make_shared<manager>();
io.post(std::bind(&manager::run, std::move(m)));

The std::move is optional but ensures that m doesn't keep the manager around when it's not wanted.

这篇关于将unique_ptr引用传递给boost :: bind?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆