gpt4 book ai didi

c++ - Boost::bind 一个带有 boost::function 参数的方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:24:35 31 4
gpt4 key购买 nike

我想为 async_write 提供一个额外的 boost::function。我希望首先调用连接自己的 HandleWrite 函数,然后调用提供的 boost::function。

绑定(bind)asio async_write的Connection成员方法

void Connection::HandleWrite(
const boost::system::error_code& e,
boost::function<void (const boost::system::error_code&)> handler)
{
// Code removed for clarity

if(!handler.empty())
handler(e);
};

尝试将 HandleWrite 绑定(bind)到 asio async_write 并提供另一个绑定(bind)作为处理程序的值。这不编译。我做错了什么?

  void Connection::QueueRequest(
boost::shared_array<char> message,
std::size_t size,
boost::function<void (const boost::system::error_code&)> handler)
{
// Code hidden for clarity

boost::asio::async_write(m_Socket, boost::asio::buffer(buffer),
boost::bind(&Connection::HandleWrite, shared_from_this(),
boost::asio::placeholders::error,
handler
)
);
}

我从编译器得到的错误信息如下:

Error 1   error C2825: 'F': must be a class or namespace when followed by '::'    boost\bind\bind.hpp 69
Error 2 error C2039: 'result_type' : is not a member of '`global namespace'' boost\bind\bind.hpp 69
Error 3 error C2146: syntax error : missing ';' before identifier 'type' boost\bind\bind.hpp 69
Error 4 error C2208: 'boost::_bi::type' : no members defined using this type boost\bind\bind.hpp 69
Error 5 fatal error C1903: unable to recover from previous error(s); stopping compilation boost\bind\bind.hpp 69

最佳答案

您到底遇到了什么错误?我没有看到你问题中显示的代码有任何明显的错误,所以我不能给你一个直接的答案。

但是,Kornel 的回答让我产生了疑问,因为我认为由 boost::bind 生成的仿函数可以接受任意数量的参数,而忽略多余的参数。

所以我很快破解了这个来验证:

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/function.hpp>
#include <string>
#include <iostream>


void Foo(const boost::system::error_code&)
{
// whatever
}

struct Client : boost::enable_shared_from_this<Client>
{
void HandleWrite(
const boost::system::error_code& Err,
boost::function<void(const boost::system::error_code&)> OtherHandler
)
{
std::cout << "MyHandler(" << Err << ")\n";
OtherHandler(Err);
}

void MakeTheCall(boost::function<void (const boost::system::error_code&)> Other)
{
using boost::asio::ip::tcp;

// Of course, the scope and initialization of
// io_service, sock and request are all wrong here,
// as we're only interested in testing if the async_write
// call below will compile.
// Don't try to run this at home!
boost::asio::io_service io_service;
tcp::socket sock(io_service);
boost::asio::streambuf request;

boost::asio::async_write(sock, request,
boost::bind(&Client::HandleWrite, shared_from_this(),
boost::asio::placeholders::error,
Other
)
);
}
};


int main()
{
boost::shared_ptr<Client> c;
c->MakeTheCall(boost::bind(&Foo, _1));

return 0;
}

这是我猜你想做的事情的草图。

正如预期的那样,它确实可以编译,因此将它与您实际执行的操作进行比较可能会帮助您找到问题所在。

关于c++ - Boost::bind 一个带有 boost::function 参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2155159/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com