Foo x (Bar ())

Discussion in 'C++' started by Taras_96, Oct 14, 2009.

  1. Taras_96

    Taras_96 Guest

    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
    Taras_96, Oct 14, 2009
    #1
    1. Advertising

  2. Taras_96

    James Kanze Guest

    On Oct 14, 3:19 am, Taras_96 <> 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
    James Kanze, Oct 14, 2009
    #2
    1. Advertising

  3. Taras_96

    Taras_96 Guest

    Thanks James
    Taras_96, Oct 15, 2009
    #3
  4. Taras_96

    Joe Smith Guest

    James Kanze wrote in message
    news:...
    > On Oct 14, 3:19 am, Taras_96 <> 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++.
    Joe Smith, Oct 15, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    383
    John Roth
    Jul 29, 2005
  2. Jonathan Bartlett
    Replies:
    7
    Views:
    452
    Peter Nilsson
    Jul 8, 2005
  3. Wejn
    Replies:
    2
    Views:
    117
    Michal
    Nov 29, 2003
  4. Max Williams
    Replies:
    10
    Views:
    202
    Max Williams
    Dec 15, 2007
  5. Gunnar Hjalmarsson
    Replies:
    12
    Views:
    308
    Darren Dunham
    Feb 24, 2005
Loading...

Share This Page