Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Foo x (Bar ())

Reply
Thread Tools

Foo x (Bar ())

 
 
Taras_96
Guest
Posts: n/a
 
      10-14-2009
Hi all,

The FAQ at http://www.parashift.com/c++-faq-lit...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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      10-14-2009
On Oct 14, 3:19 am, Taras_96 <(E-Mail Removed)> wrote:

> The FAQ
> athttp://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).


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."

--
James Kanze
 
Reply With Quote
 
 
 
 
Taras_96
Guest
Posts: n/a
 
      10-14-2009
Thanks James
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      10-15-2009

James Kanze wrote in message
news:(E-Mail Removed)...
> On Oct 14, 3:19 am, Taras_96 <(E-Mail Removed)> wrote:


>> 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."


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++.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vector: Foo[5] == ((foo*)Foo) + 5 ? .rhavin grobert C++ 4 09-24-2008 01:57 PM
Using new foo where foo is a new Foo object. blackholebutterfly@gmail.com Javascript 4 12-23-2007 10:45 PM
why functions in modules need 'global foo' for integer foo but not dictionary foo? seberino@spawar.navy.mil Python 3 07-29-2005 12:42 PM
Invoking constructor : Foo(var) and Foo ins(var) Alex Vinokur C++ 4 09-24-2003 04:01 PM
The same assembler code for foo(char&) and foo(char*) Alex Vinokur C++ 7 09-21-2003 04:36 AM



Advertisments