[asio] documentation example, bind semantics
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
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?
IMHO, in this particular case it's just a matter of style, because the performance benefit is negligible here (new_connection gets copied anyway - and maybe even several times, handle_accept gets called rarely, etc.)
participants (2)
-
Igor R
-
John M. Dlugosz