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

2024-04-18


1582. Template default arguments and deduction failure

Section: 13.10.3  [temp.deduct]     Status: drafting     Submitter: John Spicer     Date: 2012-10-31

According to 13.10.3 [temp.deduct] paragraph 5,

The resulting substituted and adjusted function type is used as the type of the function template for template argument deduction. If a template argument has not been deduced and its corresponding template parameter has a default argument, the template argument is determined by substituting the template arguments determined for preceding template parameters into the default argument. If the substitution results in an invalid type, as described above, type deduction fails.

This leaves the impression that default arguments are used after deduction failure leaves an argument undeduced. For example,

  template<typename T> struct Wrapper;
  template<typename T = int> void f(Wrapper<T>*);
  void g() {
    f(0);
  }

Deduction fails for T, so presumably int is used. However, some implementations reject this code. It appears that the intent would be better expressed as something like

...If a template argument is used only in a non-deduced context and its corresponding template parameter has a default argument...

Rationale (November, 2013):

CWG felt that this issue should be considered by EWG in a broader context before being resolved.

Additional note, April, 2015:

EWG has requested that CWG resolve this issue along the lines discussed above.

Notes from the May, 2015 meeting:

CWG agreed that a default template argument should only be used if the parameter is not used in a deducible context. See also issue 2092.