Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I reliably emplace_back in a vector of a type that does not have an assignment operator?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

These requirements are documented at cppreference.com.

For std::vector<T>::emplace_back :

Type requirements

T (the container’s element type) must meet the requirements of MoveInsertable and EmplaceConstructible.

Additionally std::vector has a general requirement that the type has to be Erasable.

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. For std::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 and CopyConstructible. This has since been relaxed. Now, except for Erasable, the requirements just depend on the operations performed on the vector.

So for modern version of C++ the answer to your question is yes, it is reliable. But if you are using a version older than C++11 the answer is no, it is not supported.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading