Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] documentation example, bind semantics
From: Igor R (boost.lists_at_[hidden])
Date: 2012-05-03 07:06:39


> 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.)


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