|
From: | Paul Eggert |
Subject: | Re: Type-safe typecasts |
Date: | Tue, 6 Apr 2021 11:59:57 -0700 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 |
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 the time, the type you're converting to void * is obvious from context. (However, I guess I wouldn't object in code that I don't have to look at myself. :-)
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.
[Prev in Thread] | Current Thread | [Next in Thread] |