Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are std::vector required to use move instead of copy?

Tags:

c++

c++11

vector

Are std::vector required to use move instead of copy, at reallocation on grow/swap/erase by standard? Or this is purely implementation optimization?

If std::vector required to use move, at what conditions? Where these rules/conditions can be read?

I found this How to enforce move semantics when a vector grows? . But there are no answers about guarantees.

UPDATE

When it required to use move constructor on reallocation? And where it says that it required to reallocate with move semantic?

like image 727
tower120 Avatar asked Jan 30 '23 07:01

tower120


1 Answers

Are std::vector required to use move instead of copy, by standard?

I presume that you refer to move and copy of the elements.

Neither copy, nor move is necessarily required to be used at all, if you don't use the member functions that do those things. Some member functions are required to copy elements and other member functions are required to move elements.

When it required to use move constructor on reallocation

It is required to use move if the element is not copy insertable (this requires no rule, you can't do what you can't do). Otherwise it is required to copy if the move constructor can throw (this is required by strong exception guarantee quoted below). If non-throw of move can be proven, then it seems to be up to implementation, as I don't find any further guarantees. A good implementation would move when it is noexcept.

The exception guarantee on relevant functions:

If an exception is thrown other than by the move constructor of a non-CopyInsertable T there are no effects.

like image 150
eerorika Avatar answered Feb 05 '23 15:02

eerorika