15

I am very surprised that on various sampled versions of g++, the following compiles without error or warning:

// Adapted from boost::checked_delete()
template <class T> inline void assert_complete()
{
  typedef char type_must_be_complete[ sizeof(T) ? 1 : -1 ];
  (void) sizeof(type_must_be_complete);
}

class X;

void f()
{
  assert_complete<X>();
}

class X {};

int main() {}

If the definition of X is missing or in a different translation unit, I do get errors.

But in the program as above, isn't the definition of f the single instantiation point of my template? And isn't the incompleteness of X at that instantiation point a semantic error?

Does the (C++03 and/or C++11 Draft) Standard call this program well-formed, ill-formed, ill-formed but diagnostic not required, or undefined behavior?

Edit: @David Rodriguez - dribeas reports that clang++, comeau, and Visual Studio 2010 also accept similar code.

1
  • Interestingly when I added an int to the definition of X, sizeof(T) was 4. Templates have been known in the past to have precognitive abilities. Aug 26, 2011 at 20:38

2 Answers 2

12

(I was waiting to Alf Steinbach to post an answer, but since he is not doing it, I will post the reference that he mentioned in the Lounge C++ chat):

The standard indicates that template instantiations are performed after the translation unit has already been translated, so that in time, template instantiations happen after all the non templated elements have already been processed. This is described in section 2.2 Phases of translation:

Paragraphs 1-6 define the preprocessor work and basic textual operations (conversions of the character set, concatenation of literals...)

7/ White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. (2.7). The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.

8/ Translated translation units and instantiation units are combined as follows: Each translated translation unit is examined to produce a list of required instantiations. The definitions of the required templates are located. It is implementation-defined whether the source of the translation units containing these definitions is required to be available. All the required instantiations are performed to produce instantiation units. [ Note: These are similar to translated translation units, but contain no references to uninstantiated templates and no template definitions. — end note ] The program is ill-formed if any instantiation fails.

I have removed some of the notes for brevity. Now the important bit seems to be that the code is translated without triggering template instantiations in one step, and then in a later step the templates are instantiated. This in turn means that if the type is complete anywhere in the translation unit, it will have been processed by the time the compiler gets to the instantiation.

Disclaimer: This seems like a good reason for all of the compilers that I have tried showing the exact same behavior (gcc, clang, comeau, VS 2010), but this only states when in time the instantiation is performed, it does not explicitly state that the type can be incomplete at the point of instantiation of the template.

8
  • spencercw's answer and comment would seem to indicate that it is explicitly allowed elsewhere in the standard. Oct 26, 2011 at 10:49
  • @MatthewWalton: spencercw's answer does not relate to this problem. If instead of a function template it was a non-template, then the code would fail to compile, as ashepler points in his comment to spencercw's answer, the fact that the identifier refers to the same type in all the TU does not mean that there are no restrictions on what you can do or not do before the type definition is encountered (i.e. while still incomplete). Oct 26, 2011 at 11:02
  • The specific question is about why int size1() { return sizeof(X); } will not compile if X is incomplete at this point, while template <typename T> int size2() { return sizeof(T) } will compile even if the type is incomplete at the point of instantiation, and spencercw's answer does not deal with this difference. Oct 26, 2011 at 11:05
  • @DavidRodríguez-dribeas Sir I am still confused with concepts of POI which you taught in some of your other answer (sorry I don't remember exactly in which question you had specified POI), so in fact there is no POI concept exists now? Jan 16, 2016 at 9:34
  • @AngelusMortis: I don't understand the question. Point of Instantiation is part of the language, it has been since templates entered the language and there will be (probably) forever. Jan 18, 2016 at 11:40
6

This line completes the type:

class X {};

So long as the type is completed somewhere in the translation unit then any earlier incomplete instances will be completed.

Here's the relevant section from the standard [basic.types] (3.9 paragraph 7):

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.

2
  • 1
    But there are lots of things you can't do with a not-yet completed type, even if it is completed later in the TU. Try taking the template out of the picture: class X; void f() { (void) sizeof(X); } class X{}; will definitely cause errors.
    – aschepler
    Aug 26, 2011 at 22:06
  • Ah yes, I see your point; looks like I've probably misinterpreted the spec. However, [temp.arg.type] states "a template type argument may be an incomplete type", which I suppose allows it to use the completed type defined later in the translation unit.
    – spencercw
    Aug 26, 2011 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.