sizeof with function type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fraser Ross

    sizeof with function type

    class A{};

    void func() {
    sizeof ( A (A()) );
    sizeof ( ( A (A()) ) );
    sizeof ( A ((A())) );
    }

    The first operand is a type-id of a function. It doesn't compile with
    Comeau which is fine.

    The other two are supposed to be making the operands get parsed as
    expressions. The third is the typical way using extra parentheses
    around the first parameter. The second compiles and the third doesn't
    with Comeau.

    I expected the opposite. I think the third might be a bug. I'm not
    sure about the second.


    Fraser.


  • James Kanze

    #2
    Re: sizeof with function type

    On Oct 8, 6:17 pm, "Fraser Ross" <z...@zzzzzz.co mwrote:
    class A{};
     void func() {
    sizeof  ( A (A()) );
    sizeof  ( ( A (A()) ) );
    sizeof  ( A ((A())) );
      }
    The first operand is a type-id of a function.  It doesn't
    compile with Comeau which is fine.
    The other two are supposed to be making the operands get
    parsed as expressions.  The third is the typical way using
    extra parentheses around the first parameter.  The second
    compiles and the third doesn't with Comeau.
    I expected the opposite.  I think the third might be a bug.
    I'm not sure about the second.
    Such declarations are tricky. According to the standard (§8.1),
    "It is possible to identify uniquely the location in the
    abstract-declarator where the identifier would appear if the
    construction were a declarator in a declaration. The named type
    is then the same as the type of the hypothetical identifier."
    So if we consider your type-id's:

    A (A()) -A f(A ()) -A f( A (*)() )
    (A (A())) -??? I can't make this one into a declaration.
    Apparently, Comeau agrees with me. If
    you think it is a type-id, where do you
    put the identifier?
    A ((A())) -A (f(A())) -A (f (A (*)()))

    The last is basically the same as the first, with an extra set
    of parentheses.

    Note that if you are actually writing a declaration, you provide
    the symbol, rather than the compiler putting it anywhere that
    will result in the desired parse. The extra parentheses that
    you cite for the third, for example, work in a declaration
    because there is an identifier, and it is not in the same place
    as it comes out above:

    A f( (A()) ) ;

    If the f is inside the outer parentheses, however, this is a
    legal declaration of a function.

    --
    James Kanze (GABI Software) email:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    • Fraser Ross

      #3
      Re: sizeof with function type

      I see what is happening with the third now, thanks.

      The code was reduced from P25 of Modern C++ Design. There was something
      like my first operand and later it was changed to something like my
      third operand. My second operand is something that works.

      Fraser.


      Comment

      Working...