[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

sizeof with function type

Fraser Ross

10/8/2008 4:17:00 PM

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.


2 Answers

James Kanze

10/8/2008 8:40:00 PM

0

On Oct 8, 6:17 pm, "Fraser Ross" <z...@zzzzzz.com> wrote:
> 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.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Fraser Ross

10/9/2008 10:24:00 AM

0

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.