No video

Back to Basics: The Rule of Five in C++ - Andre Kostur - CppCon 2023

  Рет қаралды 30,689

CppCon

CppCon

Күн бұрын

cppcon.org/
---
Back to Basics: The Rule of Five in C++ - Andre Kostur - CppCon 2023
github.com/Cpp...
Designing a class to behave correctly when copied and moved takes a lot of thought. The Core Guidelines provide guidance to streamline that work. In this talk we are going to look at the Core Guideline known as "the Rule of Five", how it came about, and is there anything better.
---
Andre Kostur
Andre Kostur has been a professional C++ developer for nearly 30 years, and was responsible for introducing and championing the use of C++ in his previous company. He is responsible for forming and leading a C++ Users Group as his current company, routinely presenting on all topics regarding C++ ranging from the basics to the latest developments in C++20 and beyond. He has previously been a speaker at CppCon.
---
Videos Filmed & Edited by Bash Films: www.BashFilms.com
KZbin Channel Managed by Digital Medium Ltd: events.digital...
---
Registration for CppCon: cppcon.org/reg...
#cppcon #cppprogramming #cpp

Пікірлер: 42
@vasudevans948
@vasudevans948 Ай бұрын
Thank you so much. Cleared a lot of doubts. Took screen prints of Rule of 5 and the table of special methods provided by compiler vs ones provided by the user. Can't thank you enough. Saved me a lot of reading and going round in circles to nowhere
@stephanebourque490
@stephanebourque490 5 ай бұрын
Great talk! Always nice to get a refresher on the basics. The examples clearly show the usage of the rule. Looking forward to your 2024 talk.
@IsaacC20
@IsaacC20 5 ай бұрын
I love these B2B videos. And this one is long overdue! Thanks, Andre!
@videofountain
@videofountain 5 ай бұрын
Thanks. Useful, Interesting, Calm talk.
@russCoding
@russCoding 5 ай бұрын
Thanks Andre, appreciate your time and effort sharing your knowledge of the C++ language. Whilst I am very familiar with the rules of 3/5/0, it is often nice to challenge and remind oneself of their knowledge and assumptions.
@SafaAndac
@SafaAndac 5 ай бұрын
I really like the idea of back to basics section. This talk fits perfectly to help me understand.
@justinlink1616
@justinlink1616 5 ай бұрын
Great talk! Very clear and well explained.
@treehouse7962
@treehouse7962 5 ай бұрын
37:25 Since C++20, a class with user-declared constructors is considered not to be an aggregate. ("= default" is still user-declared.)
@hkp9257
@hkp9257 5 ай бұрын
thanks, great talk
@coder2k
@coder2k 5 ай бұрын
Great talk, thank you!
@debajyotimajumder472
@debajyotimajumder472 5 ай бұрын
Came for entertainment, Found gold. Immediately smashed that like button
@benhetland576
@benhetland576 5 ай бұрын
The question that soon arises for maintainers of older code soon becomes: If the code was already written correctly complying with the Rule of Three, will it still be safe with the new compiler without any changes? As far as I can understand - based on the chart - it still should be. You just don't get the benefit of move semantics. It can be a bit more fuzzy when inheritance is involved, and maybe one of the base classes has been "upgraded" to the Rule of Five, not to mention when some of the methods may be declared virtual too. Larger codebases often find themselves in such a mixed "transition state" after the passage of time and many programmers have left their mark.
@andrekostur2683
@andrekostur2683 5 ай бұрын
When you move to the new compiler, the presence of the copy operations would cause the move operations to be not declared. So attempts to move would "fall back" to the copies.
@fane7063
@fane7063 9 күн бұрын
For a SString you could get rule of zero by using std::vector
@ParvezKhanPK
@ParvezKhanPK 5 ай бұрын
informative and important cppCon topic.. however, the few of the last QnA wasn't clear without real example/code.
@colinmaharaj50
@colinmaharaj50 5 ай бұрын
6:41 But you see, I will never construct an allocation function in a manner like this to make a leak. Most of my memory allocation will have a simple step to later delete. In any case, I will never have a constructor with routines, without a destructor with the opposing routines new/delete new[]/delete[], open/close etc
@Ircy2012
@Ircy2012 Ай бұрын
I want to learn C++ but the more I learn either from books or elsewhere the more it seems like C++ is a language that wrote itself into a corner of bad decisions and now can't fix them because it has to keep backwards compatibility and people can't switch because nothing else with the same use cases is as developed (or widely used) as it. Like all this would be so much easier and less error prone if the compiler didn't create or remove different things depending on a myriad of factors but just expected you to define what you need and to manually tell it to provide defaults (or mark as intentionally not implemented) for what you didn't.
@andrewduncan1217
@andrewduncan1217 5 ай бұрын
Around 32:00 Andre talks about a class that the developer wants to use the copy constructor or assignment operator when moving and suggests commenting out the move constructor and move assignment operator to get that behaviour. Would it be possible to =default the move constructor and move assignment operator to get the same result?
@andrekostur2683
@andrekostur2683 5 ай бұрын
No. If you =default the moves, then they would be present and thus move operations would call those (compiler-generated) functions instead of the copy operations. And the idea was that one wanted to force everything through the copy operations.
@TinBryn
@TinBryn 5 ай бұрын
I wonder if there is a proposal for some kind of `value_ptr` which is like `unique_ptr` except it can be copied by copying the underlying data to a new allocation, possibly with a custom allocator similar to a custom deleter.
@Dziaji
@Dziaji 5 ай бұрын
I just created one of these the other day. I called it HeapObject. It knows how to copy and move itself, and it supports declaring an inherited class, and it looks to see if a function is defined for copying the inherited type and if not, defaults to using operator=, it also will implicitly cast to T* and explicitly cast to any other type of pointer, and it defines operator* and operator-> I'm also going to do this with a reference type Reference, and a HeapArray, and I'm pretty sure once I do that, every singel class in my code base will fall under the rule of 0
@Firestar-rm8df
@Firestar-rm8df 5 ай бұрын
Would it make sense in implementing sstring to use std::vector as the container type? Implementing Estring to use string feels like a bit of a cop-out answer here... but I'm not sure if std::vector would be enough as I honestly haven't carefully considered it. I would think it would get us close with copy/move semantics though, and it still has access to the pointer through the data() member(or something with a similar name?) iirc.
@andrekostur2683
@andrekostur2683 5 ай бұрын
Would it make sense for SString? No, because SString is supposed to be the example to talk about a "broken" implementation. Could you use it in EString? Sure. std::vector behaves correctly using the rule of 0. Whether either of these would constitute a "good" string class is beyond the scope of the talk.
@Firestar-rm8df
@Firestar-rm8df 5 ай бұрын
@@andrekostur2683 ​ I don't think the point for it was to be broken, just simplified(like std::string but worse) and showing the incremental improvements. EString or extended string was called such as it utilized composition to wrap std::string with extra functionality, rather than working with more primitive base components I thought? Unless I'm misunderstanding here? He also said that he stepped away from simple string to extended string as he didn't find a nice way to represent that. That's what I was asking about when I asked if it made sense to use that(as a nice way to represent that). I think we can both agree that this isn't a 'good' way to write a string class as there is a lot more to them typically, including short string optimizations that we are completely ignoring here, but for the purposes of this talk on the rules of 0, 3, and 5 I was wondering if it would be a nice way to represent the string that handles it correctly. Based on your response though, it sounds like it would behave as expected with the rule of 0 if I'm understanding right? ((I am not familiar with all the nuances of the compiler's auto generated constructor definitions when using std::vector, which is mostly why I'm not sure, but I *think* if I understood this talk right, it should work as expected?)) That was more what I was wondering.
@andrekostur2683
@andrekostur2683 3 ай бұрын
@@Firestar-rm8df Yep, using std::vector as the final refinement to SString to make it work with the rule of 0 would work fine. It knows how to copy, move, and destroy itself well.
@ujin981
@ujin981 5 ай бұрын
the start is at 5:41
@bruderdasisteinschwerermangel
@bruderdasisteinschwerermangel 5 ай бұрын
I feel like the entire special member functions are not that well implemented in C++ considering we get a talk about the rule of 5 every second cppcon
@IsaacC20
@IsaacC20 5 ай бұрын
@19:45-19:49 This sounds incorrect. Can someone verify std::unique_ptr is NOT copyable and IS movable?
@IsaacC20
@IsaacC20 5 ай бұрын
@20:30 This also sounds incorrect. "If you don't provide one [move assignment operator], the compiler will move-assign each of your member variable". Is this behavior simply true?
@andrekostur2683
@andrekostur2683 5 ай бұрын
You are right, I misspoke. The intent of this example was to illustrate the preceeding statement about classes which take away the copy operations. std::unique_ptr is movable, but not copyable. Good catch!
@andrekostur2683
@andrekostur2683 5 ай бұрын
@@IsaacC20 Assuming that one hasn't provided copy operations, or the other move operation, or a destructor, yes. What situation are you concerned about?
@YellowCable
@YellowCable 5 ай бұрын
1. don't use multiple inheritance 2. don't use operator overloading 3. don't use templates 4. don't use lambdas 5. don't use C++ at all.
@stevenbroshar7948
@stevenbroshar7948 5 ай бұрын
It is kinda ASMR which is nice. What is the point to this talk? It very slowly describes how to deal with dynamic memory with lots of repetition. .... I guess the take away is: memory management in c++ is a nightmare.... This is why the Whitehouse says to not use c++ 😉 .... And btw what is the rule of 5? .... Switching example in the middle?! So... You implemented a string using a string class. Huh?
@andrekostur2683
@andrekostur2683 5 ай бұрын
What is the Rule of 5? Slide 20 @15:50. Rule of 5 applies to any case where you do something special in any of the 5 special members. Manual dynamic memory handling is probably one of the easiest examples for the intended audience to generally immediately understand.
@sustrackpointus8613
@sustrackpointus8613 3 ай бұрын
Petition to all c++ programmers: PLEASE FOR THE LOVE OF GOD dye your hair blue and become rust cultist, so the humanity doesn't have to deal with this mess anymore
@matthijshebly
@matthijshebly 5 ай бұрын
Time to move on from C++ to better, more modern languages. It's had a good run. Time for it to retire.
@AnalogDude_
@AnalogDude_ 5 ай бұрын
it's the king of all, others are made with either C or C++ or assembler.
@virno69420
@virno69420 5 ай бұрын
Get out of here rust fanboy! Cpp will never die 😤
@Ptiteigne
@Ptiteigne 3 ай бұрын
I'm not sure I got what was the issue with the shared_ptr version of Simple String (21:47 - Slide 27)
@andrekostur2683
@andrekostur2683 3 ай бұрын
The class' intent on copying is to end up with two separate instances (separate copies of the data) of a string. But if you copy the shared_ptr, what you end up with two strings that are sharing the _same_ data. Which means that when you change one of them, the other one is also "changed" since the two are sharing the data.
@eeeeeeee133
@eeeeeeee133 5 ай бұрын
i use string_view and i dont have problem
@chie5747
@chie5747 4 ай бұрын
The example presented is contrived - we can use std::string. We can't use a string_view variable when we want that variable to own the data as well.
Чёрная ДЫРА 🕳️ | WICSUR #shorts
00:49
Бискас
Рет қаралды 4,2 МЛН
Zombie Boy Saved My Life 💚
00:29
Alan Chikin Chow
Рет қаралды 5 МЛН
Databases are the endgame for data-oriented design
20:31
SpacetimeDB
Рет қаралды 1,6 М.
C++  Tutorial: Rule of 5  [move constructor, move assignment operator]
29:46
Professor Hank Stalica
Рет қаралды 16 М.
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 852 М.
Bjarne Stroustrup: C++ | Lex Fridman Podcast #48
1:47:13
Lex Fridman
Рет қаралды 1 МЛН
Networking in C++
32:50
The Cherno
Рет қаралды 242 М.
Чёрная ДЫРА 🕳️ | WICSUR #shorts
00:49
Бискас
Рет қаралды 4,2 МЛН