Let there be some class to which I want to pass an object boost::bind to some method:

 class FOO { void f( /*type???*/ boostbind_, int i_) { /* ??? вызов boostbind с параметром i_*/ } }; 

And there is another class whose method I want to pass:

 class BOO { void b() { int i=0; auto t = boost::bind(&BOO::b2, this, _1, _2, i); obj.f(t, i); } void b2(boost::system::error_code, size_t, int); FOO obj; }; 

How correctly to declare a class method in which boost::bind is transferred and how then to use it?

PS It is understood that the class FOO knows nothing about the class BOO (including its existence)

UPDATE Attempt to do through boost::function produces compilation errors when used with boost::asio::ip::udp::socket::async_receive_from(...) . Sample code (to compile to initialize (which takes a lot of additional code) the socket is not necessary):

 class FOO { public: void f(boost::function<void(boost::system::error_code ec_, size_t size_, int i_)> boostbind_) { s->async_receive_from( boost::asio::buffer(buffer, 2048), endPoint, boostbind_); } char buffer[2048]; boost::asio::ip::udp::endpoint endPoint; shared_ptr<boost::asio::ip::udp::socket> s; }; class BOO { public: void b() { int i = 99; obj.f(boost::bind(&BOO::b2, this, _1, _2, i)); } void b2(boost::system::error_code ec_, size_t size_, int i_) { std::cout << "\nb2"; } FOO obj; }; int main() { BOO a; ab(); system("pause"); return 0; } 

Compilation errors:

Error 1 error C2338: ReadHandler type requirements not met c: \ libs \ boost \ boost \ asio \ basic_datagram_socket.hpp 893 1 Test

Error 2 error C2064: c. \ Libs \ boost \ boost \ asio \ basic_datagram_socket.hpp 893 1 Test

  • Can use boost :: function? - Alex
  • How? Can an example follow the pattern in question? - Dmitry
  • And why do you need it? - VladD
  • @VladD reduce code connectivity, make wrappers for operations of the same type (consisting of several steps). On good, the FOO class should not know anything about the class BOO - Dmitry
  • On then boost :: function, yes. Or update the language version, now this is std :: function. - VladD

1 answer 1

Judging by the documentation in your case, something like this:

 void f(boost::function<void(int x)> boostbind_, int i_); 

The only doubts cause your call to boost::bind , again, judging by the documentation for boost :: bind at the end, you do not need to pass i to bind , since for it you have already transferred placeholders::_2 . Accordingly, in void f() you make a call to boostbind_(i_);

  • And, I don’t need to transfer the parameter i to Dmitry in f -
  • I wrote int x in boost::function based on the documentation, but as it turned out there is no need, I checked your code here , and you are right, in boost::bind do not need to pass arguments i and _2 . - Igor Gilmutdinov
  • Already figured out. It is necessary to specify ONLY in the template boost::function ONLY the arguments for which placeholders are allocated. - Dmitry
  • your function that you pass to async_recieve_from must have the signature void handler (const boost :: system :: error_code & error, // Result of operation. std :: size_t bytes_transferred // Number bytes received.); those. your argument i_ is superfluous, instead you need to bind either a specific value or refuse it altogether. - Igor Gilmutdinov
  • I cannot edit the top comment, but I see that you have dealt with the problem. - Igor Gilmutdinov