Lifetime of temporaries [duplicate]
Asked Answered
M

2

37

The following code works fine, but why is this correct code? Why is the "c_str()" pointer of the temporary returned by foo() valid? I thought, that this temporary is already destroyed when bar() is entered - but it doesn't seem to be like this. So, now I assume that the temporary returned by foo() will be destroyed after the call to bar() - is this correct? And why?

std::string foo() {
  std::string out = something...;
  return out;
}

void bar( const char* ccp ) {
  // do something with the string..
}

bar( foo().c_str() );
Mesomorph answered 18/11, 2010 at 11:16 Comment(0)
J
51

$12.2/3- "Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception."

The lifetime of the temporary returned by foo() extends until the end of the full expression where it is created i.e. until the end of the function call 'bar'.

EDIT 2:

$1.9/12- "A full-expression is an expression that is not a subexpression of another expression. If a language construct is defined to produce an implicit call of a function, a use of the language construct is considered to be an expression for the purposes of this definition."

Jamille answered 18/11, 2010 at 11:20 Comment(2)
also note that c_str() just returns a temporary pointer. Its lifetime says nothing about the lifetime of the data it points to.Catacaustic
c_str() doesn't return a temporary at all. It returns a rvalue of pointer type. Rvalues of pointer type aren't objects. They don't have lifetime.Duren
S
66

A temporary object is destroyed when the full-expression that lexically contains the rvalue whose evaluation created that temporary object is completely evaluated. Let me demonstrate with ASCII art:

____________________   full-expression ranges from 'b' to last ')'
bar( foo().c_str() );
     ^^^^^          ^
       |            |
     birth       funeral
Salty answered 18/11, 2010 at 11:21 Comment(1)
The metaphorical illustrations inside the ASCII art :) are very illustrative. I really liked themJamille
J
51

$12.2/3- "Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception."

The lifetime of the temporary returned by foo() extends until the end of the full expression where it is created i.e. until the end of the function call 'bar'.

EDIT 2:

$1.9/12- "A full-expression is an expression that is not a subexpression of another expression. If a language construct is defined to produce an implicit call of a function, a use of the language construct is considered to be an expression for the purposes of this definition."

Jamille answered 18/11, 2010 at 11:20 Comment(2)
also note that c_str() just returns a temporary pointer. Its lifetime says nothing about the lifetime of the data it points to.Catacaustic
c_str() doesn't return a temporary at all. It returns a rvalue of pointer type. Rvalues of pointer type aren't objects. They don't have lifetime.Duren

© 2022 - 2024 — McMap. All rights reserved.