On 08/05/2026 09:48, Arsen Arsenović wrote:
Martin Uecker <[email protected]> writes:
I am not convinced that moving to C++ actually makes code more
type safe or otherwise better.
It certainly does, by providing a way to implement type-safe containers
for instance, and by providing generic mechanisms of initialization and
cleanup.
There are many implementations of type-safe containers in C (including
Martin's). Whether those solutions would meet your expectations is
another question.
It's tempting to think of type-safe vs. non-type-safe as a binary
choice, but I think there are degrees of type safety. In my opinion, the
de facto standard CONTAINER_OF() macro is type-safe enough to all
intents and purposes.
C++'s templates undoubtedly provide a more powerful facility than the
preprocessor, but some of us like being able to inspect and reason about
preprocessed translation units.
The simplest way of implementing type-safe containers in C is to follow
the precedent set by NDEBUG and predefine macros to the value of any
required parameters before including any source files that require
parametric polymorphism. This also offers the easiest and most
straightforward migration path from monomorphic to polymorphic code.
--
Christopher Bazley
Staff Software Engineer, GNU Tools Team.
Arm Ltd, 110 Fulbourn Road, Cambridge, CB1 9NJ, UK.
http://www.arm.com/