<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 621</TITLE>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<STYLE TYPE="text/css">
  INS { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  .INS { text-decoration:none; background-color:#D0FFD0 }
  DEL { text-decoration:line-through; background-color:#FFA0A0 }
  .DEL { text-decoration:line-through; background-color: #FFD0D0 }
  @media (prefers-color-scheme: dark) {
    HTML { background-color:#202020; color:#f0f0f0; }
    A { color:#5bc0ff; }
    A:visited { color:#c6a8ff; }
    A:hover, a:focus { color:#afd7ff; }
    INS { background-color:#033a16; color:#aff5b4; }
    .INS { background-color: #033a16; }
    DEL { background-color:#67060c; color:#ffdcd7; }
    .DEL { background-color:#67060c; }
  }
  SPAN.cmnt { font-family:Times; font-style:italic }
</STYLE>
</HEAD>
<BODY>
<P><EM>This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21
  Core Issues List revision 118b.
  See http://www.open-std.org/jtc1/sc22/wg21/ for the official
  list.</EM></P>
<P>2025-09-28</P>
<HR>
<A NAME="621"></A><H4>621.
  
Template argument deduction from function return types
</H4>
<B>Section: </B>13.9.4&#160; [<A href="https://wg21.link/temp.expl.spec">temp.expl.spec</A>]
 &#160;&#160;&#160;

 <B>Status: </B>C++11
 &#160;&#160;&#160;

 <B>Submitter: </B>Richard Corden
 &#160;&#160;&#160;

 <B>Date: </B>16 February 2007<BR>


<P>[Voted into WP at August, 2010 meeting.]</P>



<P>It does not appear that the following example is well-formed,
although most compilers accept it:</P>

<PRE>
    template &lt;typename T&gt; T foo();
    template &lt;&gt; int foo();
</PRE>

<P>The reason is that 13.9.4 [<A href="https://wg21.link/temp.expl.spec#11">temp.expl.spec</A>] paragraph 11 only
allows trailing <I>template-argument</I>s to be omitted if they
&#8220;can be deduced from the function argument type,&#8221; and
there are no function arguments in this example.</P>

<P>13.9.4 [<A href="https://wg21.link/temp.expl.spec">temp.expl.spec</A>] should probably say &#8220;function
type&#8221; instead of &#8220;function argument type.&#8221;  Also,
a subsection should probably be added to 13.10.3 [<A href="https://wg21.link/temp.deduct">temp.deduct</A>]
to cover &#8220;Deducing template arguments from declarative
contexts&#8221; or some such.  It would be essentially the same as
13.10.3.3 [<A href="https://wg21.link/temp.deduct.funcaddr">temp.deduct.funcaddr</A>] except that the function type from
the declaration would be used as the type of <TT>P</TT>.</P>

<P><B>Proposed resolution (March, 2008):</B></P>

<OL>
<LI><P>Insert the following as a new subsection after
13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>]:</P></LI>

<BLOCKQUOTE>

<P><TABLE><TR>
<TD><INS>14.9.2.6 Deducing template arguments in a declaration
that names a specialization of a function template</INS></TD>
<TD ALIGN="right"><INS>[temp.deduct.funcdecl]</INS></TD>
</TR></TABLE></P>

<P><INS>Template arguments can be deduced from the function type
specified when declaring a specialization of a function
template. [<I>Note:</I> this can occur in the context of an
explicit specialization, an explicit instantiation, or a
friend declaration. &#8212;<I>end note</I>] The function
template's function type and the declared type are used as
the types of <TT>P</TT> and <TT>A</TT>, and the deduction is
done as described in 13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>].</INS></P>

</BLOCKQUOTE>

<LI><P>Change 13.9.4 [<A href="https://wg21.link/temp.expl.spec#11">temp.expl.spec</A>] paragraph 11 as
follows:</P></LI>

<BLOCKQUOTE>

A trailing <I>template-argument</I> can be left unspecified in the
<I>template-id</I> naming an explicit function template specialization
provided it can be deduced from the function <DEL>argument</DEL> type
<INS>(14.9.2.6 [temp.deduct.funcdecl])</INS>...

</BLOCKQUOTE>

</OL>

<P><B>Notes from the September, 2008 meeting:</B></P>

<P>The proposed resolution is probably more than is needed.
Instead of a complete new section, the material could become a
paragraph in 13.7.7 [<A href="https://wg21.link/temp.fct">temp.fct</A>].</P>

<P><B>Proposed resolution (February, 2010):</B></P>

<P>Add the following paragraph at the end of 13.7.7 [<A href="https://wg21.link/temp.fct">temp.fct</A>]:</P>

<BLOCKQUOTE>

<INS>In a declaration that names a specialization of a function
template, template arguments can be deduced from the function
type.  [<I>Note:</I> this can occur in the context of an explicit
specialization, an explicit instantiation, or a friend
declaration. &#8212;<I>end note</I>] The function template's
function type and the declared type are used as the types of
<TT>P</TT> and <TT>A</TT> and the deduction is done as described
in 13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>].</INS>

</BLOCKQUOTE>

<P><B>Proposed resolution (March, 2010):</B></P>

<P>This issue is resolved by the resolution of <A HREF="873.html">issue 873</A>.</P>

<BR><BR>
</BODY>
</HTML>
