Тёмный

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

CppCon
Подписаться 150 тыс.
Просмотров 30 тыс.
50% 1

cppcon.org/
---
Back to Basics: The Rule of Five in C++ - Andre Kostur - CppCon 2023
github.com/CppCon/CppCon2023
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
RU-vid Channel Managed by Digital Medium Ltd: events.digital-medium.co.uk
---
Registration for CppCon: cppcon.org/registration/
#cppcon #cppprogramming #cpp

Наука

Опубликовано:

 

28 фев 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 41   
@vasudevans948
@vasudevans948 15 дней назад
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 4 месяца назад
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.
@videofountain
@videofountain 4 месяца назад
Thanks. Useful, Interesting, Calm talk.
@IsaacC20
@IsaacC20 4 месяца назад
I love these B2B videos. And this one is long overdue! Thanks, Andre!
@russCoding
@russCoding 4 месяца назад
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 4 месяца назад
I really like the idea of back to basics section. This talk fits perfectly to help me understand.
@justinlink1616
@justinlink1616 4 месяца назад
Great talk! Very clear and well explained.
@treehouse7962
@treehouse7962 4 месяца назад
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 4 месяца назад
thanks, great talk
@coder2k
@coder2k 4 месяца назад
Great talk, thank you!
@debajyotimajumder472
@debajyotimajumder472 4 месяца назад
Came for entertainment, Found gold. Immediately smashed that like button
@benhetland576
@benhetland576 4 месяца назад
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 4 месяца назад
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.
@ParvezKhanPK
@ParvezKhanPK 4 месяца назад
informative and important cppCon topic.. however, the few of the last QnA wasn't clear without real example/code.
@ujin981
@ujin981 4 месяца назад
the start is at 5:41
@Firestar-rm8df
@Firestar-rm8df 4 месяца назад
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 4 месяца назад
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 4 месяца назад
@@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 2 месяца назад
@@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.
@colinmaharaj50
@colinmaharaj50 4 месяца назад
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
@andrewduncan1217
@andrewduncan1217 4 месяца назад
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 4 месяца назад
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 4 месяца назад
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 4 месяца назад
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
@Ircy2012
@Ircy2012 25 дней назад
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.
@bruderdasisteinschwerermangel
@bruderdasisteinschwerermangel 4 месяца назад
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 4 месяца назад
@19:45-19:49 This sounds incorrect. Can someone verify std::unique_ptr is NOT copyable and IS movable?
@IsaacC20
@IsaacC20 4 месяца назад
@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 4 месяца назад
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 4 месяца назад
@@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 3 месяца назад
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 4 месяца назад
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 4 месяца назад
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 2 месяца назад
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 4 месяца назад
Time to move on from C++ to better, more modern languages. It's had a good run. Time for it to retire.
@AnalogDude_
@AnalogDude_ 4 месяца назад
it's the king of all, others are made with either C or C++ or assembler.
@virno69420
@virno69420 4 месяца назад
Get out of here rust fanboy! Cpp will never die 😤
@Ptiteigne
@Ptiteigne 2 месяца назад
I'm not sure I got what was the issue with the shared_ptr version of Simple String (21:47 - Slide 27)
@andrekostur2683
@andrekostur2683 2 месяца назад
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 4 месяца назад
i use string_view and i dont have problem
@chie5747
@chie5747 3 месяца назад
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.
Далее
Спецэффекты в Симс 4
00:36
Просмотров 279 тыс.
Лайфхак для дачников
00:13
Просмотров 17 тыс.
Master Pointers in C:  10X Your C Coding!
14:12
Просмотров 288 тыс.
WHY did this C++ code FAIL?
38:10
Просмотров 213 тыс.
Back to Basics: Debugging in Cpp - Greg Law - CppCon 2023
1:00:37
Simulating the Evolution of Rock, Paper, Scissors
15:00
ЗАКОПАЛ НОВЫЙ ТЕЛЕФОН!!!🎁😱
0:28
Смело ставь iOS 18
0:57
Просмотров 131 тыс.