rkldabs@[EMAIL PROTECTED]
wrote:
> Hi,
>
> $4.3 talks about conversion of functions to pointers, specifically "An
> lvalue of function type T...". My idea of "function type" is based on
> the following article from Herb "http://www.ddj.com/cpp/184401824".
>
> I wrote the following small piece of code.
>
>
> // CODE SNIPPET 1
> int *fn(int *p, const char& rc){return (int *)0;}
>
> typedef int* MYFUNCTIONTYPE(int *, const char&);
>
> void invoke(MYFUNCTIONTYPE t){
> (t)(0, 'A');
> }
>
> void invoke(MYFUNCTIONTYPE *t){
> (*t)(0, 'A');
> }
>
> int main(){
> invoke(fn);
> }
>
> This code is ill-formed because the two versions of invoke are not
> sufficiently different to be overloaded as complained by VS2008 and
> Comeau.
In fact, the two types are precisely identical. A function type is
always a pointer to a function.
> However a slight modification to use typedef of function pointer,
> makes the code perfectly well-formed as expected with the call
> resolved to the first version of invoke.
>
>
> // CODE SNIPPET 2
> int *fn(int *p, const char& rc){return (int *)0;}
>
> typedef int* (*MYFUNCTIONTYPE)(int *, const char&); // Changed
> to typedef of Function Pointer Type
>
> void invoke(MYFUNCTIONTYPE t){
> (t)(0, 'A');
> }
>
> void invoke(MYFUNCTIONTYPE *t){
> (*t)(0, 'A');
> }
The second invoke is actually doing something different.It doesn't take
a function pointer argument, but a pointer to a function pointer
argument, i.e. two indirections instead of one. There is no "no
indirection" version of function types, though you may simply strip the
"*" for convenience. (I.e. C doesn't copy code around if you pass a
function as argument to another function, it always only provides the
pointer to the first byte of the code, so to say. That's why (*f)(..)
and f(xx) are identical for a function type).
> a) Why is the first piece of code giving error?
Because you define the same function (namely invoke) twice.
> b) $4.3- Does this section talk about the "function type" as in "CODE
> SNIPPET 1" or as a more generic term and as interpreted in "CODE
> SNIPPET 2"?
SNIPPED 1 is a function type, SNIPPED 2, first function, too, second
function is a pointer to a function type (and a pointer to a pointer to
the actual code called, i.e. f will contain the address of a memory cell
which will contain the address of a function, whereas you only have one
indirection in all other cases).
Greetings,
Thomas
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|