If you have a compiler that supports rvalue links, it will be moved to a vector, which is sometimes quite cheap.
An alternative to this is to directly create an object in a vector, which can be done using vec.emplace_back("abc"); . This only calls one constructor.
Both of these are C ++ 11 functions. Copying elision is not allowed here, so without these functions a copy will still be made.
However, if the copy constructor does not have observable side effects (which it should not have in any case), the smart compiler can still perform this optimization because the “as is” rule allows any optimization that leads to the same observed behavior. I do not know if any current compiler is working. If not, I doubt anyone will make an effort to add such optimization because rvalue links put an end to this need.
source share