r/cpp • • Apr 23 '26

Devirtualization and Static Polymorphism

https://david.alvarezrosa.com/posts/devirtualization-and-static-polymorphism/
81 Upvotes

36 comments sorted by

View all comments

Show parent comment

10

u/_Noreturn Apr 24 '26

I don't want it known at runtime, for swapping out types or storing many inside 1 container.

std::vector<std::unique_ptr<SomeCommonBase>> Data;

for(auto& d : Data)
       d->someVirtFunc();

using crtp makes this annoying to do and using a std::variant would make the types hard coded limitting who can extend it.

Also virtual functions help in binary sizes and can hide things using Pimpl