Is sizeof (void ()) a legal expression?

From [5.3.3 / 1], I found that:

The sizeof operator does not apply to an expression that has a function or partial type

From [3.9 / 5] I found that:

Incomplete object types and cv void are incomplete types

In any case, for sizeof does not evaluate its operands, I would say that sizeof(void()) is a legal expression (in fact, GCC compiles it, and the result is 1). On the other hand, from here , void not mentioned when discussing sizeof , neither when the types with size 1 are mentioned, nor in the list that have a specific implementation size.

The question arises: is there sizeof(void()) legal expression?
Is the size guaranteed to be 1?
Or is it a legal expression leading to UB and that is all?

+49
c ++ language-lawyer sizeof void
Sep 01 '16 at 18:05
source share
6 answers

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

+56
01 Sep '16 at 18:12
source share

From a glance at CppReference.com - the operator sizeof documentation literally reads:

sizeof cannot be used with function types , incomplete types, or bit fields.

And since void() is a type of function, sizeof(void()) not a legal expression.

In our usage example, we can see the error comment on this line:

 std::cout << "size of function: " << sizeof(void()) << '\n'; // error 
+39
Sep 01 '16 at 18:12
source share

In addition, if you compile the code, for example, the example below:

 #include <iostream> int main() { std::cout << sizeof(void()); } 

The code compiles correctly and produces a value of 1, but if you look at the compilation, you will see the following:

main.cpp: In the function 'int main ()':

main.cpp: 5: 29: warning: invalid application of 'sizeof' to function type [-Wpointer-arith]

std :: cout <SizeOf (invalid ());

So, obviously, sizeof() not applicable for function types, so the code generates a warning. This is not true.




Code here


+9
Sep 01 '16 at 18:48
source share

A slight background.

The question arose due to misinterpretation of the sizeof operator.
In fact, the OP considered a void() expression that has an incomplete type in the context of sizeof , and the question itself can be read as - why sizeof accepts the void() expression, which is an incomplete type and should not be accepted as indicated in the working draft?
That is why [3.9 / 5] is actually mentioned, otherwise it would not make sense.

However, the fact is that the question actually contains two interesting questions:

  • Why sizeof(void()) not legal?
    This is the actual question, both from the name itself.

  • Why sizeof((void())) not legal?
    This is the alleged OP question.

Answers below:

  • void() in sizeof(void()) interpreted as a type of function, and it is poorly formed, as for [5.3.3 / 1] (emphasis mine):

    The sizeof operator should not be applied to an expression with a function or an incomplete type, to the name of such types indicated in brackets , [...]

  • (void()) in sizeof((void())) is an expression with an incomplete type void (note that sizeof is an invaluable context), and it is poorly formed as [5.3.3 / 1] (emphasized by me):

    The sizeof operator should not be applied to an expression that has a function or an incomplete type , to a name in brackets of such types, [...]

In both cases, GCC compiles the warning code.

+5
Sep 08 '16 at 12:18
source share

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 gl-values โ€‹โ€‹of a bit field.

Since void() is a function type, therefore its sizeof() not valid

Note:

void() is a function that takes no arguments and returns nothing

Quoting an example from the docs:

 //<< "size of function: " << sizeof(void()) << '\n' // error 

So, the answers to your questions:

1) No, this is not a legitimate expression.

2) It will display as 1, but will show a warning

3) the same as 1).

+1
Sep 07 '16 at 18:35
source share

Straigth from link C99 NO

Section 6.5.3.4 of the sizeof statement specified in the document:

The sizeof operator should not be applied to an expression that has a function type or an incomplete type, to a name in brackets of that type, or to an expression that denotes a member of a bit field.

In accordance with paragraphs 19 and 20 of section 6.2.5 Types :

  1. The void type contains an empty set of values; it is an incomplete type that cannot be completed.
  2. ... A function type describes a function with the specified return type. The type of a function is characterized by the return type, number, and types of its parameters. A function type is called a derivative of its return type, and if its return type is T, the function type is sometimes called a "function" that returns T. A function type construct from a return type is called a "derived function type".

So void () is a type of function derived from an incomplete type, and its illegal as an operand for sizeof. However, its profitability will depend on the implementation of the compiler and does not have a guaranteed return.

C99 standard

+1
Sep 11 '16 at 10:09
source share



All Articles