[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Type-safe typecasts

From: Bruno Haible
Subject: Re: Type-safe typecasts
Date: Tue, 06 Apr 2021 21:54:04 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-206-generic; KDE/5.18.0; x86_64; ; )

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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]