Does const reference prolong the life of a temporary object returned by a temporary object?
Asked Answered
L

3

0

I know that const reference prolongs the life of a temporary locally. Now I am asking myself if this propriety can be extended on a chain of temporary objects, that is, if I can safely define:

std::string const& foo = aBar.getTemporaryObject1().getTemporaryObject2();

My feeling is that, since the the first method aBar.getTemporaryObject1() returns already a temporary object, the propriety doesn't hold for aBar.getTemporaryObject2().

Legging answered 2/5, 2016 at 12:35 Comment(3)
A temporary will be valid during the expression it appears in. The fact that it returns another temporary, doesn't change the semantics. You can still capture it.Jahn
@StoryTeller but this doesn't mean that the first object will also be guaranteed to be valid during the lifespan of the second, right?Legging
No, of course not. If the second object contains a reference to the first, it'll be a dangling reference.Jahn
S
5

The lifetime extension only applies when a reference is directly bound to that temporary.

For example, initializing another reference from that reference does not do another extension.

However, in your code:

std::string const& foo = aBar.getTemporaryObject1().getTemporaryObject2();

You are directly binding foo to the return value of getTemporaryObject2() , assuming that is a function that returns by value. It doesn't make a difference whether this was a member function of another temporary object or whatever. So this code is OK.

The lifetime of the object returned by getTemporaryObject1() is not extended but that doesn't matter (unless getTemporaryObject2's return value contains references or pointers to that object, or something, but since it is apparently a std::string, it couldn't).

Scriptwriter answered 2/5, 2016 at 12:46 Comment(0)
W
3
std::string const& foo = aBar.getTemporaryObject1().getTemporaryObject2();

is valid (TemporaryObject2 is extended but not TemporaryObject1)

std::string const& foo = aBar.getTemporaryObject1().member;

is also valid (TemporaryObject1 is extended).

but

std::string const& foo = aBar.getTemporaryObject1().getReference();

is not valid: lifetime of TemporaryObject1 is not extended.

Weird answered 2/5, 2016 at 13:7 Comment(0)
H
0

The title is misleading. You shouldn't return a reference to local object as stated in the title, but temporary object (return by value).

string & foo1()
{
  string tmp("hello");
  return tmp; 
}

string foo2()
{
  string tmp("hello");
  return tmp; 
}

void foo3()
{
  const string & r1 = foo1(); // crashes later.
  const string & r2 = foo2(); // Ok, object lives in scope of foo3.
}

the second call is nothing else than:

const string & r2 = string("hello");

As long as the functions return by value, the call-stack doesn't matter. The life-time of the last object will be extended to the life-time of the scope of it's reference.

Hatpin answered 2/5, 2016 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.