Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: "Function T...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 9821 of 9909
Post > Topic >>

Re: "Function Types"

by Thomas Richter <thor@[EMAIL PROTECTED] > Jul 23, 2008 at 07:37 PM

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! ]
 




 3 Posts in Topic:
"Function Types"
rkldabs@[EMAIL PROTECTED]  2008-07-23 07:28:18 
Re: "Function Types"
Thomas Richter <thor@[  2008-07-23 19:37:08 
Re: "Function Types"
Jiang <goo.mail01@[EMA  2008-07-24 02:36:57 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Aug 20 13:15:54 CDT 2008.