cpp_weekly
cpp_weekly copied to clipboard
What is type erasure?
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.
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
Coming up in episode 343.
This made me immediately remember LLVM's function_ref. It uses this exact technique shown in the video.
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.
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?