Home > Back-end >  Vs, STD: : vector and insert it into the other position and insert the end, expand the space of the
Vs, STD: : vector and insert it into the other position and insert the end, expand the space of the

Time:04-16

As title,
In VS, when you insert before the end, the vector extension space using the mobile constructor;
Expand when inserted in the end, the vector space using the copy constructor;
Itself, I have a class only allow mobile structure or copy, not the copy constructor or copy... Then, when using the vector container when off

 

Void F () {
Struct A {
Int v.
(A) {printf (CODEPOS_ "A (the) \ n"); }
~ (A) {printf (CODEPOS_ "\ n"); }
A (const A&) {printf (CODEPOS_ "A (const A&) \n"); };
A (A& & A (r) {printf (CODEPOS_ "A& & R \ n "); R.v=0; };
A& Operator=(A& & R) {printf (CODEPOS_ A& "" Operator=(A& & R \ n "); R.v=0; return *this; };
A& Operator=(const A&) {printf (CODEPOS_ A& "" Operator=(const A&) \ n ");; return *this; };
};
STD: : vector A (1);
A, b.
//vector into other position and insert the end, expand the space of the method is not the same as
//insert other location: call _Uninitialized_move, call mobile constructor
//insert the end: call _Uninitialized_copy, calls the copy constructor
/*
V & lt; Vector> Source:
TemplatePointer _Emplace_reallocate (const pointer _Whereptr, _Valty & amp; & . _Val)
{//reallocate and insert by perfectly forwarding _Val at _Whereptr
.
If (_Whereptr==this - & gt; _Mylast ())//insert the end: call _Uninitialized_copy, calls the copy constructor
{//at the back, dojo.provide strong guarantee
_Umove_if_noexcept (this - & gt; _Myfirst (), and this - & gt; _Mylast (), _Newvec);
}
Else//insert other location: call _Uninitialized_move, call mobile constructor
{//dojo.provide basic guarantee
_Umove (this - & gt; _Myfirst (), _Whereptr _Newvec);
_Constructed_first=_Newvec;
_Umove (_Whereptr, this - & gt; _Mylast (), _Newvec + _Whereoff + 1);
}
.
}
*/
Anderson, nsert (a. do v.begin (), STD: : move (b));//call mobile constructor to expand space
Anderson, nsert (a.e nd (), STD: : move (b));//call the copy constructor to expand space

}

CodePudding user response:

Move the constructor usually expect not throw exceptions, you this to the movement of the thrown exception constructor standard library containers are not to use,
You have to add exception details: noexcept, promise not to throw exception ~ ~
A (A& & A (r) noexcept {printf (CODEPOS_ "A& & R \ n "); R.v=0; };

CodePudding user response:

I tried on GCC4.8.5, with insert position it doesn't matter, print exactly the same as
And floor said is right, do not add noexcept standard library is not called, added a call