int (*&)()

Discussion in 'C++' started by Asif Zaidi, Feb 8, 2010.

  1. Asif Zaidi

    Asif Zaidi Guest

    What does the syntax in subject line mean - how should I read it.


    Thanks

    Asif
    Asif Zaidi, Feb 8, 2010
    #1
    1. Advertising

  2. Asif Zaidi

    Syron Guest

    Am 08.02.2010 04:45, schrieb Asif Zaidi:
    > What does the syntax in subject line mean - how should I read it.
    >
    >
    > Thanks
    >
    > Asif


    if this is a typedef (like 'int(*&func)()') it is a reference to a
    pointer to a function that returns an int.

    Kind regards, Syron
    Syron, Feb 8, 2010
    #2
    1. Advertising

  3. Asif Zaidi

    Asif Zaidi Guest

    On Feb 8, 12:28 am, Syron <> wrote:
    > Am 08.02.2010 04:45, schrieb Asif Zaidi:
    >
    > > What does the syntax in subject line mean - how should I read it.

    >
    > > Thanks

    >
    > > Asif

    >
    > if this is a typedef (like 'int(*&func)()') it is a reference to a
    > pointer to a function that returns an int.
    >
    > Kind regards, Syron




    Thanks

    Asif
    Asif Zaidi, Feb 8, 2010
    #3
  4. Asif Zaidi

    Asif Zaidi Guest

    On Feb 8, 12:28 am, Syron <> wrote:
    > Am 08.02.2010 04:45, schrieb Asif Zaidi:
    >
    > > What does the syntax in subject line mean - how should I read it.

    >
    > > Thanks

    >
    > > Asif

    >
    > if this is a typedef (like 'int(*&func)()') it is a reference to a
    > pointer to a function that returns an int.
    >
    > Kind regards, Syron



    I am trying to implement the following and getting compile failure.
    Any suggestions ?

    typedef int (*& func_ptr1)();

    int goo()
    {
    cout << "un goo" << endl;
    return 0;
    }


    int main()
    {

    func_ptr1 p1 = goo;
    }

    The error is below

    1>d:\profiles\waz003\my documents\visual studio 2008\projects
    \hw5_3\hw5_3\hw5_3.cpp(174) : error C2440: 'initializing' : cannot
    convert from 'int (__cdecl *)(void)' to 'func_ptr1'
    Asif Zaidi, Feb 8, 2010
    #4
  5. Asif Zaidi wrote:
    > On Feb 8, 12:28 am, Syron <> wrote:
    >> Am 08.02.2010 04:45, schrieb Asif Zaidi:
    >>
    >>> What does the syntax in subject line mean - how should I read it.
    >>> Thanks
    >>> Asif

    >> if this is a typedef (like 'int(*&func)()') it is a reference to a
    >> pointer to a function that returns an int.
    >>
    >> Kind regards, Syron

    >
    >
    > I am trying to implement the following and getting compile failure.
    > Any suggestions ?
    >
    > typedef int (*& func_ptr1)();
    >
    > int goo()
    > {
    > cout << "un goo" << endl;
    > return 0;
    > }
    >
    >
    > int main()
    > {
    >
    > func_ptr1 p1 = goo;
    > }
    >
    > The error is below
    >
    > 1>d:\profiles\waz003\my documents\visual studio 2008\projects
    > \hw5_3\hw5_3\hw5_3.cpp(174) : error C2440: 'initializing' : cannot
    > convert from 'int (__cdecl *)(void)' to 'func_ptr1'
    >
    >


    I believe the reference to non-const requires a modifiable l-value for
    initialisation. Your 'goo' is not an lvalue. You can either do

    typedef int (* const & func_ptr_ref)();

    ...

    func_ptr_ref pr = goo;

    or define a variable of the type 'pointer to function' and initialise
    your reference with it:

    ...
    int main()
    {
    int (*some_fptr)();
    func_ptr1 p1 = some_fptr;
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 8, 2010
    #5
  6. Asif Zaidi

    Asif Zaidi Guest

    On Feb 8, 7:12 am, Victor Bazarov <> wrote:
    > Asif Zaidi wrote:
    > > On Feb 8, 12:28 am, Syron <> wrote:
    > >> Am 08.02.2010 04:45, schrieb Asif Zaidi:

    >
    > >>> What does the syntax in subject line mean - how should I read it.
    > >>> Thanks
    > >>> Asif
    > >> if this is a typedef (like 'int(*&func)()') it is a reference to a
    > >> pointer to a function that returns an int.

    >
    > >> Kind regards, Syron

    >
    > > I am trying to implement the following and getting compile failure.
    > > Any suggestions  ?

    >
    > > typedef int (*& func_ptr1)();

    >
    > > int goo()
    > > {
    > >     cout << "un goo" << endl;
    > >     return 0;
    > > }

    >
    > > int main()
    > > {

    >
    > >     func_ptr1 p1 = goo;
    > > }

    >
    > > The error is below

    >
    > > 1>d:\profiles\waz003\my documents\visual studio 2008\projects
    > > \hw5_3\hw5_3\hw5_3.cpp(174) : error C2440: 'initializing' : cannot
    > > convert from 'int (__cdecl *)(void)' to 'func_ptr1'

    >
    > I believe the reference to non-const requires a modifiable l-value for
    > initialisation.  Your 'goo' is not an lvalue.  You can either do
    >
    >     typedef int (* const & func_ptr_ref)();
    >
    >     ...
    >
    >         func_ptr_ref pr = goo;
    >
    > or define a variable of the type 'pointer to function' and initialise
    > your reference with it:
    >
    >     ...
    >     int main()
    >     {
    >          int (*some_fptr)();
    >          func_ptr1 p1 = some_fptr;
    >     }
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks Victor - a few questions though

    The first option you state: why does it work. I had the same thing you
    had except I did NOT have the const in the typedef definition. Why
    would the const help ?

    The second option you state: that is just a function ptr is it not. It
    is not a reference to a ptr to a function which is what I want ?



    Thanks

    Asif
    Asif Zaidi, Feb 8, 2010
    #6
  7. Asif Zaidi

    Syron Guest

    Am 08.02.2010 14:40, schrieb Asif Zaidi:
    > On Feb 8, 7:12 am, Victor Bazarov<> wrote:
    > [...]
    >
    > Thanks Victor - a few questions though
    >
    > The first option you state: why does it work. I had the same thing you
    > had except I did NOT have the const in the typedef definition. Why
    > would the const help ?
    >
    > The second option you state: that is just a function ptr is it not. It
    > is not a reference to a ptr to a function which is what I want ?
    >
    >
    >
    > Thanks
    >
    > Asif
    >
    >


    if a function pointer would not be const, you could do some REALLY
    stupid things like "funcPtr++".

    and as I now think about that, a reference to a const pointer does not
    really make sense, does it?
    Syron, Feb 8, 2010
    #7
  8. Asif Zaidi wrote:
    > On Feb 8, 7:12 am, Victor Bazarov <> wrote:
    >> Asif Zaidi wrote:
    >>> On Feb 8, 12:28 am, Syron <> wrote:
    >>>> Am 08.02.2010 04:45, schrieb Asif Zaidi:
    >>>>> What does the syntax in subject line mean - how should I read it.
    >>>>> Thanks
    >>>>> Asif
    >>>> if this is a typedef (like 'int(*&func)()') it is a reference to a
    >>>> pointer to a function that returns an int.
    >>>> Kind regards, Syron
    >>> I am trying to implement the following and getting compile failure.
    >>> Any suggestions ?
    >>> typedef int (*& func_ptr1)();
    >>> int goo()
    >>> {
    >>> cout << "un goo" << endl;
    >>> return 0;
    >>> }
    >>> int main()
    >>> {
    >>> func_ptr1 p1 = goo;
    >>> }
    >>> The error is below
    >>> 1>d:\profiles\waz003\my documents\visual studio 2008\projects
    >>> \hw5_3\hw5_3\hw5_3.cpp(174) : error C2440: 'initializing' : cannot
    >>> convert from 'int (__cdecl *)(void)' to 'func_ptr1'

    >> I believe the reference to non-const requires a modifiable l-value for
    >> initialisation. Your 'goo' is not an lvalue. You can either do
    >>
    >> typedef int (* const & func_ptr_ref)();
    >>
    >> ...
    >>
    >> func_ptr_ref pr = goo;
    >>
    >> or define a variable of the type 'pointer to function' and initialise
    >> your reference with it:
    >>
    >> ...
    >> int main()
    >> {
    >> int (*some_fptr)();
    >> func_ptr1 p1 = some_fptr;
    >> }
    >>
    >> V
    >> [..]


    Please do not quote signatures.

    >
    > Thanks Victor - a few questions though
    >
    > The first option you state: why does it work. I had the same thing you
    > had except I did NOT have the const in the typedef definition. Why
    > would the const help ?


    A reference to const can be initialised with a non-lvalue. That's just
    one of the language rules.

    > The second option you state: that is just a function ptr is it not. It
    > is not a reference to a ptr to a function which is what I want ?


    Not sure I understand your question. 'p1' is your reference to a
    pointer to function. Assign something to 'p1' and you're going to be
    changing 'some_ptr'.

    Now, take the "function" out of all this, perhaps it's what confuses
    you, and imagine that you have just ints. Now, 12345 is an int, isn't
    it? Can you define a reference to an int and initialize it with 12345?
    No, you're not allowed to, because 12345 is not an lvalue.

    int &ri = 12345;

    However, you can define a reference to a const int and initialise it
    with that number:

    int const & rci = 12345;

    (the compiler will create a temporary with that value and make the
    temporary live as long as the reference to it).

    Or you could have a real int (a variable) and initialise a reference
    with it:

    int real_int;

    int& ri = real_int;

    I am not sure this is any clearer, but the principles that govern the
    initialisation of references to objects do apply when those objects are
    pointers to functions.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 8, 2010
    #8
  9. Asif Zaidi

    SG Guest

    Hallo Syron,

    On 8 Feb., 15:34, Syron wrote:
    >
    > if a function pointer would not be const, you could do some REALLY
    > stupid things like "funcPtr++".


    Even for a non-const pointer to a function this is illegal.
    sizeof(some_funtion) also won't work.

    > and as I now think about that, a reference to a const pointer does not
    > really make sense, does it?


    Agreed. I don't think the OP really wants a reference here.

    Cheers,
    SG
    SG, Feb 8, 2010
    #9
  10. Asif Zaidi

    James Kanze Guest

    On Feb 8, 3:45 am, Asif Zaidi <> wrote:
    > What does the syntax in subject line mean - how should I read
    > it.


    According to the rules of type expressions. A reference to a
    pointer to a function taking no arguments and returning int.

    --
    James Kanze
    James Kanze, Feb 8, 2010
    #10
  11. Asif Zaidi

    James Kanze Guest

    On Feb 8, 12:58 pm, Asif Zaidi <> wrote:
    > On Feb 8, 12:28 am, Syron <> wrote:


    > > Am 08.02.2010 04:45, schrieb Asif Zaidi:


    > > > What does the syntax in subject line mean - how should I
    > > > read it.


    > > if this is a typedef (like 'int(*&func)()') it is a
    > > reference to a pointer to a function that returns an int.


    > I am trying to implement the following and getting compile
    > failure. Any suggestions ?


    > typedef int (*& func_ptr1)();


    > int goo()
    > {
    > cout << "un goo" << endl;
    > return 0;
    > }


    > int main()
    > {
    >
    > func_ptr1 p1 = goo;
    > }


    Why? The formal reason why it doesn't work is that you're
    trying to initialize a reference to a non-const with an rvalue.
    But the real question is "what do you think you're trying to
    do?" To make the above work, it's sufficient to change the
    typedef to:

    typedef int (*const & func_ptr1)();

    But while the results may be legal C++, they still don't make
    any logical sense.

    > The error is below


    > 1>d:\profiles\waz003\my documents\visual studio 2008\projects
    > \hw5_3\hw5_3\hw5_3.cpp(174) : error C2440: 'initializing' : cannot
    > convert from 'int (__cdecl *)(void)' to 'func_ptr1'


    The error message could be better, the fact remains: you can't
    initialize a reference to a non-const with an rvalue. Given,
    however, that at least in the above case, it doesn't make any
    sense to even want to, the question remains: what are you really
    trying to do?

    --
    James Kanze
    James Kanze, Feb 8, 2010
    #11
  12. Asif Zaidi

    James Kanze Guest

    On Feb 8, 1:40 pm, Asif Zaidi <> wrote:
    > On Feb 8, 7:12 am, Victor Bazarov <> wrote:


    [...]
    > The first option you state: why does it work. I had the same
    > thing you had except I did NOT have the const in the typedef
    > definition. Why would the const help ?


    Because the standard says so. Basically, when initializing a
    reference, you need an lvalue, unless it is a reference to
    const. (Strictly speaking, Victor was wrong when he said you
    needed a modifiable lvalue: you need an lvalue whose type is not
    const, which isn't quite the same thing. Morally, however, if
    it's not the same thing, then you're cheating somewhere, and
    you're going to have troubles down the road.)

    --
    James Kanze
    James Kanze, Feb 8, 2010
    #12
  13. Asif Zaidi

    James Kanze Guest

    On Feb 8, 2:34 pm, Syron <> wrote:
    > Am 08.02.2010 14:40, schrieb Asif Zaidi:
    > > On Feb 8, 7:12 am, Victor Bazarov<> wrote:
    > > [...]


    > > Thanks Victor - a few questions though


    > > The first option you state: why does it work. I had the same
    > > thing you had except I did NOT have the const in the typedef
    > > definition. Why would the const help ?


    > > The second option you state: that is just a function ptr is
    > > it not. It is not a reference to a ptr to a function which
    > > is what I want ?


    > if a function pointer would not be const, you could do some
    > REALLY stupid things like "funcPtr++".


    The ++ operator isn't supported on function pointers. But
    non-const function pointers do make sense---in fact, why use a
    pointer to a function, instead of calling the function directly,
    if the pointer is const?

    > and as I now think about that, a reference to a const pointer
    > does not really make sense, does it?


    Yes and no. It's generally simpler to use the pointer directly,
    but such references are likely to occur in templates and such.
    If you instantiate:
    std::vector< int (*)() >
    , some of it's functions, like push_back, will take references
    to const pointers.

    --
    James Kanze
    James Kanze, Feb 8, 2010
    #13
  14. Asif Zaidi

    Asif Zaidi Guest

    On Feb 8, 4:59 pm, James Kanze <> wrote:
    > On Feb 8, 3:45 am, Asif Zaidi <> wrote:
    >
    > > What does the syntax in subject line mean - how should I read
    > > it.

    >
    > According to the rules of type expressions.  A reference to a
    > pointer to a function taking no arguments and returning int.
    >
    > --
    > James Kanze


    Thanks for all your replies
    Asif Zaidi, Feb 9, 2010
    #14
    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. Schnoffos
    Replies:
    2
    Views:
    1,188
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,604
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    425
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    927
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    406
    James Kanze
    Apr 17, 2007
Loading...

Share This Page