Hacker News new | ask | show | jobs
by nanolith 907 days ago
One advantage to this approach is that there is less compiler magic going on. I use a similar approach, but I prefer using type safe upcasting or model checked downcasting via inline functions or explicit references to base members, instead of direct C style casting.

This also makes it easier to develop a uniform resource management strategy with allocator abstraction. Being able to easily switch between tuned bucket, pool, or bump allocation strategies can do wonders for optimization.

It's possible to model check that downcasting is done correctly, by adding support for performing type checks at analysis time. In this case, a type variable is added to the base type that can be compared before casting. Since this is an analysis only variable, it can be wrapped in a macro so that it is eliminated during normal compilation. Static assertions checked by the model checker during analysis time may need to be refactored to extract the type information as a proof obligation made by the caller. This technique actually works quite well using open source model checkers like CBMC.

Either way, some C OOP is not only useful to provide some optimization knobs, but it's also quite useful for introducing annotations that can help to formally verify C but that don't actually incur any runtime overhead.

1 comments

The biggest advantage of C-style polymorphism vs, say, C++ is that it actually offers much better encapsulation.

Having private methods declared in the header file which is supposed to be the public contract for the class is such an anti-pattern. And the usual solutions offered for this problem are ugly in their own right (*pImpl).

I only properly learnt to appreciate the power and beauty of OOP by reading people’s C code.

Which C++ modules fix.

Plus that same C pattern compiles in C++ without problems.

C++ modules sound incredibly hard to use.
Yeah, for GCC users, or the folks stuck in compilers outside the big three, still catching up to C++17.

VC++ and clang 17 mostly works now.