CppCon 2018: Jason Turner “Surprises in Object Lifetime”
Why Stack Overflow recommends it
Stack Overflow users link it when answering or asking about C++ object lifetime, temporaries and lifetime extension rules.
Recommended by 2 people across 2 different months, 2019 to 2022.
What it is
A CppCon 2018 talk by Jason Turner, a C++ trainer and host of C++Weekly, on corner cases of C++ object lifetime. It covers surprising behaviours such as lifetime extension, and asks whether the language features involved are worth their risks and what compilers and analyzers can catch.
What people said
Recommends Vivick · 2019-03 · Stack Overflow · on “Does std::initializer_list have a copy constructor and is it ever used?”
According to cppreference [LINK] , the only constructor available is the default constructor. This might have to do with how it is usually implemented : a standard C-style array. For additional information, I will suggest this video from Jason Turner at CppCon2018 [this video] that provides insight on lifetime. You can however, initialize a container with an initializer_list effectively treating it as its "copy".
Recommends Rajesh · 2022-03 · Stack Overflow · on “c++ thrift serialize multiple objects to vector<string> only end with the last object result”
Already explained neatly by @Someprogrammerdude, for an in depth treatment of the topic, you might appreciate this cppcon talk, [this video]
Cites user3882729 · 2022-11 · Stack Overflow · on “c++ object lifetime extension rules with member initializers”
In this talk [this video] , the author mentions lifetime extension rules extend to member initializers based on the standard. I see the opposite though, i.e. ~Y is called before "Hello" is printed below. Is the author referring to something else? #include <iostream> using namespace std; struct Y { ~Y() { cout << __PRETTY_FUNCTION__ << "\n"; } Y() { cout << __PRETTY_FUNCTION__ << "\n"; } }; struct X { ~X() { cout << __PRETTY_FUNCTION__ << "\n"; } X(const Y& y) : ref(y) { cout << __PRETTY_FUNCTION__ << "\n"; } const Y &ref; }; Y gety() { return {}; } X getx() { return gety(); } int main() { const X &ref = X{Y{}}; cout << "Hello\n"; } The output is Y::Y() X::X(const Y&) Y::~Y() Hello X::~X() Edit: I