How many times does std :: vector :: resize have to create new elements by default?

Our team just faced the same problem described here http://forums.codeguru.com/archive/index.php/t-514404.html , that is, a call some_vec.resize(new_size)where, N = new_size - some_vec.size()with N> = 2 and VC10, by default, everyone designed N new elements, while GCC by default constructed one element as a prototype element to copy-build it N times for new elements.

Since this is a uuid vector, 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. This was enough to cause damage in our test suite on one platform, but not in another, and there was no pleasure in finding it.

My question is: who is right? VC or GCC? Or is it one of those favorite corners of C ++ that is unspecified? TIA, --DD

+5
source share
1 answer

I bet if you compile GCC with -std=c++0x, you get the same result as with MSVC, i.e. with the default N constructs. This has changed in C ++ 11, see here . Now there are two overloads: one with a new size, which by default creates new elements, and the other with the "prototype" parameter to copy-assemble each new element.

Now, to get consistent results no matter what mode you compile, just use

v.resize(new_size, T());

: , , , (, std::unique_ptr). . , , .

+6

All Articles