Is sizeof(void()) a legal expression?

前端 未结 6 1643
暖寄归人
暖寄归人 2020-12-09 07:03

From [5.3.3/1], I found that:

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

Fro

相关标签:
6条回答
  • 2020-12-09 07:50

    From looking at CppReference.com - sizeof operator, the documentation literally states:

    sizeof cannot be used with function types, incomplete types, or bit-field glvalues.

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

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

    std::cout << "size of function: " << sizeof(void()) << '\n'; // error
    
    0 讨论(0)
  • 2020-12-09 07:55

    Straigth from the C99 reference NO

    Stated in the document under section 6.5.3.4 The sizeof operator:

    The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

    According to item 19 and 20 of section 6.2.5 Types:

    1. The void type comprises an empty set of values; it is an incomplete type that cannot be completed.
    2. ...A function type describes a function with specified return type. A function type is characterized by its return type and the number and types of its parameters. A function type is said to be derived from its return type, and if its return type is T, the function type is sometimes called ‘‘function returning T’’. The construction of a function type from a return type is called ‘‘function type derivation’’.

    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

    0 讨论(0)
  • 2020-12-09 08:01

    Also, if you compile the code, such as 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 see this:

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

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

    std::cout << sizeof(void());

    So, it is evident that sizeof() doesn't apply for function types, so the code produces a warning. It is invalid.


    Code here


    0 讨论(0)
  • 2020-12-09 08:05

    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().

    0 讨论(0)
  • 2020-12-09 08:07

    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 of sizeof and the question itself can be read as - why sizeof accept the expression void(), 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() in sizeof(void()) is interpreted as a function type and it is ill-formed as for [5.3.3/1] (emphasis mine):

      The sizeof operator shall not be applied to an expression that has function or incomplete type, to the parenthesized name of such types, [...]

    • (void()) in sizeof((void())) is an expression that has incomplete type void (note that sizeof is an unevaluated context) and it is ill-formed as for [5.3.3/1] (emphasis mine):

      The sizeof operator shall not be applied to an expression that has function or incomplete type, to the parenthesized name of such types, [...]

    In both cases GCC compiles the code with a warning.

    0 讨论(0)
  • 2020-12-09 08:09

    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 of sizeof()

    Note:

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

    Quoting a Example from Docs:

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

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

    0 讨论(0)
提交回复
热议问题