skypjack skypjack - 3 months ago 14
C++ Question

Is sizeof(void()) a legal expression?

From [5.3.3/1], I found that:


The sizeof operator shall not be applied to an expression that has function or incomplete type


From [3.9/5] I found that:


Incompletely-defined object types and cv void are incomplete types


Anyway, for
sizeof
does not evaluate it's operands, I would have said that
sizeof(void())
was a legal expression (actually GCC compiles it and the result is 1).

On the other side, from here,
void
is not mentioned while discussing
sizeof
, neither when the types having size 1 are mentioned, nor in the list of the ones having an implementation defined size.

The question is thus: is
sizeof(void())
a legal expression?

Is it guaranteed to have size equal to 1?

Or is it a legal expression resulting in an UB and that's all?

Answer

void() is a function type (it's a function which takes no arguments and returns nothing), so it's not a valid type in sizeof().