Boost logo

Boost Users :

Subject: [Boost-users] [asio] documentation example, bind semantics
From: John M. Dlugosz (mpbecey7gu_at_[hidden])
Date: 2012-05-03 02:59:30


In boost_asio/tutorial/tutdaytime3.html, the code shown is:

  void handle_accept(tcp_connection::pointer new_connection,
       const boost::system::error_code& error)
   {

and is called via a function object:
  boost::bind(&tcp_server::handle_accept, this, new_connection,
           boost::asio::placeholders::error)

I get after my coworkers I'm mentoring in the area of C++ skills to pass smart pointers by
const-ref not by value, since the atomic inc/dec is very costly.

I believe that a boost::function (and now std::function) will hold the object by value
even if the function being bound declares it as reference, and the INVOKE phase will bind
the parameter to the stored copy in the function object.

So handle_accept can and should be declared as using a

const tcp_connection::pointer&

as well, right? Is there any reason not to?

—John


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net