Reputation: 1882
Our team just ran into the same issue described here http://forums.codeguru.com/archive/index.php/t-514404.html, i.e. calling some_vec.resize(new_size)
, where N = new_size - some_vec.size()
with N >= 2, and VC10 default-constructed all N new elements, whereas GCC default constructed a single element, as the prototype element, to copy-construct it N times for the new elements.
Since this is a vector of uuid, where the default-constructor randomly initializes each new instance, we ended up with N times the same uuid with GCC and N different uuids with VC. That was enough to wreak havoc in our test suite on one platform but not another, and was no fun to find.
My question is thus: who is right? VC or GCC? Or is this one of those beloved corners of C++ that's unspecified? TIA, --DD
Upvotes: 5
Views: 209
Reputation: 131789
I bet if you compile GCC with -std=c++0x
, you'll get the same result as with MSVC, that is, the N default constructions. This has changed in C++11, see here. There are now two overloads, one with just the new size that default constructs the new elements, and another that takes a "prototype" parameter to copy-construct every new element from.
Now, to get consistent results no matter what mode you compile in, just use
v.resize(new_size, T());
Background info: The change was necessary, since there are now types that can be movable, but not copyable (like std::unique_ptr
). The old signature demanded copyability. Now the first template parameter to standard container types is only required to be copyable, if you use operations that require a copy.
Upvotes: 6