Why can't we declare a variable of type void?

后端 未结 7 1600
盖世英雄少女心
盖世英雄少女心 2020-12-08 09:43

I\'m looking for a formal explanation of that fact in the Standard. I\'ve found what 3.9.1/9 says and trying to give an explanation used that section.

S

7条回答
  •  有刺的猬
    2020-12-08 10:34

    [edit] The answer below makes valid observations, but they're contradicting. As these might be valuable, I'll not delete them, but see Ben Voight's answer and the comments there for a more straightforward approach.

    Your observations about extern declarations are specifically allowed by 7.1.1/8:

    The name of a declared but undefined class can be used in an extern declaration. Such a declaration can only be used in ways that do not require a complete class type.

    void is not a "declared but undefined class", and there's no other exception in 7.1.1 which applies.

    Additionally, 3.9/5 is fairly explicit that it is in fact allowed:

    A class that has been declared but not defined, an enumeration type in certain contexts (7.2), or an array of unknown size or of incomplete element type, is an incompletely-defined object type. [45] Incompletely defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.

    Emphasis mine. This part of the standard is quite specific about the differences between definitions and declarations, so by omission it specifies that declarations are allowed.

提交回复
热议问题