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

4

u/Serious-Regular Apr 24 '26

I don't want the call to be known.

what does this even mean? known to whom? the user/developer or the compiler? you're saying you want an abstract interface fine but what kind of take is it that you intentionally prefer that the compiler wouldn't know....?

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

-5

u/[deleted] Apr 24 '26

[removed] — view removed comment

15

u/leguminousCultivator Apr 24 '26

This is an insane take. The overwhelming majority of c++ users are not swapping out virtual functions at runtime.

4

u/Serious-Regular Apr 24 '26

You can compile new code while the current program is already running, load it, hook it in, and viola new behavior.

lol my dude this is literally the expression problem and virtual absolutely does not solve it.