Advanced C++: Static Polymorphism

preview_player
Показать описание
Everyone knows how to use the Polymorphism with dynamic binding. But dynamic binding has a run-time cost of both time and memory (virtual table). This video demos a polymorphism with static binding, which has the benefits of plymorphism without the cost of virtual table.

Рекомендации по теме
Комментарии
Автор

This is Old but gold! it's so rare to find advanced C++ tutorials these days.

acatisfinetoo
Автор

The beautiful thing about Bo's videos (other than the GREAT content), is that you can learn as much from reading the comments' discussion points... Priceless.

bluehornet
Автор

I saw this video for the first time two years ago. I saw it now and learnt even Awesome job sir

saikirangattu
Автор

Great videos! Some small critiques:
1. About the cost of dynamic poly., the vtable memory and dyn dispatch are negligible overheads. The main cost is in cache thrashing, i.e., the large and unpredictable jumps in execution causes frequent cache misses. In static poly., the calls are either exactly predictable (static) or inlined, which reduces cache misses significantly, often leading to an order of magnitude improvement in performance.
2. CRTP is not a good representative of static poly. because it is just one specific way to use static poly., and by far the least common one, at least for that purpose. CRTP is mostly for setting up boiler-plate code, which is, in some way, the reverse of polymorphism (or at least, if that's the way you've been using polymorphism, you've got it ass-backward). Also, your example is really badly chosen, because CRTP is a really poor choice for this particular case.
3. You forgot to mention the main draw-back of static polymorphism: you cannot make a run-time decision on which "derived class" to use. But, yes, code bloat and increased compilation times is also a big issue.
4. You forgot to mention some important advantages of static polymorphism: polymorphic secondary types, scalable multiple-dispatch, and inlining.
5. The last technique you showed (with Max) is typically referred to as static duck typing or parametric polymorphism, and is one of the other ways to do static polymorphism (and far more common than CRTP). And it would have been nice to mention "concepts", which are the bread and butter of static polymorphism and generic programming, they are to GP what base / interface classes are to OOP.
6. On your remark on TMP, if this is TMP, it's its simplest and most primitive form. It's like saying that "2+2" is differential calculus.

mike
Автор

great job! your teaching skills show

it is good you mentioned TMP,
i would like to see a dedicated video to this topic :-)

alltheway
Автор

Exactly. That is the whole purpose of static binding. It is a simulated polymorphism, not real. As a matter of fact, difficult to debug through debugger is one of biggest criticism for TMP.

BoQianTheProgrammer
Автор

The template function Max should be corrected by coding max = *it under the if test. Then, the return will represent the largest T in the vector v. The variable ret seems to be an orphan.

keithlevan
Автор

Amazing explaination and great way of teaching with appropriate examples..thanks thanks a lot

soulfeelings
Автор

Generic_Parser is a template class, so it cannot be directly instantiated, and it cannot call itself's process_node(). It is a good idea for the base class to be hidden from clients.

BoQianTheProgrammer
Автор

Bo Qian, you are such a humourous, Intelligent and Great C++(Techie)
7:45 lol

linuxguru
Автор

In last example: Generalized static Polymorphsim
small correction, instead of ret = *it; it should be max = *it;

manibatta
Автор

Brilliant 🎉. And very funny. Thanks for the video 👍👌🏼

tupaclives
Автор

Thank you so much. I have been searching for this for months and couldn't find a straightforward explanation without knowing the names of the things I was looking for.

Fleshbits
Автор

Am I blind or is the first example not polymorphism? if you called parse_preorder() from a base class pointer containing a derived class I would have agreed.

hardstuck
Автор

Nothing is free in this world except your Video 7:45 . Got my subscribe for that & that means These video are not free too :D

theonemanshow
Автор

10:10 is it a good idea to add a static_assert to check that type "T" is an arithmetic type? If someone puts a class in there, you cannot know if they have overloaded the comparison operators in a way that would work as intended, here. However, to force only arithmetic types means you disallow those classes that have been properly set up to work with this function.

antiHUMANDesigns
Автор

If a client(main) instantiates an object of the base class Generic_Parser rather than an instance of yours(EmployeeChart_Parser) or another publicly derived child class, will calling parse_preorder result in infinite calling of the base class process_node? Maybe only derived classes and not the base class should be available to clients? Also, should process_node in the base class have an access specifier other than public? Like Cosmin, I would appreciate a TMP video!

keithlevan
Автор

Awesome. Can still Generic_Parser have other virtual functions ? I mean can we achieve both dynamic and static polymorphism based on types of function invoked ?

azadalishah
Автор

Hi,

great video, I'm really enjoying them. Quick question. Don't we have hiding *)?

bidachon
Автор

bo qian I want to ask a question, or anyone. does static_cast also have cost ? If it is, can we say that virtual table implementation costlier than static_cast costs ?

ozancansel