Does std :: vector :: data () move?

Possible duplicate:
Does vector transfer invalidate iterators?

Consider the following codes:

std::vector<T> prepare(T*& data) { std::vector<T> buffer; // Fill in buffer. data = buffer.data(); return buffer; } ... T* data; auto vec = prepare(data); // line 12 

Is it possible that vec.data() != data on line 12? Similarly

 std::vector<T> buffer; // ... Fill in buffer ... T* data = buffer.data(); auto vec = std::move(buffer); // line 5 

Is it possible that vec.data() != data on line 5?

Almost both options are impossible with the implementation of libstdC ++ and libC ++, since the move constructors are implemented as simple pointer assignments, but it seems that the standard does not point to it (similarly. Is storage required when moving std :: vector? ). Can "persistent complexity" guarantee that vec.data() == data ?

+6
source share
1 answer

Constant complexity means that the container is not allowed to copy / move individual elements, so it must transfer ownership of the existing storage to a new object, so the pointer returned by data() must be the same.

For assignment of forwarding (and not for moving), this is true only if propagate_on_container_move_assignment true for the type of vector allocator, or distributors compare equal.

0
source

All Articles