This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Adding moving to basic_string


On Tue, May 17, 2005 at 02:56:43PM +0200, Paolo Carlini wrote:

> Jonathan Wakely wrote:
> 
> >>Therefore, optimizing operator+ is more important in the non-reference
> >>counted case, for use in the very common a = b + c.
> >>    
> >>
> >That case is optimal with ref-counted strings, but it couldn't take
> >advantage of move anyway, since b is not a temporary.
> >  
> >
> Right. We are talking about more than one operator+. Unfortunately we
> cannot do much about the most common case, reference-counting wins, it
> seems.
> 
> >The case I meant was a = b+c+d, which does a deep copy of the result of
> >(b+c) and appends the contents of d to it.  The deep copy is
> >sub-optimal.
> >  
> >
> No, this is not true when reference-counting is in: operator+ over the
> temporary
> (b+c) and d, does *not* deep copy b+c! It does:
> 
>     basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
>     __str.append(__rhs);
> 
> and the copy constructor uses a shallow copy, similarly to operator=.

Yes, but then append() causes a deep copy, right?
Whether the copy happens on line 1 or line 2 doesn't matter ...
operator+ causes a deep copy that isn't stricly required.

This could be avoided if __lhs was marked as a temporary or if we used a
move ctor instead of the copy ctor.

jon

-- 
"Once, during Prohibition, I was forced to live for days on nothing but 
 food and water."
	- W.C. Fields


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]