[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 22:13:20 +0200 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-206-generic; KDE/5.18.0; x86_64; ; ) |
Marc Nieper-Wißkirchen wrote:
> gl_list_iterator_t i = gl_list_iterator (list);
> struct foo *elt;
> while (gl_list_iterator_next (&i, (const void **) &elt, NULL))
> ++elt->bar;
This cast is dangerous: It silences the warning "passing argument 2 of
'gl_list_iterator_next' from incompatible pointer type", and is (AFAICS)
a violation of the C strict-aliasing rule [1].
> So to make my original code portable C, I would have to code
>
> ...
> const void *e;
> while (gl_list_iterator_next (&i, &e, NULL))
> {
> struct foo *foo = (void *) e;
> ++foo->bar;
> }
Right, this is the way it should be written.
> The const typecast is, unfortunately, still needed to silence compiler
> warnings as the Gnulib list API suffers a bit from const-poisoning when the
> actual elements are pointers actually non-const objects.
Yes. The element type could be 'const void *' everywhere or 'void *'
everywhere. Since the generic list code does not write to these pointers,
I chose 'const void *'.
Bruno
[1] https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule
- Re: Type-safe typecasts, (continued)
- Re: Type-safe typecasts, Marc Nieper-Wißkirchen, 2021/04/06
- Re: Type-safe typecasts, Paul Eggert, 2021/04/06
- Re: Type-safe typecasts, Marc Nieper-Wißkirchen, 2021/04/06
- Re: gl_list API, Bruno Haible, 2021/04/06
- Re: gl_list API, Paul Eggert, 2021/04/06
- Re: gl_list API, Bruno Haible, 2021/04/06
- Re: gl_list API, Marc Nieper-Wißkirchen, 2021/04/06
- Re: cast macros, Bruno Haible, 2021/04/06
- Re: cast macros, Ben Pfaff, 2021/04/18
- Re: gl_list API, Ben Pfaff, 2021/04/18
- Re: Type-safe typecasts,
Bruno Haible <=
Re: Type-safe typecasts, Ben Pfaff, 2021/04/06