如果标准::绑定与升压:: ASIO兼容? [英] Should std::bind be compatible with boost::asio?

查看:129
本文介绍了如果标准::绑定与升压:: ASIO兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图适应升压之一:: ASIO例子,可以使用C ++ 11 / TR1库。原来的code是这样的:

I am trying to adapt one of the boost::asio examples to use c++11 / TR1 libraries where possible. The original code looks like this:

void start_accept()
{ 
  tcp_connection::pointer new_connection =
    tcp_connection::create(acceptor_.get_io_service());

  acceptor_.async_accept(new_connection->socket(),
      boost::bind(&tcp_server::handle_accept, this, new_connection,
         boost::asio::placeholders::error));
}

如果我更换的boost ::绑定的std ::绑定如下:

void start_accept()
{ 
  tcp_connection::pointer new_connection =
    tcp_connection::create(acceptor_.get_io_service());

  acceptor_.async_accept(new_connection->socket(),
      std::bind(&tcp_server::handle_accept, this, new_connection,
                 boost::asio::placeholders::error ) );
      // std::bind(&tcp_server::handle_accept, this, new_connection, _1 ) );
}

我收到了大量的错误信息,与两端:

I get a large error message, with ends with:

/usr/include/c++/4.4/tr1_impl/functional:1137: error: return-statement with a value, in function returning 'void'

我使用的gcc版本与4.4版本提升1.47

I am using gcc version 4.4 with boost version 1.47

我的预期提振::绑定和std ::绑定是可以互换的。

I expected boost::bind and std::bind to be interchangeable.

推荐答案

我现在有一个解决方案

问题是,当我第一次尝试切换到 的std ::绑定 的std :: shared_ptr的 我还是用了的boost ::支持ASIO ::占位符的std ::绑定,这导致了大量的模板编译器错误的,所以后来我试图零碎切换。

The problem is that when I first tried to switch to std::bind and std::shared_ptr I was still using the boost::asio::placeholders with std::bind, this resulted in a large amount of template compiler errors, so I then tried to switch piecemeal.

我第一次尝试切换刚的boost :: shared_ptr的的std :: shared_ptr的,这个失败,因为的boost ::绑定不会的std :: shared_ptr的与制定出模板 get_pointer℃的专业化;类型名T&GT; 的std :: shared_ptr的(参见:<一href=\"http://stackoverflow.com/questions/4682343/how-to-resolve-conflict-between-boostshared-ptr-and-using-stdshared-ptr\">How解决的boost :: shared_ptr的和使用std之间的冲突:: shared_ptr的?)。

I first tried switching just boost::shared_ptr to std::shared_ptr, this failed because boost::bind will not work with std::shared_ptr with out a specialisation of the template get_pointer<typename T> for std::shared_ptr (see: How to resolve conflict between boost::shared_ptr and using std::shared_ptr?).

使用切换到std :: shared_ptr的话,我切换到的std ::绑定,此时<一后href=\"http://en.cp$p$pference.com/w/cpp/utility/functional/placeholders\"><$c$c>std::placeholders, (感谢理查德)的例子code现在编译并正常工作。

After switching to std::shared_ptr I then switched to std::bind, this time using the std::placeholders, (thanks richard) the example code now compiles and works correctly.

为了使用的std ::绑定的boost :: ASIO 确保的std :: shared_ptr的的std ::占位符也可使用。

In order to use std::bind with boost::asio make sure that std::shared_ptr and the std::placeholders are also used.

这篇关于如果标准::绑定与升压:: ASIO兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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