sizeof with function type

F

Fraser Ross

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.
 
J

James Kanze

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.
 
F

Fraser Ross

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top