I made some tests in GCC, Clang and MSVC and found out that emplace_back
never calls an assignment operator on the contained class. It only calls the copy or move constructor when reallocation takes place. Is this behavior somehow guaranteed by the standard?
The use case is that I have some classes to be sequentially stored in a number that only grows along time until the whole vector is destructed. I'd be happy to clean my code from the assignment operators.
These requirements are documented at cppreference.com.
For
std::vector<T>::emplace_back
:Additionally
std::vector
has a general requirement that the type has to beErasable
.EmplaceConstructible requires that the type is constructible using the given allocator, with the arguments provided. Since you are using the default allocator
std::allocator<T>
, this just means that the type has an accessible constructor with those arguments.MoveInsertable requires that the type is constructible using the given allocator, using an rvalue of type
T
. Forstd::allocator
this means that the type has an accessible move constructor.Erasable requires that the type is destructible using the given allocator. For
std::allocator
this means that is has an accessible destructor.That's it (except for some rules about complete vs. incomplete types). There are no requirements that mention assignment operators.
But until C++11, it used to be that the type always had to be be
CopyAssignable
andCopyConstructible
. This has since been relaxed. Now, except forErasable
, the requirements just depend on the operations performed on the vector.