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

2024-12-19


2617. Default template arguments for template members of non-template classes

Section: 13.2  [temp.param]     Status: review     Submitter: Mike Miller     Date: 2022-08-22

Consider:

struct S {
  template<typename> void f();
};

template<typename = int> void S::f() { }   // ok?

There is implementation divergence in the treatment of this example. The relevant wording appears to be 13.2 [temp.param] paragraph 12:

A default template-argument shall not be specified in the template-parameter-lists of the definition of a member of a class template that appears outside of the member's class.

However, the example above deals with a member of an ordinary class, not a class template, but it is not clear why there should be a difference between a member template of a class template and a member template of a non-template class.

Alternatively, it is not clear why the example above should be treated differently from a non-member function template, e.g.,

template<typename> void f();
template<typename = int> void f() { }

which is explicitly permitted.

Proposed resolution:

Change in 13.2 [temp.param] paragraph 10 as follows:

... A default template-argument may be specified in a template declaration. A default template-argument shall not be specified in the template-parameter-lists of the definition of a member of a class template templated class C that appears outside of the member's class class-specifier of C. A default template-argument shall not be specified in a friend class template declaration. If a friend function template declaration D specifies a default template-argument, that declaration shall be a definition and there shall be no other declaration of the function template which is reachable from D or from which D is reachable.