This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Adding moving to basic_string
Kenneth Duda wrote:
Hi everyone,
Here is a solution to
s = a + b + c + d
that boils down to one malloc followed by 4 memcpy's. This may be
unworkable within the context of std::basic_string for all sorts of
reasons that I can't imagine, but in case you guys hadn't considered
this approach, I thought I'd offer it. (If it is unworkable, I'm
curious why). Code is below, along with its output that demonstrates
that it avoids extra copies.
-Ken
The reason that this, and many other possible cool optimisations can't
be implemented is by my reading the standard imposes exactly what the
return conditions of various functions must be.. in this case its
template<class charT, class traits, class Allocator>
basic_string<charT,traits,Allocator>
operator+(const basic_string<charT,traits,Allocator>&lhs,
const basic_string<charT,traits,Allocator>&rhs);
Therefore the following program should return 1..
#include <string>
#include <iostream>
template<class T, class U>
struct same_type
{ const static int val = 0; };
template<class T>
struct same_type<T,T>
{ const static int val = 1; };
template<class T, class U>
int check_same_type(T t, U u)
{return same_type<T,U>::val;}
int main(void)
{
std::string s;
std::cout << check_same_type(s,s+s);
}
It is strange that in some places the type of various things is given
exactly, and in others it only have to be "convertable to", and
hopefully a future version of the standard will be more clear on
convertable to, and also weaken it somewhat so we can do more useful
things.. for now however we are stuck as is..