-
copy constructor in MSVC
I noticed that when you add an object to a vector in MSVC 2005 the copy constructor of that objects is called twice. There is a new temporary created inside the vector::insert code with the comment "// in case _Val is in sequence"
In gcc it is only called once.
Anyone know why the MSVC implementation does this? I dont get the comment next to it.
-
It might be easier to understand if you posted the appropriate code snippets. Also, note that copy construction can be elided.
-
This would avoid situations such as
Code:
vec.insert(vec.end(), vec[0]);
vec[0] is a reference into the same vector. Now imagine that the buffer needs to be resized. If the implementation is not careful, it might end up using a reference to the object it has just moved to a different place.
Or another problem:
Code:
vec.insert(vec.begin(), vec.back());
Even if the vector didn't reallocate the memory, if it started by making room for the new item, vec.back() might end up referencing the last but one item before it is inserted.