Boost logo

Boost Users :

Subject: Re: [Boost-users] Aliased parameters in boost::bind
From: Rainer Deyke (rainerd_at_[hidden])
Date: 2017-03-30 07:34:36


On 30.03.2017 00:12, Gavin Lambert via Boost-users wrote:
> const& parameters should be your default for anything with non-trivial
> copy/move behaviour anyway, so you'd only run into this issue in
> already-bad code.

Passing by const& can actually force extra copies to be created that
would not be created with pass-by-value.

Consider:

std::string f();

struct X {
   void by_value(std::string s) {
     this->some_member = std::move(s);
   }

   void by_const_ref(std::string const& s) {
     // Cannot move from const&
     this->some_member = s;
   }

   std::string some_member;
};

X x;

// The following line creates no copies.
x.by_value(f());

// The following line must create an extra copy.
x.by_const_ref(f());

-- 
Rainer Deyke - rainerd_at_[hidden]

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