1

Putting aside the move semantics at this moment, is vector reallocation and vector::push_back required by the standard to use placement new and copy constructor? If so where does it in the standard say so?

Tyy
  • 23
  • 2
  • possible duplicate of ["CopyConstructible" requirement for C++ stl container element](http://stackoverflow.com/questions/6532173/copyconstructible-requirement-for-c-stl-container-element) – LihO Feb 18 '13 at 16:12
  • Well it is not exactly the same question, but I think you will find there everything that you are looking for. – LihO Feb 18 '13 at 16:15
  • Apologize if I duplicated. But my understanding is that "CopyConstructible" is a requirement which the implementation does not necessarily actually use. – Tyy Feb 18 '13 at 16:20
  • not an answer, but: with a possibly throwing move constructor it's unsafe to move items from old buffer to new. one might get a failure situation impossible to recover from. so the question then is, is `std::vector` at all *permitted* to move instead of copying? – Cheers and hth. - Alf Feb 18 '13 at 16:24

2 Answers2

4

Yes and no. Vector, like all other containers, is required to use allocator_traits<allocator_type>::construct and allocator_traits<allocator_type>::destroy (§23.2.1/3) and to obtain all memory using the allocator (§23.2.1/7). If you provide a custom allocator, you can do just about anything. The default allocator is required to call ::operator new (§20.6.9.1/6) and to use placement new (§20.6.9.1/12).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • It seems that this answer doesn't discuss the question about whether copy constructor is required to be used? – Cheers and hth. - Alf Feb 18 '13 at 16:51
  • @Alf Good point. I forgot to mention that. But is it necesary; if the library copies something, the copy constructor will be used. Except that it's used through placement new, if you're using the standard allocator, and through whatever you've provided otherwise. – James Kanze Feb 18 '13 at 17:22
0

C++11 §23.3.6.3, about std::vector::reserve:

“If an exception is thrown other than by the move constructor of a non-CopyInsertable type, there are no effects.”

This means that for buffer reallocation with a non-CopyInsertable item type, an implemention is free to move items from the old buffer to the new one, if possible, then not using the type’s copy constructor.

The effect of an exception from a move constructor is left unspecified in the paragraph quoted above, and this is made explicit in §23.3.6.5 about insert and push_back: “ If an exception is thrown by the move constructor of a non-CopyInsertable T, the effects are unspecified.”

Use of placement new is implied because that’s the language’s only feature to construct an object in pre-existing storage. As James Kanze notes in his answer, it’s also explicitly required via the requirements on allocators, and by §23.2.1/2 requirement that the allocator’s construct method be used.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331