This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-11-11


969. Explicit instantiation declarations of class template specializations

Section: 13.9.3  [temp.explicit]     Status: CD2     Submitter: Jason Merrill     Date: 29 December, 2008

[Voted into WP at March, 2010 meeting.]

Consider this example:

    template <class T> struct A {
       virtual void f() {}
    };

    extern template struct A<int>;

    int main() {
       A<int> a;
       a.f();
    }

The intent is that the explicit instantiation declaration will suppress any compiler-generated machinery such as a virtual function table or typeinfo data in this translation unit, and that because of 13.9.3 [temp.explicit] paragraph 10,

An entity that is the subject of an explicit instantiation declaration and that is also used in the translation unit shall be the subject of an explicit instantiation definition somewhere in the program; otherwise the program is ill-formed, no diagnostic required.

the use of A<int> in declaring a requires an explicit instantiation definition in another translation unit that will provide the requisite compiler-generated data.

The existing wording of 13.9.3 [temp.explicit] does not express this intent clearly enough, however.

Suggested resolution:

  1. Change 13.9.3 [temp.explicit] paragraph 7 as follows:

  2. An explicit instantiation that names a class template specialization is also an explicit instantion of the same kind (declaration or definition) of each of its members (not including members inherited from base classes) that has not been previously explicitly specialized in the translation unit containing the explicit instantiation, except as described below.
  3. Change 13.9.3 [temp.explicit] paragraph 9 as follows:

  4. An explicit instantiation declaration that names a class template specialization has no effect on the class template specialization itself (except for perhaps resulting in its implicit instantiation). Except for inline functions and class template specializations, other explicit instantiation declarations have the effect of suppressing the implicit instantiation of the entity to which they refer...

Proposed resolution (October, 2009):

Change 13.9.3 [temp.explicit] paragraphs 7-9 as follows:

An explicit instantiation that names a class template specialization is also an explicit instantion of the same kind (declaration or definition) of each of its members (not including members inherited from base classes) that has not been previously explicitly specialized in the translation unit containing the explicit instantiation, except as described below. [Note: In addition, it will typically be an explicit instantiation of certain implementation-dependent data about the class. —end note]

An explicit instantiation definition that names a class template specialization explicitly instantiates the class template specialization and is only an explicit instantiation definition of only those members whose definition is visible at the point of instantiation.

An explicit instantiation declaration that names a class template specialization has no effect on the class template specialization itself (except for perhaps resulting in its implicit instantiation). Except for inline functions and class template specializations, other explicit instantiation declarations have the effect of suppressing the implicit instantiation of the entity to which they refer...