bug-gnulib
[Top][All Lists]
Advanced

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

Re: Type-safe typecasts


From: Paul Eggert
Subject: Re: Type-safe typecasts
Date: Tue, 6 Apr 2021 12:20:08 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1

On 4/6/21 12:13 PM, Marc Nieper-Wißkirchen wrote:
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

Ah, OK, then that's the problem. The API shouldn't allow null elements. They're not that useful anyway. If they really are needed for some lists, I suppose one could have a more-complicated API to return them (by setting a bool elsewhere); but usually they aren't.

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

Yes, that would be portable. But that cast indicates another problem with the API. It should return void *, not void const * (think of strchr as the model here). The API should discourage type-casts, since they're more dangerous than the alternatives.



reply via email to

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