Reputation: 8313
Say
a_function(mystruct::create().execute());
create()
creates an instance of struct as mystruct s()
then the method execute()
returns something.
does the struct continue to exist for the entire time the function is executed or is it released?
Upvotes: 2
Views: 118
Reputation: 92261
Unless there are some special conditions for extending the lifetime, any temporaries created in an expression will be destroyed at the end of the full expression. Here this is at the semi-colon.
You are even assured that multiple temporary objects will be destroyed in the reverse order of their creation, as always.
Upvotes: 0
Reputation: 385144
The temporary struct
object exists until the full expression is over. That means until a_function
has been returned from.
See 12.2 in C++03.
Upvotes: 1
Reputation: 132994
You can rest assured that a temporary object will exist as long as the full expression in which it is created hasn't been evaluated, which means your struct will "exist" until the a_function
has returned.
Upvotes: 2