CppCon 2018: Jason Turner “Surprises in Object Lifetime”

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


One of the main defining features of C++ is that of well defined object lifetime. We know that when a scope exits any local variables will be properly cleaned up. This is something that we rely upon and use regularly for the management of resources (ie RAII).

However, there are some corners of the language that challenge our understanding of object lifetime. I have been teaching classes on understanding object lifetime for over a year now and some of these places continue to surprise my students and have even surprised me.

We will explore these corners of C++ and ask if they cause enough concern that the language features should not be used, or if they are still worth the price? We will also ponder if these can be considered mistakes in the language or just side effects of a useful feature. We will also explore when and how the compiler and analyzers can warn on these issues.

Jason Turner
Developer, Trainer, Speaker

I'm available for contracting and onsite training.


*-----*
*-----*
Рекомендации по теме
Комментарии
Автор

I love Jason's style. His talks are always super entertaining.

nites
Автор

Jason it's always a pleasure listening to your talks and watching your youtube videos. Thanks for your contributions.

vertigo
Автор

When C++11 hit the scene that rekindled my interest in C++ and I went and wrote a significant amount of C++11 for a special project. But even as new and cooler things are being added to C++11 to get us to C++20, videos such as this one are demoralizing my interest in staying with C++ going forward. I've now watched several Cppcon 2018 videos and nearly everyone of them was something on the perils of C++ if you don't understand it to an arcane level. (Two exceptions was on C++ for Nividia GPUs and a cool one on template engine to parse regular expressions at compile time - and even evaluate them.)

A new book appeared this year The Rust Programming Language. I've been going through it and am seeing that it's possible for a rank and file programmer to actually master the breadth of the Rust language. With C++20, Concepts will, yeah, make it easier on rank and file programmers to consume template classes and compose somewhat simple template classes themselves, but to really master C++ generic programming just ballooned another order of magnitude in breadth complexity (and it was already exceedingly arcane).

All the special case gotchas and surprising behaviors of the C++ language just further demoralize. There are so many special knowledge issues to track and keep up with. A programming language really shouldn't have this much cognitive baggage overhead.

It really does prove the case that their needed to be a fresh start on a new systems programming capable language and thus far Rust is the best candidate for that.

TheSulross
Автор

20:56 Jason, if your audience can’t even ask questions about what you just said it is not the audience fault, it is the instructor’s.
For me a successful lesson is when I have been so clear that everyone understood.

pleiadiblu
Автор

43:45 "that is not the opening curly for main"
That's because that was put at the end of the previous line so it's easily overlooked.
And that's why I've always been in the "put the curly on the next line"-camp. Apart from visual separation of multi-line parameter lists and the function body.

sanjuuyonsai
Автор

I think that vacuous is a term used to describe a data that is destructible or supportive to confined randomization, which are options of the programmer.

tristansantiagovegan
Автор

I like watching these videos and keep coming Empty handed in terms of what I have learned at the end. It is possibly related to the opinion polls which he keeps taking.

mehtaabdhillon
Автор

"If you know who I am and have seen my previous talks, you may want to move to the front"
I would have done that and yelled wrong or right answers because Jason reaction will be fun

childhood
Автор

26:30 On both gcc and clang only aggregate initialization extends lifetime.

Additionally gcc is smart enough to notice this and warns about unused variable only if it does not extend lifetime of object with destructor with side effects.

GrzesiuG
Автор

Const ref lifetime extensions make me uncomfortable. I would rather just get a compiler error if I try to take a reference of an rvalue, imho. The fact that "const T &x = getRValueT();" is valid and compiles without error makes me so uneasy. This feature seems more like a hack to protect silly mistakes where a compiler error (or even just a warning) should have been enforced instead. The destruction of a reference should never trigger a destructor :\

bdafeesh
Автор

I just tried the std::string_view returning function with a const char[] local array.
GCC (pre-12 trunk) now warns against this (with a cryptic error message) __only in O3 mode__. (it prints garbage/nothing when using any other optimization level).
CLang (pre-14 trunk) compiles fine and does print "hello world" __only from O1 and up__, it still prints garbage for O0 (but never warns).
I find it strange that compilers are still not able to detect "local variable use outside function" UB.

Sadiinso
Автор

The auto[]= behavior, non-intuitive but logical. Copy the main object and then make references to its members. One would think a temporary is created, then a copy/move for each member. But that's less efficient.

Автор

57:26 Small typo "Dandling" should be "Dangling"

ruadeil_zabelin
Автор

48:36 If I were to do something with s and get_Holder().s seperately before returning it can't move right? Because you're technically using both objects? I'm I correct in my understanding that it will only do this if it knows it CAN be moved at all (ie. you're not using the moved-from object anymore before returning)?

ruadeil_zabelin
Автор

How did you get the a.out option in compiler explorer ?

jagansai
Автор

Surprises! I understood

Moved-from objects still have to be destroyed!
Even without a named object we have to be thinking about lifetime!
Complex rules allow for the lifetime extension of temporaries that are assigned to references!
Lifetime extension rules apply recursively to member initializers!
std::string is highly optimized, don't underestimate it!
std::initializer_list<> invocations create hidden const arrays!
std::array has 0 constructors, for efficiency!
to be continued

childhood
Автор

I am a fool and don't get why the generated initializer code has a + 2 in it.

henke
Автор

4 years later not much has changed. Jason's talk about Surprises in Object Lifetimes, Herb Sutter's talks on overhauling the language syntax entirely with syntax 2, Nicolai Josuttis's talk about The Nightmare of Initialization, personal experience, and the icing on the cake - the NSA's 2022 official recommendation to strategically migrate away from using C++ entirely - For new projects, I would not pick C++. Rust, Python, C# - I'll even suffer Go or TypeScript over this. For those already in the trenches maintaining existing code you didn't write - I'm so sorry.

kurt