This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/52938] std::string::reserve request is not maintained if object is used in other object copy ctor
- From: "abdul.tohmaz at emc dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 11 Apr 2012 23:50:21 +0000
- Subject: [Bug libstdc++/52938] std::string::reserve request is not maintained if object is used in other object copy ctor
- Auto-submitted: auto-generated
- References: <bug-52938-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52938
--- Comment #6 from Abdul Tohmaz <abdul.tohmaz at emc dot com> 2012-04-11 23:50:21 UTC ---
Guys, I do value your points, but please consider the following:
I can't always be in control when a copy is made of my string. Here is a quick
example.
string x;
x.reserve(1024); //no memory realloc for my string up to 1024
x="aaa";
x+= "bb"; // so far no memory realloc.
//now I call a function by third party library whose
//signature is void doit(const string&).
doit(x);
x+= "cc"; //I am expecting no memory realloc here.
//but in fact the last statement will reallocate memory if
//doit put a copy of my string in a vector: ex
//void doit (const std::string& val){ vec.push_back(val);}
Don't you think this is a problem? I called reserve on x to avoid memory
reallocation, and yet I am faced with memory reallocation just because I call
doit; whose signature (const ref) suggests that my object should not be
changed.