composition of declarators

Discussion in 'C++' started by ssailor, Apr 24, 2007.

  1. ssailor

    ssailor Guest

    I saw an exaple in the c++ standard(clause 6.8),

    -------------------------------------------------------
    struct T1 {
    T1 operator ()( int x ) { return T1(x ); }
    int operator =( int x ) { return x; }
    T1(int ) { }
    };
    struct T2 { T2(int ){ } };
    int a , (*(* b)( T2 ))( int ), c , d;
    void ff ()
    {
    T1(a) = 3 ,
    (*(* b)( T2(c )))( int(d )); // AA
    }
    --------------------------------------------

    I am confused that the declaration at the line AA is valid.
    Then, what's the meaning of this declaration? Thank in advance.
     
    ssailor, Apr 24, 2007
    #1
    1. Advertising

  2. ssailor wrote:
    > I saw an exaple in the c++ standard(clause 6.8),
    >
    > -------------------------------------------------------
    > struct T1 {
    > T1 operator ()( int x ) { return T1(x ); }
    > int operator =( int x ) { return x; }
    > T1(int ) { }
    > };
    > struct T2 { T2(int ){ } };
    > int a , (*(* b)( T2 ))( int ), c , d;
    > void ff ()
    > {
    > T1(a) = 3 ,
    > (*(* b)( T2(c )))( int(d )); // AA
    > }
    > --------------------------------------------
    >
    > I am confused that the declaration at the line AA is valid.


    It is.

    > Then, what's the meaning of this declaration? Thank in advance.


    The second part of the declaration declares 'b' to be a pointer
    to a function that takes one argument of type 'int' and returns
    a pointer to a function that takes one argument of type T2 and
    returns T1. 'c' and 'd' are names of the formal arguments and
    do not add meaning to the declaration. The fact that they are
    in parentheses does not matter.

    In the example in the subclause 6.8 of the Standard the
    declaration of 'b' is preceded by another declaration of 'T2'
    which masks the type name, thus making parsing of the 'b'
    declaration improper, and the code ill-formed. That's at least
    AIUI. No diagnostic is required, though. Online trial of
    Comeau compiles either code without a problem.

    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, Apr 24, 2007
    #2
    1. Advertising

  3. ssailor

    ssailor Guest

    Victor Bazarov Wrote:
    > 'c' and 'd' are names of the formal arguments and
    > do not add meaning to the declaration. The fact that they are
    > in parentheses does not matter.

    I took for granted that T2(c ), int(d ) were function-style casts
    instead of function parameter declarations. This is the cause of my
    confusion.

    Thanks again for your clarification.
     
    ssailor, Apr 24, 2007
    #3
  4. "Victor Bazarov" <> wrote in message
    news:...
    > ssailor wrote:
    >> I saw an exaple in the c++ standard(clause 6.8),
    >>
    >> -------------------------------------------------------
    >> struct T1 {
    >> T1 operator ()( int x ) { return T1(x ); }
    >> int operator =( int x ) { return x; }
    >> T1(int ) { }
    >> };
    >> struct T2 { T2(int ){ } };
    >> int a , (*(* b)( T2 ))( int ), c , d;
    >> void ff ()
    >> {
    >> T1(a) = 3 ,
    >> (*(* b)( T2(c )))( int(d )); // AA
    >> }
    >> --------------------------------------------
    >>
    >> I am confused that the declaration at the line AA is valid.

    >
    > It is.
    >
    >> Then, what's the meaning of this declaration? Thank in advance.

    >
    > The second part of the declaration declares 'b' to be a pointer
    > to a function that takes one argument of type 'int' and returns
    > a pointer to a function that takes one argument of type T2 and
    > returns T1.


    Actually, it is the other way around: it is a function pointer taking a T2
    and returning a pointer to a function taking an int and returning a T1.

    The funny thing is, if you replace the comma on the previous line with a
    semi-colon, it actually becomes a double function call: first on the
    function pointed to by the earlier defined 'b', passing a T2 constructed out
    of c, and another function call on the function returned by b(), passing an
    int constructed out of d)

    - Sylvester Hesp
     
    Sylvester Hesp, Apr 24, 2007
    #4
  5. Sylvester Hesp wrote:
    > "Victor Bazarov" <> wrote in message
    > news:...
    >> ssailor wrote:
    >>> I saw an exaple in the c++ standard(clause 6.8),
    >>>
    >>> -------------------------------------------------------
    >>> struct T1 {
    >>> T1 operator ()( int x ) { return T1(x ); }
    >>> int operator =( int x ) { return x; }
    >>> T1(int ) { }
    >>> };
    >>> struct T2 { T2(int ){ } };
    >>> int a , (*(* b)( T2 ))( int ), c , d;
    >>> void ff ()
    >>> {
    >>> T1(a) = 3 ,
    >>> (*(* b)( T2(c )))( int(d )); // AA
    >>> }
    >>> --------------------------------------------
    >>>
    >>> I am confused that the declaration at the line AA is valid.

    >>
    >> It is.
    >>
    >>> Then, what's the meaning of this declaration? Thank in advance.

    >>
    >> The second part of the declaration declares 'b' to be a pointer
    >> to a function that takes one argument of type 'int' and returns
    >> a pointer to a function that takes one argument of type T2 and
    >> returns T1.

    >
    > Actually, it is the other way around: it is a function pointer taking
    > a T2 and returning a pointer to a function taking an int and
    > returning a T1.


    Right. My mistake.

    >
    > The funny thing is, if you replace the comma on the previous line
    > with a semi-colon, it actually becomes a double function call: first
    > on the function pointed to by the earlier defined 'b', passing a T2
    > constructed out of c, and another function call on the function
    > returned by b(), passing an int constructed out of d)


    The funnier thing is, if you drop 'T1' from the line above, the
    entire statement becomes executable, assigning 3 to 'a' and then
    calling 'b' and the other function as you described.

    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, Apr 24, 2007
    #5
    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. John Schutkeker

    Need Composition Editor Tutorial

    John Schutkeker, Oct 17, 2003, in forum: Java
    Replies:
    0
    Views:
    334
    John Schutkeker
    Oct 17, 2003
  2. Ayaz Ahmed Khan

    Variables as array size declarators.

    Ayaz Ahmed Khan, Feb 26, 2004, in forum: C++
    Replies:
    8
    Views:
    1,167
    Ayaz Ahmed Khan
    Feb 29, 2004
  3. chandanlinster

    Are declarators optional?

    chandanlinster, Feb 1, 2007, in forum: C Programming
    Replies:
    2
    Views:
    330
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Feb 1, 2007
  4. array declarators

    , Sep 23, 2007, in forum: C Programming
    Replies:
    5
    Views:
    378
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F
    Sep 24, 2007
  5. Francis Moreau

    Question about array declarators

    Francis Moreau, Jun 16, 2009, in forum: C Programming
    Replies:
    11
    Views:
    716
    Francis Moreau
    Jun 23, 2009
Loading...

Share This Page