Message from C, C++ discussions

January 2020

— I have a question, when we return by value, the copy of the returned object is created and then that copy is returned so is copy constructor called during this copying?

Message permanent page


Yep but either the object gets moved or with rtvo (return value optimization) this copy gets eleminated (except maybe for primitives)

— Well we should always stick to the object with which the operator was called. What if multiple exist and we always return cout we might return the wrong object because it was called on something else

Message permanent page

— Also what is the lifetime of the newly created copy?

— Depends if it's a deep copy or a shallow copy if I'm not mistaken

— Well, when we return by value, how do we know whether it is deep/shallow?

— If it's a single value being returned that gets stored in some variable it will have the lifetime of the variable

Message permanent page

— It's different for objects and I'm vague on exactly what it does. Lemme find a source

— Http://

— It depends if it is a pure value or a dynamic allocated object/pointer/reference/rvalue ref

— C++ I suppose? Usually custom copyctor implements deep copy, or if fields are either trivial or have custom copyctors

Message permanent page

— user-provided's the name