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?
void()
is a function type (it's a function which takes no arguments and returns nothing), so it's not a valid type insizeof()
.Also, if you compile the code, such as the example below:
The code compiles correctly and produces a value of 1, but if you look at the compilation, you see this:
So, it is evident that
sizeof()
doesn't apply for function types, so the code produces a warning. It is invalid.Code here
A little premise.
The question arose from a misinterpretation of the
sizeof
operator.In fact the OP considered
void()
an expression that has incomplete type in the context ofsizeof
and the question itself can be read as - whysizeof
accept the expressionvoid()
, that is an incomplete type and should not be accepted as mentioned in the working draft?That's why [3.9/5] is mentioned actually, otherwise it wouldn't have made sense.
That said, the fact is that the question contains actually two interesting questions:
Why is
sizeof(void())
not legal?This is the actual question as from the title itself.
Why is
sizeof((void()))
not legal?This is the intended question of the OP.
Answers below:
void()
insizeof(void())
is interpreted as a function type and it is ill-formed as for [5.3.3/1] (emphasis mine):(void())
insizeof((void()))
is an expression that has incomplete typevoid
(note thatsizeof
is an unevaluated context) and it is ill-formed as for [5.3.3/1] (emphasis mine):In both cases GCC compiles the code with a warning.
As already highlighted in the docs here http://en.cppreference.com/w/cpp/language/sizeof
Notes
sizeof()
cannot be used with function types, incomplete types, or bit-field glvalues.Since
void()
is a function type, so its not a valid type ofsizeof()
Note:
void()
is a function that takes no arguments and returns nothingQuoting a Example from Docs:
So Answers to your questions:
1)No it is not a legal Expression.
2)It will show as 1 , but will show a warning
3)same as 1).
From looking at CppReference.com - sizeof operator, the documentation literally states:
And since
void()
is a function type, thensizeof(void())
is not a legal expression.In their usage example, we can see their error comment on this line:
Straigth from the C99 reference NO
Stated in the document under section 6.5.3.4 The sizeof operator:
According to item 19 and 20 of section 6.2.5 Types:
Thus void() is a function type derived from an incomplete type and its illegal as operand for the sizeof. That said its returns will depends on the compiler implementation and does not have any return value guaranteed
C99 Standard