Is a pointer to an array of unknown size incomplete?

后端 未结 2 516
感动是毒
感动是毒 2021-01-06 20:11

3.9/6 N3797:

[...]

The type of a pointer to array of unknown size, or of a type defined by a typedef declaration to be an array of unkno

相关标签:
2条回答
  • 2021-01-06 20:35

    I think this wording is defective. In your code:

     int (*a)[];
    

    the type of a is actually complete. The type of *a is incomplete. It seems to me (as dyp says in comments) that the intent of the quote was to say that there is no way that later in the program, *a will be an expression with complete type.

    Background: some incomplete types can be completed later e.g. as suggested by cdhowie and dyp:

    extern int a[];
    int b = sizeof a;  // error
    int a[10];
    int c = sizeof a;  // OK
    

    However int (*a)[]; cannot be completed later; sizeof *a will always be an error.

    0 讨论(0)
  • 2021-01-06 20:37

    Like C++ statements, English sentences must be interpreted in context. The context of the quoted sentence makes its meaning perfectly clear. The paragraph reads (§3.9 [basic.types]/p6, the sentence you quoted is bolded):

    A class type (such as “class X”) might be incomplete at one point in a translation unit and complete later on; the type “class X” is the same type at both points. The declared type of an array object might be an array of incomplete class type and therefore incomplete; if the class type is completed later on in the translation unit, the array type becomes complete; the array type at those two points is the same type. The declared type of an array object might be an array of unknown size and therefore be incomplete at one point in a translation unit and complete later on; the array types at those two points (“array of unknown bound of T” and “array of N T”) are different types. The type of a pointer to array of unknown size, or of a type defined by a typedef declaration to be an array of unknown size, cannot be completed.

    Read in context, it's clearly saying that a "pointer to array of unknown bound of T" can't be "completed" into a "pointer to array of N T" in the way that an object declared as an "array of unknown bound of T"" can be later defined as an "array of N T"

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