c++ - Functional programming with boost: pass a boost::bind to a boost::bind -


I'm writing to try to get a functional approach for some code. Want to pass in another function that does something with it, and then to schedule a later function at a boost :: asio :: io_service .

I inspired the definition of ( io_service :: post ) template ):

  #include & lt; Stdio.h & gt; # Include & lt; Boost / asio / io_service.hpp & gt; # Include & lt; Boost / bind.hpp & gt; Template & lt; Typename FUNC & gt; Zero foo_then_smile (FUNC handler) {handler (); Printf ("smile \ n!"); } Zero foo (int a, int b, int c) {printf ("important answer is% d \ n", a + b + c); } Int main () {boost :: asio :: io_service ioService; IoService.post (boost :: bind (foo_then_smile, boost :: bind (foo, 1, 2, 3))); Return 0; }   

But I'm getting:

  to call "bind (& lt; unsolved loaded type type & gt ;, :: : BI :: bind_t & lt; Void, void (*) (int, int, int), boost :: _ bi :: list3 & quot; boost :: _ bi :: value & gt; int & gt; boost :: _ bi :: value & lt; int & gt;, boost: _ bi:: value >> 

candidates

After a big list of

And if you can not use c ++ 11, then you can use the function template as a multicolor Functions can be converted into objects:

  struct foo_then_smile {typedef void result_type; Template & lt; Typename FUNC & gt; Zero operator () (FUNC handler) const {handler ()); Printf ("smile \ n!"); }};   

and use it (to avoid mixing the bind to protect to note ): < Pre> IoService.post (boost :: bind (foo_then_smile), boost :: security (boost :: tie (foo, 1, 2, 3))));

See this #include & lt; Stdio.h & gt; # Include & lt; Boost / asio / io_service.hpp & gt; # Include & lt; Boost / bind.hpp & gt; #include & lt; Boost / bind / protect.hpp & gt; Struct foo_then_smile {typedef zero result_type; Template & lt; Typename FUNC & gt; Zero operator () (FUNC handler) const {handler ()); Printf ("smile \ n!"); }}; Zero foo (int a, int b, int c) {printf ("important answer is% d \ n", a + b + c); } Int main () {boost :: asio :: io_service ioService; IoService.post (boost :: tie (foo_then_smile), boost :: security (boost :: tie (foo, 1, 2, 3))); IoService.run (); }

Print:

  Important answer 6 smile!    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -