Re: Why no generic function pointers?
Ben Bacarisse skrev:
[...]
>The compiler can very much generate the correct call, BUT it's a
>different matter if such a function exist, and this is the job of the
>run-time loader to detect.
>
I was making a narrow technical point: given a generic function
pointer type, a C function call does not have the information required
to check the type and generate the correct calling code. Assume fp is
such a generic pointer. What is the type of function being called
here:
>
fp(12.3);
Ohh..no
we can't do pointer arithmetic on pointer-to-void either:
void foo(void *p)
{
(*(char *)p)++;
}
so I don't see why you assume I somehow suggest that calling a "generic
function pointer" to be meaningful.
Converting a function pointer to some "generic function pointer", would
throw away information, while converting a "generic function pointer" to
a function pointer, would add information.
I don't see why such conversion should be explicit (i.e. require a
cast), other than as providing a comment for humans (that is, if typedef
of the function pointer is used, else not very readable).
Hmm.. another possibly useful thing with a "generic function pointer",
could be to hold functions (of different types) in a jump table:
gen_func_t jmp[3] = {f1, f2, f3};
Anyway, this is rather academic... in ca. 99,99% IRL cases, void* will
work fine as a generic function pointer. :)
--
Tor <bwzcab@wvtqvm. vw | tr i-za-h a-z>
Ben Bacarisse skrev:
Tor Rustad <tor_rustad@hot mail.comwrites:
>
>
>Ben Bacarisse skrev:
>>Just to be clear, I was focusing on the function call. At that point,
>>there is not enough information for the compiler to generate a correct
>>call, at least not in the most general case.
>>there is not enough information for the compiler to generate a correct
>>call, at least not in the most general case.
>different matter if such a function exist, and this is the job of the
>run-time loader to detect.
I was making a narrow technical point: given a generic function
pointer type, a C function call does not have the information required
to check the type and generate the correct calling code. Assume fp is
such a generic pointer. What is the type of function being called
here:
>
fp(12.3);
we can't do pointer arithmetic on pointer-to-void either:
void foo(void *p)
{
(*(char *)p)++;
}
so I don't see why you assume I somehow suggest that calling a "generic
function pointer" to be meaningful.
Converting a function pointer to some "generic function pointer", would
throw away information, while converting a "generic function pointer" to
a function pointer, would add information.
I don't see why such conversion should be explicit (i.e. require a
cast), other than as providing a comment for humans (that is, if typedef
of the function pointer is used, else not very readable).
Hmm.. another possibly useful thing with a "generic function pointer",
could be to hold functions (of different types) in a jump table:
gen_func_t jmp[3] = {f1, f2, f3};
Anyway, this is rather academic... in ca. 99,99% IRL cases, void* will
work fine as a generic function pointer. :)
--
Tor <bwzcab@wvtqvm. vw | tr i-za-h a-z>
Comment