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

2024-10-26


1532. Explicit instantiation and member templates

Section: 13.9.3  [temp.explicit]     Status: CD3     Submitter: Johannes Schaub     Date: 2012-08-04

[Moved to DR at the April, 2013 meeting.]

According to 13.9.3 [temp.explicit] paragraph 8,

An explicit instantiation that names a class template specialization is also an explicit instantiation 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.

This could be read as an indication that member class templates and member function templates are instantiated (as templates) when their containing class template is instantiated. For example,

  template<typename T> struct A {
    template<typename U> void f() {
      T t;
      t.f();
    }
  };

  template struct A<int>;

In this view, the result would be a member function template definition in class A<int> equivalent to

  template<typename U> void f() {
    int t;
    t.f();
  }

Such a template could never be validly instantiated and thus would presumably fall under the rule in 13.8 [temp.res] paragraph 8,

If no valid specialization can be generated for a template, and that template is not instantiated, the template is ill-formed, no diagnostic required.

The wording of 13.9.3 [temp.explicit] paragraph 1 appears not to allow member templates to be instantiated as templates, however, mentioning only a “member template specialization” as a possibility:

A class, a function or member template specialization can be explicitly instantiated from its template. A member function, member class or static data member of a class template can be explicitly instantiated from the member definition associated with its class template.

This appears to be a contradiction, and although a diagnostic for a member template such as the example above would be helpful, most or all current implementations do not do so. Either the wording of paragraph 1 should be changed to allow explicit instantiation of a member template as a template, analogous to explicitly specializing a member template as a template, or paragraph 8 should be clarified to exclude member templates from the members explicitly instantiated when the containing class template is explicitly instantiated.

Proposed resolution (October, 2012):

Change 13.9.3 [temp.explicit] paragraph 8 as follows:

An explicit instantiation that names a class template specialization is also an explicit instantiation of the same kind (declaration or definition) of each of its members (not including members inherited from base classes and members that are templates) 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]