A Deep Dive Into C++ Object Lifetimes - Jonathan Müller - C++Now 2024

preview_player
Показать описание
---

A Deep Dive Into C++ Object Lifetimes - Jonathan Müller - C++Now 2024
---

A C++ program manipulate objects, but it is undefined behavior if you attempt to manipulate them while they are not alive.
So let's do a deep dive into object lifetime.

When are objects created and when are they destroyed?
How does temporary lifetime extension come into play and what changed there recently?
What happens when you std::malloc memory and just pretend objects are there without creating anything?
Or worse: You use mmap() to read shared memory.
How do unions interact with constructors, strict aliasing, or the "common initial sequence"?
What when you explicitly call the destructor and later re-use the same storage?
What's the deal with std::launder, std::bit_cast, and std::start_lifetime_as?

We'll answer all of those questions and much more.
We'll do that by looking at the C++ standard, old and new proposals, and compiler optimizations.
---

---

Jonathan Müller

---

C++Now 2025 - 28th April - 2nd May
C++Now is an annual onsite international C++ programming and coding conference held in Aspen, Colarado. For all C++ developers, C++ software engineers and those involved with the C++ language, CppNow provides an indepth and technical content provided by the best and brightest C++ experts of the C++ world.
Video Sponsors: millennium and think-cell
---

---

#boost #cpp #cppprogramming #cplusplus #software development
Рекомендации по теме
Комментарии
Автор

Great talk, I hadn't thought about the concept of pointer provenance before but it makes a lot of things click together now that just seemed like strange rules previously.

N....
Автор

31:51 🤓 this code also needs to verify that (this != &other)

Roibarkan
Автор

44:19 so, if I have a data structure with its values *scattered* across a single allocation, does it make sense do a (complicated) loop to start_lifetime_as for each of the value elements? I don’t mind doing it if the loop(s) is elided anyway, and if it improves the correctness of the code (i doubt the compiler will see into that much logic anyway to make my objects blessed, but ok). I could do a start_lifetime_as_array for the whole allocation but that seems an overkill too because not all memory will be having usable objects.

Great talk!

AlfredoCorrea
Автор

What if the constructor throws an exception in placement new when you reuse a pre-existing variable after calling std::destroy_at? Then the variable is not constructed and the destuctor will be called on a non-constructed storage😱 This isn't an exception-safe practice. Only use placement new on storage where the call to destructor isn't inevitable or the constructor is noexcept.

sqlexp