Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DieHertz/11326183 to your computer and use it in GitHub Desktop.
Save DieHertz/11326183 to your computer and use it in GitHub Desktop.
Lifetime of temporary object is extended until the end of scope of reference to subobject.
#include <iostream>
struct Foo {
~Foo() { std::cout << "~Foo()\n"; }
};
struct Bar {
Foo foo;
~Bar() { std::cout << "~Bar()\n"; }
};
int main() {
auto&& foo = Bar{}.foo;
std::cout << "exit\n";
}
@DieHertz
Copy link
Author

In this particular case, the lifetime of temporary Bar{} is prolonged until the scope of foo ends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment