Boost::bind a method with boost::function parameter
I would like to provide an extra boost::function to a async_write. I want the connections own HandleWrite function to be called first and then call the provided boost::function.
Member method of Connection that binds to asio async_write
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); };
Trying to bind HandleWrite to a asio async_write and provide another bind as the value for handler. This doesn't compile. What am I doing wrong?
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 ) ); }
The error message I get from the compiler is the following:
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 n开发者_StackOverflowot 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
What error(s) are you getting exactly? I don't see anything obviously wrong in the code shown in your question, so I can't give you a direct answer.
However, Kornel's answer made me doubt, as I thought that the functors generated by boost::bind can take any number of arguments and simply ignore the extra ones.
So I quickly hacked this to verify:
#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;
}
which is sketching what I guess you're trying to do.
As expected, it does compile, so comparing it with what you're actually doing may help you finding the problem.
The problem turned out to be in another place that used the same HandleWrite function and wasn't bound correctly. After fixing that it compiled.
精彩评论