Title
Explicit instantiation and member templates
Status
cd3
Section
13.9.3 [temp.explicit]
Submitter
Johannes Schaub

Created on 2012-08-04.00:00:00 last changed 123 months ago

Messages

Date: 2013-04-15.00:00:00

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

Date: 2012-10-15.00:00:00

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]
Date: 2012-08-04.00:00:00

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.

History
Date User Action Args
2014-03-03 00:00:00adminsetstatus: drwp -> cd3
2013-10-14 00:00:00adminsetstatus: dr -> drwp
2013-05-03 00:00:00adminsetmessages: + msg4430
2013-05-03 00:00:00adminsetstatus: ready -> dr
2012-11-03 00:00:00adminsetmessages: + msg4071
2012-11-03 00:00:00adminsetstatus: open -> ready
2012-08-04 00:00:00admincreate