0

How to store pointer data in vector in case of CRTP

Hi As virtual polymorphism has an overhead (very minor now a days with latest hardware), alternative is CRTP (Curiously Recurring Template Pattern) which only involves static binding. Refer code below: https://www.sololearn.com/compiler-playground/cF9c7x25UT7G It works fine and objective is achieved without virtual. Now my concern is about ptr1 and ptr2 datatype. I could have declared them auto as well. But mostly we do iterate over list or vector of base class pointer and perform same operation (like getrole here) on all pointers in case of virtual keyword, what to be done for CRTP case? In other words, what should be my template type for vector to store ptr1 and ptr2? Feel free to ask for any clarifications.! https://code.sololearn.com/cfl9yL7tYBso/?ref=app https://code.sololearn.com/cF9c7x25UT7G/?ref=app

27th Sep 2023, 7:30 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 Answers
+ 1
you can make a vector of < std::variant > over your types, and then call the interface member function through a generic lambda in a visit statement.
27th Sep 2023, 10:53 AM
MO ELomari
MO ELomari - avatar
0
Thanks. I tried as below and it seems working as expected. Any suggestion is welcome on variant and visit usage as I am very new to it. Does this variant not an overhead compared to earlier normal vector of base pointer ? Was that virtual overhead far more than variant overhead ? Is CRTP not much needed as it does not do much ? https://code.sololearn.com/cfl9yL7tYBso/?ref=app
27th Sep 2023, 1:49 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
you can't have a vector of base pointer because " Employee<QA> " is a completely different type compared to "Employee<Developer> " and you cannot store them in a vector unless you give them a common interface as a base class ( vector is only able to hold one type ). i didn't get your question ( Was that virtual overhead far more than variant overhead ? ) did you mean runtime polymorphism over static polymorphism ?
27th Sep 2023, 5:23 PM
MO ELomari
MO ELomari - avatar
0
Crtp is used to avoid run time polymorphism. Main advantage of run time polymorphism is ability to store all pointers of base type (refering to different derived type objects) into vector or list. that advantage is not possible with only crtp unless we go with variant and visit. Now question is about worth of crtp. It obviously helps to avoid run type overhead by eliminating virtual overhead. But again cost of variant is added. So, is this complex design of CRTP (template classes and variant with visit) is worth or simply to go with run time polymorphism is better choice ? Is there something CRTP provides which I am not able to see? p.s. : static binding is always more compared to virtual overhead. also variant is also an overhead by type check and different mechanism std::visit is handling. so, we saved a lot by going to static and avoiding virtual but a bit we added also by using variant.
27th Sep 2023, 5:31 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
variant has nothing to do with static polymorphism is just a way to dynamically dispatch the incompatible interfaces. here's another approach using <std::any>: https://www.sololearn.com/compiler-playground/c3L303p75IvU https://code.sololearn.com/c3L303p75IvU/?ref=app
27th Sep 2023, 5:51 PM
MO ELomari
MO ELomari - avatar