Foo x (Bar ())

T

Taras_96

Hi all,

The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
writes that with the statement:

Foo x (Bar ())

that

"When the compiler sees Foo x(Bar()), it thinks that the Bar() part is
declaring a non-member function that returns a Bar object, so it
thinks you are declaring the existence of a function called x that
returns a Foo and that takes as a single parameter of type "non-member
function that takes nothing and returns a Bar." "

Is this the correct syntax for declaring a function that takes a
function parameter? I thought that the declaration required the
dereference operator:

Foo x( Bar (*) ());

Or is there a difference between a function parameter and a function
pointer parameter (I've never come across a function parameter, and
don't see how it would work).

Taras
 
J

James Kanze

The FAQ
athttp://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
writes that with the statement:
Foo x (Bar ())

"When the compiler sees Foo x(Bar()), it thinks that the Bar()
part is declaring a non-member function that returns a Bar
object, so it thinks you are declaring the existence of a
function called x that returns a Foo and that takes as a
single parameter of type "non-member function that takes
nothing and returns a Bar." "
Is this the correct syntax for declaring a function that takes
a function parameter? I thought that the declaration required
the dereference operator:
Foo x( Bar (*) ());
Or is there a difference between a function parameter and a
function pointer parameter (I've never come across a function
parameter, and don't see how it would work).

This is a language defect C++ inherits from C. The sequence
"Bar ()" (in this context) declares a function, not a pointer to
a function. But according to C (and thus C++), in a
parameter-declaration-clause, "After determining the type of
each parameter, any parameter of type `array of T' or `function
returning T' is adjusted to be `pointer to T' or `pointer to
function returning T,' respectively."
 
J

Joe Smith

James Kanze wrote in message
This is a language defect C++ inherits from C. The sequence
"Bar ()" (in this context) declares a function, not a pointer to
a function. But according to C (and thus C++), in a
parameter-declaration-clause, "After determining the type of
each parameter, any parameter of type `array of T' or `function
returning T' is adjusted to be `pointer to T' or `pointer to
function returning T,' respectively."

I have actually actively used that once, in C code.
I had a typedef that defines the format of what was essentially a callback
function.
I used this both in a second declaration of functions made for the callback,
and also in the parameter of the function that uses the callback.

It went something like:
--------------------
typedef int (callback_t)(int);

int mycallback(int);
//second declaration ensures prototype
//matched callback type definition
callback_t mycallback;

void funcThatUsesCallback(callback_t cback);

--------------------

In that context, it makes reasonable sense. Syntactically, I can pass
mycallback
to the function by name without taking an address. Inside
funcThatUsesCallback, I can use cback by calling it as if it were a real
function name, without needing to dereference it first.

Thus in many contexts, cback can be treated much like a parameter of
function type, rather than a pointer to a function. Obviously, there are a
few places where it differs, but I'm guessing this similarity and type of
use case was actually a quite intentional feature.


The only big problem is that feature results in the most vexing parse of
C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top