Hi Ben,

> I've found a few macros for casts useful over years. PSPP uses
> CONST_CAST and UP_CAST from the file below quite a bit:
> https://git.savannah.gnu.org/cgit/pspp.git/tree/src/libpspp/cast.h

That's pointing to a nice solution to the problem that casts don't warn in C.
I use to write code like

   void *p = ...;
   int *q = (int *) p;

because I want the reader of the program to be aware that there is a cast.

Paul uses to write code like

   void *p = ...;
   int *q = p;

because he wants the compiler to issue warnings if the type of p is changed
to something else.

It would be cool to have the advantages of both approaches:

   void *p = ...;
   int *q = SAFE_CAST (int *, p);

that would indicate that there is a cast AND warn if 'int *' and 'void *'
are not compatible.  Can you implement SAFE_CAST with the techniques from
PSPP, or is Marc's approach needed?

Btw, I like Marc's approach of using _Generic for warning purposes on newer
compilers, and still maintain compatibility with older compilers.

Bruno


Reply via email to