Incomplete class usage in template

前端 未结 2 1873
北恋
北恋 2020-12-01 15:02

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

// Adapted from boost::checked_delete()
templat         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 15:32

    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.

提交回复
热议问题