Type Erasure

  Рет қаралды 9,684

Everything Cpp

Everything Cpp

7 жыл бұрын

Type Erasure, a C++ specific design pattern, allows unrelated types to all be used by a single type. This video discusses what this pattern looks like, how it works, and some of the areas in the standard library where you will find it leveraged.
A special thanks to Nevin :-) Liber for teaching me everything I know on the subject.
A note about the video: I should have decayed the type in the assignment operator, or at least removed the reference. See pinned comment, below.

Пікірлер: 20
@ferguswaugh80
@ferguswaugh80 7 жыл бұрын
Hi, I'm a little new to universal references, so I may have some misunderstandings, but I'm fairly sure that the type 'T' in your Model is deduced as T& in the case of feeding the constructor an l-value. E.g. in this code snippet: godbolt.org/g/i12iR2 Is this deliberate? Or would something like this be better? godbolt.org/g/d3kdfx Really liking these videos by the way, covers some interesting C++ topics that I've found difficult to understand elsewhere, thanks for uploading :)
@EverythingCpp
@EverythingCpp 7 жыл бұрын
Ahh, a fine catch! Yes, I should have decayed the type! m_value.reset(new Model(std::forward(value))); is what I should have put. I was careless with the universal reference... Am new to the KZbins, and now evaluating options to fix that...
@EverythingCpp
@EverythingCpp 7 жыл бұрын
Though I believe the example, as is, deduces correctly. But for T&, it would be broken, storing a reference to the original, which is not intended.
@BloodHaZaRd666
@BloodHaZaRd666 4 жыл бұрын
@@EverythingCpp Didn't understand you rpoint of view? Could you explain further plz
@AmirHosseinTamjidi
@AmirHosseinTamjidi 4 жыл бұрын
I cannot emphasize how well prepared and clear this video was. Well done!
@SuhailKhan-vr6ik
@SuhailKhan-vr6ik 3 жыл бұрын
Excellent video. Finally got an understanding of what we are trying to achieve with type erasure and how we do it.
@stephenhowe4107
@stephenhowe4107 3 жыл бұрын
I like what you present. Very helpful. Just for the sake of completeness, you might like to say, why would you want to do type erasure - what are the benefits? What do you gain? And also what do you lose (but the video mentions some of the downsides)? It is important that the programmer knows all of these so they can say to themselves, "yes I am going to use type erasure in the code here" or "because of these downsides and code requirements, I am not going to use type erasure". std::any is reaching towards a C++ version of C's union. I have seen Alexandrescu's discriminated union code (mentioned in Herb Sutter's books). And also, what, if any, are there of alternative ways of implementing type erasure? It is important that various ways of doing C++ patterns are documented, the pluses and minuses of each technique and what problem they attempt to solve.
@ekremvkilic
@ekremvkilic 4 ай бұрын
The return *this in operator overload of assignment is missing. Also, it is better to define Move Constructor for Model struct and move the passed object along the way. Besides that, the video is great. Thanks!
@p.polunin
@p.polunin Жыл бұрын
Thank you very much for such a clear explanation!
@TheTemplarofAnubis
@TheTemplarofAnubis 7 жыл бұрын
Some applications and use case examples would help so much with your videos.
@yomoma6692
@yomoma6692 6 жыл бұрын
Tanks, that helped quite a bit.
@Bbdu75yg
@Bbdu75yg Жыл бұрын
Brilliant ❤ , thank you so much !
@xiaobaibai5871
@xiaobaibai5871 5 жыл бұрын
great idea, thanks
@muhdiversity7409
@muhdiversity7409 9 ай бұрын
This video just proves that C++ jumped the shark long ago. Where most of your effort is fighting C++ syntax/complexity and becoming a language lawyer as opposed to writing simple code that does something useful.
@yyoud88
@yyoud88 5 жыл бұрын
Don't you need to use `std::move(value)` in ctor and move assignment of `TypeErased`?
@MaitreBart
@MaitreBart Жыл бұрын
I think the proper handler would be std::forward().
@yyoud88
@yyoud88 Жыл бұрын
I don’t think so. Search copy-and-move clang tidy.
@R_BNK
@R_BNK 5 жыл бұрын
Very nice explanation...👍
What the heck is type erasure? 🤔
9:15
Vincent Pradeilles
Рет қаралды 4,8 М.
Type Erasure
2:47
Professor Lupoli
Рет қаралды 3,4 М.
Шокирующая Речь Выпускника 😳📽️@CarrolltonTexas
00:43
Глеб Рандалайнен
Рет қаралды 10 МЛН
КАРМАНЧИК 2 СЕЗОН 6 СЕРИЯ
21:57
Inter Production
Рет қаралды 477 М.
Joven bailarín noquea a ladrón de un golpe #nmas #shorts
00:17
WHY DOES SHE HAVE A REWARD? #youtubecreatorawards
00:41
Levsob
Рет қаралды 39 МЛН
Why typename?
7:42
Everything Cpp
Рет қаралды 14 М.
Swift: Associated Types & Type Erasure
33:15
The Dev Method
Рет қаралды 2,5 М.
Павел Новиков - Веселье с type erasure
57:33
C++ User Group
Рет қаралды 1,4 М.
Writing cache friendly C++ - Jonathan Müller - Meeting C++ 2018
54:38
DejaVue #E010 - Design Patterns in Vue.js
29:39
DejaVue
Рет қаралды 427
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 719 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1 МЛН
Golang Tutorial for Beginners | Full Go Course
3:24:59
TechWorld with Nana
Рет қаралды 1,8 МЛН
Type Erasure in Java Generics | Type Erasure and Java Generics
13:52
KK JavaTutorials
Рет қаралды 11 М.
Не обзор DJI Osmo Pocket 3 Creator Combo
1:00
superfirsthero
Рет қаралды 1,3 МЛН
Which Phone Unlock Code Will You Choose? 🤔️
0:14
Game9bit
Рет қаралды 13 МЛН
wireless switch without wires part 6
0:49
DailyTech
Рет қаралды 845 М.
ПРОБЛЕМА МЕХАНИЧЕСКИХ КЛАВИАТУР!🤬
0:59
Корнеич
Рет қаралды 3,6 МЛН
Обзор игрового компьютера Макса 2в1
23:34