This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[Patch]New feature to all kind of stl container.vector::data_move() returns vector::data() that can be moved,just as unique_ptr::release() to unique_ptr::get()
- From: "=?gb18030?b?98jTsMql0/I=?=" <805600352 at qq dot com>
- To: "=?gb18030?b?bGlic3RkYysr?=" <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 8 Jan 2015 09:51:59 +0800
- Subject: [Patch]New feature to all kind of stl container.vector::data_move() returns vector::data() that can be moved,just as unique_ptr::release() to unique_ptr::get()
- Authentication-results: sourceware.org; auth=none
I have a new feature to add to class string,vector and so on.The feature is easy in the form like this:
string::data_move()
string::c_str_move()
vector::data_move()
and so on
X::xxx_move() can return something equal to X::xxx() except that you can transfer/move the returned value (which is on the heap) without copying.And when X::xxx_move() is called then X loses control of the resouces and will destruct nothing.
In comparison:
vector<int> v1{0,1};
vector<int> v2{move(v1)};//can only transfer ownership between object of same type;
char* p=v1.data_move();XXX.setData(p);//can transfer ownership to anything that holds a char*;
If this don't interest you because vector is used widely,let's look at stringstream,since no one will forget to convert it to a string when it has finished it's job:
stringstream ss;
for(int i=0;i<10000000;++i)ss<<i<<' ';
setString(ss.str());//you can't save this heavy copy in ss.str()
setString(ss.str_move());don't have to leave the heap memory of ss for fear that ss's destructor will loss its job
I name the feature HAPPY STEALING.^_^
Anyone agree with me?