Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is meant by this?

Status
Not open for further replies.

maluk

Programmer
Oct 12, 2002
79
SG
I found a declaration like this:


Code:
void h(int());

What does this mean? Is that argument a pointer to function or what? If it was intended as a pointer to function then should it be declared like this:


Code:
void h(int (*)());

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Syntactically it's a function prototype (incorrect, of course). May be, it's:
Code:
void h(int[]); // square brackets
?..
 
Nope.

This one I found on the C++ Standards (ANSI ISO/IEC 14882) document. Here's the link here:


It is in the third item of section 3. Here are the complete examples:

Code:
void h(int());
void h(int (*)());              //  redeclaration of  h(int())
void h(int x()) { }             //  definition of  h(int())
void h(int (*x)()) { }          //  ill-formed: redefinition of  h(int())

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
hello? anyone?

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
I thought you already found your answer?
int() is a declaration for a function pointer taking no arguments and returning an int.
 
So int() == int (*)()?

I thought the correct declaration for a pointer to a function is int (*)() rather than int().

Can anyone explain why int () is same as int (*)()? I am confused because int () for me looks like an int declaration that is very different from int(*)().

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
C++ Standard 13.1 said:
- Parameter declarations that differ only in that one is a function type and the other is a pointer to the same function type are equivalent. That is, the function type is adjusted to become a pointer to function type (8.3.5).
Example:
void h(int());
void h(int (*)());
...
As usually, we can miss names on unused parameters in parameter list.
So it's true equivalent. But many of old C++ compilers do not recognize this construct...
How funny!
 
maluk said:
Can anyone explain why int () is same as int (*)()?
I don't know why, but unfortunately, that's what the standard says. There's a lot of things in the standard that make me ask "Why the hell did they do that??" ;-)
 
Haha..I guess I should just accept the fact that int() is equivalent to int (*)().

Thanks guys!

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Actually, they aren't the same.

int() is the type of a function.

int(*)() is the type of a pointer to a function.

When used in a prototype, however, int() is treated as exactly equivalent to int(*)().

It's almost like how int[] gets treated the same as int*.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top