I think returning shared_ptrA better alternative is probably to return by const reference. This avoids (a potentially expensive) copy operation, but does not allow the accessor to alter the vector If you are returning a local std::vector you could also return it by argument If you really want to return shared_ptr.
I think returning shared_ptr> rarely is useful. I would only do this if several objects where to hold a shared vector that they could manipulate. To me this indicates a design flaw.
A better alternative is probably to return by const reference. This avoids (a potentially expensive) copy operation, but does not allow the accessor to alter the vector. If you are returning a local std::vector you could also return it by argument.
If you really want to return shared_ptr>, consider if shared_ptr> would do the job (the vector can be inspected by many, but only manipulated by the owner). However A is generally more expensive than B, but return value optimizations often applies here. For C++11 std::vector has a move constructor that will guarantee that returning a local std::vector won't require expensive copy operations.
Remember, don't prematurely optimize :).
All of this is ignoring RVO/NRVO which makes the question moot in the first place. – ildjarn Nov 2 at 20:53.
In C++03, prefer: If providing access to a member, return by const reference. Else if returning a local, use an out reference parameter. In C++11, prefer: If providing access to a member, return by const reference.
Else if returning a local, return by by value, as move construction will avoid excessive copies. The situation where you would want to return a reference counted smart pointer would be where multiple objects with disjoint lifetimes will all want to access the object. This is not the most common scenario however, but it does happen often enough.
I disagree with both #2 suggestions. Copy elision/RVO/whatever-they-want-to-call-it can make a lot of returning of containers cheap. And unless you're returning a temporary, move constructors will only be invoked if you explicitly ask for them via std::move.
Either way, it's better to use copy elision. – Nicol Bolas Nov 3 at 2:56.
Returning shared_ptr> guarantees no extra copy will occur. Returning vector may avoid extra copy if return value optimization (RVO) kicks in, or if move semantics from C++11 is used. But if it's important to avoid copy I wouldn't use this (even if you can ensure these optimizations will always be available), because I don't think it's a good idea to use return-a-copy semantics while actually meaning not copying.
I would probably go with one of these, but it depends: pass a reference to vector as parameter pass insert iterator (e.g. Back_inserter) instead.
About the reference: Right, because it will be destroyed in the end of the function scope. I'm cross compiling over different platforms, ios using xCode, vs2010. Whcih do you recommand then?
– Alon Amir Nov 2 at 8:01 @AlonAmir See the updated answer. If avoiding copy is important, don't pass vector by value. – hamstergene Nov 2 at 8:29 3 pass a reference to the vector as parameter: I wouldn't, if the function creates the vector internally, then it is a return value.
The calling convention for all compilers I know of, in the case of a return type that does not fit in registers will implement that for you: the caller allocates the space and passes a pointer to the calle, the callee then initializes that memory with a real object (calling the appropriate constructor depending on the code). The effect is that you get a cleaner interface with exactly the same cost. – David Rodríguez - dribeas Nov 2 at 8:30 @DavidRodríguez-dribeas Right, that's exactly what I'm trying to avoid, making functions that receive "out" parameters.
– Alon Amir Nov 2 at 8:32.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.