Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In both C and C++ a declaration of an object <code>a</code> of incomplete type will match the definition of object <code>a</code> where the type is complete. What you observe simply illustrates the fact that in C++ you are allowed to use incomplete types in non-defining declarations. But once you get to the definition the type must be complete.</p> <p>This behavior is not restricted to arrays. For example, you can declare</p> <pre><code>extern class X x; </code></pre> <p>for a totally unknown class <code>X</code>, and then, when <code>class X</code> is already fully defined, you can define</p> <pre><code>X x; </code></pre> <p>which will match to the above declaration.</p> <p>The same thing happens with your array. Firstly you declare an object of incomplete type</p> <pre><code>extern int a[]; </code></pre> <p>and then you define it with complete type</p> <pre><code>int a[100]; </code></pre> <p>The types here indeed are not matching. However, the C++ language never required them to match. For example, 3.9/7 explicitly states </p> <blockquote> <p>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.</p> </blockquote> <p>It means that <em>the same</em> array object can have incomplete type initially, but acquire a complete type later on. (See also the example in 3.9/7). This doesn't, of course, mean that you can declare <code>a</code> as an <code>int</code> and then define it as a <code>double</code>. The only type-related freedom you have here is, again, to complete an incomplete type. No more.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload