19

I am really confused now on how and which method to use to return object from a function. I want some feedback on the solutions for the given requirements.

Scenario A: The returned object is to be stored in a variable which need not be modified during its lifetime. Thus,

const Foo SomeClass::GetFoo() {
 return Foo(); 
}

invoked as:

someMethod() {
 const Foo& l_Foo = someClassPInstance->GetFoo();
//...
}

Scneraio B: The returned object is to be stored in a variable which will be modified during its lifetime. Thus,

void SomeClass::GetFoo(Foo& a_Foo_ref) {
     a_Foo_ref = Foo(); 
    }

invoked as:

someMethod() {
 Foo l_Foo;
 someClassPInstance->GetFoo(l_Foo);
//...
}

I have one question here: Lets say that Foo cannot have a default constructor. Then how would you deal with that in this situation, since we cant write this anymore:

Foo l_Foo

Scenario C:

Foo SomeClass::GetFoo() {
 return Foo(); 
}

invoked as:

someMethod() {
 Foo l_Foo = someClassPInstance->GetFoo();
//...
}

I think this is not the recommended approach since it would incur constructing extra temporaries.

What do you think ? Also, do you recommend a better way to handle this instead ?

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
brainydexter
  • 19,826
  • 28
  • 77
  • 115
  • 2
    http://en.wikipedia.org/wiki/Return_value_optimization – vladr Apr 11 '10 at 05:21
  • scenario A is fine but really no improvement over C. I'd use C unless Foo's default constructor is "fast", Foo's copy ctor is "slow" and you don't want to rely on the compiler being smart enough to elide unnecessary copies in which case B is also acceptable. – sellibitze Apr 11 '10 at 05:31
  • note that `a_Foo_ref = Foo();` creates object and performs copy. It is nearly equivalent to the last scenario – Anycorn Apr 11 '10 at 05:33
  • @aaa: No, it does not. That statement is allowed to construct only a single object. – Billy ONeal Apr 11 '10 at 05:37
  • I actually read about this in More Effective C++(pg 104). Scenario C is also recommended there. It is suggested that the compiler would optimize scenario C and "construct the object defined by the return expression inside the memory allocated for l_Foo", thereby reducing the total cost of temporaries to 0. I don't know how the compiler would do that, but I take the author's word for now. My question then is, when we have big objects, why is Scenario C then not recommended ? I was always told that it involves creating/deleting temporaries and thus recommended to use B. – brainydexter Apr 11 '10 at 05:43
  • @brainydexter: Any recommendations to use B are probably people who were using compilers without RVO/NRVO. There was a time when it was not a common optimization. However, pretty much every compiler made in the last decade implements RVO/NRVO, so you should be fine. – Billy ONeal Apr 11 '10 at 05:46
  • 1
    Yes, it still applies in that case. The way that RVO works is that the calling code allocates memory for the object, then passes the address of that memory to the function, which then constructs the returned object in that memory. Calling the `add()` function in your example wouldn't interfere with that. One thing that can cause RVO to not work, though, is when the function creates multiple objects and may return any one of them, as shown here: http://en.wikipedia.org/wiki/Return_value_optimization#Compiler_support – Josh Townzen Apr 11 '10 at 06:40

2 Answers2

16

First, let's look into the things that come into play here:

(a) Extending lifetime of a temporary when it's used to initialize a reference - I learnt about it in this publication by Andrei Anexandrescu. Again, it feels weird but useful:

class Foo { ... }

Foo GetFoo() { return Foo(); }  // returning temporary

void UseGetFoo()
{
   Foo const & foo = GetFoo();
   // ... rock'n'roll ...
   foo.StillHere();
}

The rule says that when a reference is initialized with a temporary, the temporary's lifetime is extended until the reference goes out of scope. (this reply quotes the canon)

(b) Return Value Optimization - (wikipedia) - the two copies local --> return value --> local may be omitted under circumstances. That's a surprising rule, as it allows the compiler to change the observable behavior, but useful.

There you have it. C++ - weird but useful.


So looking at your scenarios

Scenario A: you are returning a temporary, and bind it to a reference - the temporary's lifetime is extended to the lifetime of l_Foo.

Note that this wouldn't work if GetFoo would return a reference rather than a temporary.

Scenario B: Works, except that it forces a Construct-Construct-Copy-Cycle (which may be much more expensive than single construct), and the problem you mention about requiring a default constructor.

I wouldn't use that pattern to create a object - only to mutate an existing one.

Scenario C: The copies of temporaries can be omitted by the compiler (as of RVO rule). There is unfortunately no guarantee - but modern compilers do implement RVO.

Rvalue references in C++ 0x allows Foo to implement a resource pilfering constructor that not only guarantees supression of the copies, but comes in handy in other scenarios as well.

(I doubt that there's a compiler that implements rvalue references but not RVO. However there are scenarios where RVO can't kick in.)


A question like this requires mentioning smart pointers, such as shared_ptr and unique_ptr (the latter being a "safe" auto_ptr). They are also in C++ 0x. They provide an alternate pattern for functions creating objects.


Community
  • 1
  • 1
peterchen
  • 40,917
  • 20
  • 104
  • 186
  • @peter: Thanks for such an elaborative response with those links! I really appreciate it. I have been trying to figure this out for quite some time, so am familiar with Scenario A. I had 1 question about Scenario B. I usually use B when I have to modify an object and not when I have to create it inside GetFoo(). Now, assuming that; when we call `Foo l_Foo; someClassPInstance->GetFoo(l_Foo);` :: We send the reference of l_Foo, thus no temporaries here. And then, in `void SomeClass::GetFoo(Foo& a_Foo_ref){ //.. }` lets just say, I modify it and not create it. Do you think then B is a good option – brainydexter Apr 11 '10 at 06:13
  • Also, look at my last comment for original question. – brainydexter Apr 11 '10 at 06:14
  • @brainydexter: Yes, that's a typical use scenario for a reference. It requires some documentation (is it out or in/out etc.). Thanks for the reference, I'll update with a link. --- I've recently toyed around with rvalue references - it was a good exercise for me, too. – peterchen Apr 11 '10 at 06:53
  • `Foo const & f = GetFoo();` should read `Foo const & foo = GetFoo();`, but good answer all the same. – Cory-G Jul 25 '14 at 23:28
4

Out of the three scenarios, number 3 is the ideomatic method, and the one you should probably use. You won't be paying for extra copies because the compiler is free to use copy elision to avoid the copy if possible.

Secnario A is wrong. You end up with a reference to a temporary which is destroyed when the statement containing the function call finishes. Okay, Scenario A is not wrong, but you should still use Scenario C.

Secnario B works just fine, but:

Lets say that Foo cannot have a default constructor. Then how would you deal with that in this situation, since we cant write this anymore: Foo l_Foo.

Foo has to have a default constructor. Even if you don't give it one, the compiler must for you. Assuming you declare a private constructor, there's no way you can use this calling method. You need to either make Foo default constructable or use Secnario C.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • 1
    `const Foo SomeClass::GetFoo()` :: Scenario A :: I am not returning the reference to a temporary here.. – brainydexter Apr 11 '10 at 05:24
  • @brainydexter: Oops.. you are correct. However, I believe it's still wrong. I'm not 100% sure, but I believe the temporary will be destroyed despite having a reference to it. You need to have the actual object around. – Billy ONeal Apr 11 '10 at 05:30
  • 1
    scenario A is fine but really no improvement over C. There is a special rule in C++ that extends the life-time of the temporary object in this case. – sellibitze Apr 11 '10 at 05:33
  • You asked for it :-) I asked this earlier and it is valid. You can read about it here: http://stackoverflow.com/questions/2615162/return-value-not-a-reference-from-the-function-bound-to-a-const-reference-in-t http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/ – brainydexter Apr 11 '10 at 05:34