From: "Marc Nieper-Wißkirchen" <marc.nieper+gnu@gmail.com>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: "Marc Nieper-Wißkirchen" <marc.nieper+gnu@gmail.com>,
"Gnulib bugs" <bug-gnulib@gnu.org>
Subject: Re: Type-safe typecasts
Date: Tue, 6 Apr 2021 21:13:58 +0200 [thread overview]
Message-ID: <CAEYrNrQWvb880a12ghLQBmH0FSRwbve2JS7cxbK_u6kaBgT6eA@mail.gmail.com> (raw)
In-Reply-To: <84aa607b-197b-6e15-ff1d-3bf4a078075c@cs.ucla.edu>
[-- Attachment #1: Type: text/plain, Size: 1303 bytes --]
Hi Paul,
thanks!
By the way, the snippet you gave is not portable C code, as it assumes
> that 'void *' and 'struct foo *' have the same machine representation.
> This is not necessarily true on (admittedly now-rare) machines that have
> different flavors of pointers. I suspect the main problem here is either
> in the calling code, or in the API for gl_list_iterator_next: if it
> returned a possibly-null value of type 'const void *' instead of storing
> the result through a 'const void **' pointer, the calling code wouldn't
> have gotten into this portability mess.
>
gl_list_iterator_next has to return two things: An element (represented by
a const void *) and a boolean value. As elements may be NULL, the boolean
value is really needed. Of course, one could switch the actual return
parameter with the one returned through a pointer but then it wouldn't look
as nice in while or for-loops.
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;
}
...
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.
Marc
[-- Attachment #2: Type: text/html, Size: 2822 bytes --]
next prev parent reply other threads:[~2021-04-06 19:14 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-06 7:18 Type-safe typecasts Marc Nieper-Wißkirchen
2021-04-06 18:59 ` Paul Eggert
2021-04-06 19:13 ` Marc Nieper-Wißkirchen [this message]
2021-04-06 19:20 ` Paul Eggert
2021-04-06 19:34 ` Marc Nieper-Wißkirchen
2021-04-06 20:28 ` gl_list API Bruno Haible
2021-04-06 20:39 ` Paul Eggert
2021-04-06 21:04 ` Bruno Haible
2021-04-06 21:09 ` Marc Nieper-Wißkirchen
2021-04-06 23:03 ` cast macros Bruno Haible
2021-04-18 19:08 ` Ben Pfaff
2021-04-18 19:27 ` gl_list API Ben Pfaff
2021-04-06 20:13 ` Type-safe typecasts Bruno Haible
2021-04-06 19:25 ` Ben Pfaff
2021-04-06 19:54 ` Bruno Haible
2021-04-06 19:56 ` Bruno Haible
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAEYrNrQWvb880a12ghLQBmH0FSRwbve2JS7cxbK_u6kaBgT6eA@mail.gmail.com \
--to=marc.nieper+gnu@gmail.com \
--cc=bug-gnulib@gnu.org \
--cc=eggert@cs.ucla.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).