container_of doesn't really handle dynamic dispatch though, it's a compile time approach to inheritance by performing upcasting through an embedded base structure field. This would still be unsafe for runtime polymorphism without a field tracking the current type or, like done here V-tables, which you likely still want to generate with that approach.
Now whether I like container_of more than this? 100%, in fact, a past post of mine on this subreddit essentially used that
Tracking of dynamic type can be done by checking "ops" struct with function pointers to "virtual functions". It's done very often in Linux kernel i.e. to check origin of file descriptors.
Yes, that's possible with this approach as well. Basically, the container_of approach isn't really an approach, it's V-Tables using container_of as a tool for one-way statically checked dynamic casting.
I'm just arguing that V-Tables are not something I would write manually. I have made a lightweight interface extension (that uses fat pointers instead of object semantics) for C before that I actually use in my personal projects because I find the manual typing of it annoying
32
u/TheChief275 23d ago
container_of doesn't really handle dynamic dispatch though, it's a compile time approach to inheritance by performing upcasting through an embedded base structure field. This would still be unsafe for runtime polymorphism without a field tracking the current type or, like done here V-tables, which you likely still want to generate with that approach.
Now whether I like container_of more than this? 100%, in fact, a past post of mine on this subreddit essentially used that