Topic: N2284 : The version of basic_string::operator+() taking two rvalue


Author: Joe Gottman <jgottman@carolina.rr.com>
Date: Wed, 11 Jul 2007 22:07:24 CST
Raw View
The latest draft of the working paper for C++09 has the following spec
for the version of basic_string::operator+() that takes two rvalue
references:

     template<class charT, class traits, class Allocator>&&
       basic_string<charT,traits,Allocator>
         operator+(basic_string<charT,traits,Allocator>&& lhs,
                   basic_string<charT,traits,Allocator>&& rhs);

-4- Returns: lhs.append(rhs). [Note: or equivalently rhs.insert(0, lhs)
--end note]


    There's a slight problem with the returns clause and the
accompanying note: lhs.append(rhs) and rhs.insert(0, lhs) are not really
equivalent. If lhs has a large enough capacity() to hold the sum of the
two strings without reallocation and rhs doesn't then returning
lhs.append(rhs) is much more efficient than returning rhs.insert(0,
lhs).  On the other hand, if rhs has a large enough capacity() and lhs
doesn't then the opposite is true.  Since the entire reason for defining
this overload is to avoid reallocation, shouldn't the returns clause
specify that we chose an option that doesn't reallocate if one exists?

Joe Gottman

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]