[Top][All Lists]

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

Re: cast macros

From: Bruno Haible
Subject: Re: cast macros
Date: Wed, 07 Apr 2021 01:03:52 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-206-generic; KDE/5.18.0; x86_64; ; )

I wrote:
> So far we have been lacking type-casts that warn for invalid use;
> it is well possible that with the PSPP macros (or with Marc's approach
> of _Generic), we can design a type-safe wrapper around gl_list.h

Here is a simplified test case: The simplest container type is a box type,
that contain just one value. Can one of you complete this code, so that
it produces the warnings / no warnings as indicated?

typedef const void ** gl_box_t;

extern gl_box_t create_box (const void *);
extern const void *box_get_value (gl_box_t);

#include "cast.h"

   gives a warning if EXPR is not of type FROM,
   and returns EXPR, cast to type TO.  */

struct foo;
#define ELEMENT_TYPE struct foo *
#define CREATE_BOX(V) \
  create_box (CAST_TO_FROM (const void *, ELEMENT_TYPE, V))
#define BOX_GET_VALUE(BOX) \
  CAST_TO_FROM (ELEMENT_TYPE, const void *, box_get_value (BOX))

struct foo *a;
struct foo *b;
const void *c;
void *d;
int *i;

main ()
  (void) CAST_TO_FROM (int *, const void *, c); // no warning
  (void) CAST_TO_FROM (int *, void *, d);       // no warning
  (void) CAST_TO_FROM (int *, const void *, d); // no warning
  (void) CAST_TO_FROM (int *, void *, c);       // warning
  (void) CAST_TO_FROM (int *, void *, i);       // warning
  (void) CAST_TO_FROM (int *, char *, i);       // warning

  gl_box_t box = CREATE_BOX (a);                // no warning
  return BOX_GET_VALUE (box) == b;              // no warning

reply via email to

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