I use visual studio 2010 and its stl implementation is from dinkumware
I noticed that when internal buffer of vector grows, there is a _Uninitialized_move is called. which is much like std::uninitialized_copy.
- for scalar type, it eventually forwards call to memmove
- for non-scalar type, it will call copy constructor of contained type.
I know that for non-scalar type, bit-wise coping isn’t safe, but I am get confused in this case, because the old objects will be destroyed very soon. it looks like that bit-wise coping for all types is safe, and hence we only need free raw buffer after _Uninitialized_move. this will save lots of copy constructor and destructor for non-trivial objects.
so, is it safe to just move contents for non-scalar type?
If you’re completely sure that objects you move don’t have pointers to each other (subobjects of those included) – then just moving contents is okay. However you can’t know that unless you know how the type is designed inside. One exception is that if the type is not larger than a pointer (
sizeof(Type) <= sizeof( void* )), then it is very unlikely to have pointers to objects in the same container, so usually it can be just moved.