Re: Type-safe typecasts

2021-04-06 Thread Bruno Haible
Marc Nieper-Wißkirchen wrote: > gl_list_iterator_t i = gl_list_iterator (list); > struct foo *elt; > while (gl_list_iterator_next (, (const void **) , NULL)) > ++elt->bar; This cast is dangerous: It silences the warning "passing argument 2 of 'gl_list_iterator_next' from incompatible pointer

Re: Type-safe typecasts

2021-04-06 Thread Bruno Haible
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

Re: Type-safe typecasts

2021-04-06 Thread Bruno Haible
Ben Pfaff wrote: > 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 Other projects may want to use these macros as well. That would make a great

Re: Type-safe typecasts

2021-04-06 Thread Marc Nieper-Wißkirchen
CCing Bruno because of his involvement with the Gnulib list modules. Disallowing NULL list elements could break existing code that actually uses them but returning elements with type void * instead of const void * would be much less incompatible. Code can trivially be ported to such an updated

Re: Type-safe typecasts

2021-04-06 Thread Ben Pfaff
On Tue, Apr 6, 2021 at 12:18 AM Marc Nieper-Wißkirchen wrote: > I have been wondering whether it makes sense to add a small utility trying to > make typecasts in C as type-safe as possible. I've found a few macros for casts useful over years. PSPP uses CONST_CAST and UP_CAST from the file below

Re: Type-safe typecasts

2021-04-06 Thread Paul Eggert
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

Re: Type-safe typecasts

2021-04-06 Thread Marc Nieper-Wißkirchen
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

Re: Type-safe typecasts

2021-04-06 Thread Paul Eggert
On 4/6/21 12:18 AM, Marc Nieper-Wißkirchen wrote: So what I have in mind are macros that do a type conversion from A to B and that signal an error on modern compilers if the input is not of type A. For this, the C11 construct _Generic can be used. Not sure it's worth the aggravation. Most of

Type-safe typecasts

2021-04-06 Thread Marc Nieper-Wißkirchen
Hi, I have been wondering whether it makes sense to add a small utility trying to make typecasts in C as type-safe as possible. The problem is that typecasts are sometimes unavoidable. For an example, let's take a look at the following snippet using Gnulib's xlist module: struct foo { int