【问题标题】:How to use Boost::asio::buffer(buf, size) with boost bind?如何将 Boost::asio::buffer(buf, size) 与 boost 绑定一起使用?
【发布时间】:2011-11-04 23:10:47
【问题描述】:

我们在一些 .h 文件中有一个成员函数

template <typename MutableBufferSequence> 
int read_some(boost::asio::ip::tcp::socket& sock, 
    const MutableBufferSequence& buffers) 
{
    return sock.read_some(buffers);
}

我们希望在我们的一个类函数中包含这样的代码:

boost::packaged_task<int> pt(boost::bind(&http_request::read_some, this, &socket, boost::asio::buffer(buffer, buffer_size)));

这给了我 87 个编译器错误,并告诉我 boost::bind 不能以这种方式工作。我想知道如何通过boost::bindboost::asio::buffer 传递给我的函数?

【问题讨论】:

  • 您作为第三个参数传递给boost::bind()socket 变量的类型是什么?

标签: c++ boost boost-asio boost-bind


【解决方案1】:

你必须告诉编译器哪个你想绑定read_some。由于是模板,所以传入bind()时必须指定模板参数。

在你的情况下,你想要http_request::read_some&lt;boost::asio::mutable_buffers_1&gt;

附带说明,您还错误地传入了套接字对象本身。您正在传递一个指向套接字的指针,并且该函数需要一个引用。不要将&amp;socket 传递给bind,而是传递boost::ref(socket),或者您可以使函数采用套接字指针而不是引用。

【讨论】:

    猜你喜欢
    • 2021-05-06
    • 2012-10-01
    • 2018-08-28
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多