C++ Insights - Episode 46: Consider making your assignment operators lvalue only

  Рет қаралды 1,066

Andreas Fertig

Andreas Fertig

Күн бұрын

Пікірлер: 13
@garyp.7501
@garyp.7501 3 ай бұрын
That is a good tip if you want to make your assignments always use a reference to the object.
@valentinsiltsenko
@valentinsiltsenko 11 ай бұрын
Great tip. Thanks a lot!
@andreas_fertig
@andreas_fertig 11 ай бұрын
You're welcome!
@TheBuilder
@TheBuilder 11 ай бұрын
I'll think about it
@stanbarvinsky1011
@stanbarvinsky1011 11 ай бұрын
great tip! thank you
@andreas_fertig
@andreas_fertig 11 ай бұрын
My pleasure!
@VincentZalzal
@VincentZalzal 11 ай бұрын
Have you seen any downsides to doing this? Does it prevent some common code idiom? I wonder if this could be made into a general guideline.
@David_Friberg
@David_Friberg 11 ай бұрын
There are some downsides to this, but potentially the benefit holds their weight. See my comment above for details.
@VincentZalzal
@VincentZalzal 11 ай бұрын
@@David_Friberg For some reason, I don't see your comment.
@davidf7196
@davidf7196 11 ай бұрын
@@VincentZalzal Seems KZbin's overzealously censored my comment, and does so even when I try to repost it. Trying from another account. What I wrote was: Note that this technique may unexpectedly break the rule of zero, in the cases where it would otherwise apply. As you declare the copy assignment op (even when explicitly defining it as defaulted) you suppress the implicit declaration of a defaulted move ctor and defaulted a move assignment operator. A mitigation comes with a cost: adding additional boilerplate code to bring us into rule of five, even if all special members are explicitly defined as defaulted. As an additional niche pitfall, it may also make aggregates into non-aggregates from C++20 and onward, as we need only user-declare a ctor to disqualify the class as an aggregate. Whilst this technique makes sense semantically, syntactically it can add a lot of noise, particularly in environments (automotive, safety-critical) where rule guidelines such as AUTOSAR C++14 (rule A12-0-1) will enforce rule of five if you cannot stick to rule of zero. Whilst this seems to have been addressed in MISRA C++:2023, I'm still leaning towards mitigation for to-rvalue assignment being enforced by static analysis (fully decidable) instead of via a language measure that comes with this kind of cost.
@David_Friberg
@David_Friberg 11 ай бұрын
@@VincentZalzal For some reason my other (explanatory) comment keeps getting censored by KZbin, no idea why. Instead, I posted it as a Q&A on r/cpp_questions, hopefully you can find it there.
@VincentZalzal
@VincentZalzal 11 ай бұрын
@@David_FribergFound it, thanks.
C++ Insights - Episode 40: The traps of returning references
9:08
Andreas Fertig
Рет қаралды 1,1 М.
Counter-Strike 2 - Новый кс. Cтарый я
13:10
Marmok
Рет қаралды 2,8 МЛН
Their Boat Engine Fell Off
0:13
Newsflare
Рет қаралды 15 МЛН
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 849 М.
why are switch statements so HECKIN fast?
11:03
Low Level
Рет қаралды 437 М.
C++ Insights - Episode 41: How type-traits work
6:44
Andreas Fertig
Рет қаралды 3,9 М.
The Value of Source Code
17:46
Philomatics
Рет қаралды 216 М.
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 992 М.
Python laid waste to my C++!
17:18
Sheafification of G
Рет қаралды 188 М.
C++ Insights - Episode 42: C++20: When is a struct an aggregate
4:52
Counter-Strike 2 - Новый кс. Cтарый я
13:10
Marmok
Рет қаралды 2,8 МЛН