From: Keith.S.Thompson+u@gmail.com   
      
   David Brown writes:   
   > On 02/01/2026 23:18, Chris M. Thomasson wrote:   
   [...]   
   >> typedef void (*generic_func_ptr)(void)   
   >   
   > There is no generic function pointer type equivalent to "void *" - you   
   > are always going to need casts when converting between different   
   > pointer types. And you /really/ need to make sure you convert to   
   > exactly the correct type before calling the pointed-to function. But   
   > the definition you gave here is commonly used when people want to have   
   > generic function pointers.   
      
   Yes, but ...   
      
   There is no generic function pointer type equivalent to void* in the   
   sense that values can be converted to and from the type implicitly.   
   For example:   
      
    int n = 42;   
    void *vp = &n; // implicit conversion   
    double *dp = vp; // implicit conversion, potentially dangerous   
      
   There are no such implicit conversions for function pointer types.   
      
   However, in the sense of a round-trip conversion yielding the   
   original result *all* function pointer types can be treated as   
   generic function pointer types. You just have to use casts to do   
   any conversions.   
      
   It's very common for all pointer types in a given implementation   
   to have the same representation, and for round-trip conversions   
   to safely yield the original value, but it's not guaranteed by   
   the language, and there are implementations where, for example,   
   function pointer types are bigger than object pointer types, or   
   void* is bigger than int*. Certain collections of pointer types   
   are guaranteed by the language to have the same representation:   
      
   - void*, char*, signed char*, unsigned char*   
   - All struct pointer types   
   - All union pointer types   
   - All function pointer types   
      
   Incidentally, if I want a generic function pointer type, I might   
   define it so that it can't be used accidentally without a cast.   
   For example:   
      
    typedef struct dummy__ (generic_function)(void);   
      
   where struct dummy__ is an incomplete type. (Note that I've   
   typedef'ed the function type, not the pointer type.) But that   
   might be considered overkill; treating a void function with   
   no parameters as generic is probably good enough.   
      
   --   
   Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com   
   void Void(void) { Void(); } /* The recursive call of the void */   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|