cpp_weekly icon indicating copy to clipboard operation
cpp_weekly copied to clipboard

What is type erasure?

Open lefticus opened this issue 3 years ago • 2 comments

lefticus avatar Jun 23 '22 00:06 lefticus

Notes for this episode: https://compiler-explorer.com/z/9W9r5f77o

Also to mention is that there are many blog posts that go into much more detail here.

lefticus avatar Aug 29 '22 21:08 lefticus

Note that if you swap the order of "object" and "speak" functions, then you accidentally pessimize the binary because the pointer to the object being called is not in the correct register, so the compiler has to generate code to swap registers around. See this example with the two swapped into the inefficient ordering: https://compiler-explorer.com/z/hrY13xe5M

lefticus avatar Aug 31 '22 20:08 lefticus

Coming up in episode 343.

lefticus avatar Sep 15 '22 19:09 lefticus

This made me immediately remember LLVM's function_ref. It uses this exact technique shown in the video.

red1bluelost avatar Sep 27 '22 00:09 red1bluelost

Yes, definitely originally inspired by function_ref like things, I forgot to mention that. I first heard that was a concept, then refused to look at how it was done, because I wanted to see if I could figure it out on my own.

lefticus avatar Sep 27 '22 02:09 lefticus

Hi @lefticus , first of all I'd like to congratulate you about your amazing channel on YoutTube.

I've started to watch the videos and really got addicted to it :).

I wrote a little project to evaluate 7 different techniques to do polymorphism, all of them from the amazing video from Klaus Iglberger where he points out that the best paradigm is to have no paradigms when coding in C++...

But being a C++ developer from the nineties, so before all the amazing stuff that was added to the language (and here cames your channel again to save me tons of time), I really don't get all the "avoidance" of OO nowadays.

For example, why do all the "type erased" stuff and not a simple and effective interface? You've touched the point of having a class that implements it and you have no idea where it came from and that's the beauty of this paradigm to me.

So, I wrote those same 7 examples from Mr. Klaus to help me get "what's the point" of each technique and besides the "Variant-Visitor" which is much faster, I really see no point in why not to use the OO... Specially in C++ in which we can have multiple inheritance.

Can you please enlighten me (or do a video or a series maybe) about it?

Bigous avatar Oct 03 '22 20:10 Bigous