<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 606</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="606"></A><H4>606.
  
Template argument deduction for rvalue references
</H4>
<B>Section: </B>13.10.3.2&#160; [<A href="https://wg21.link/temp.deduct.call">temp.deduct.call</A>]
 &#160;&#160;&#160;

 <B>Status: </B>CD1
 &#160;&#160;&#160;

 <B>Submitter: </B>Peter Dimov
 &#160;&#160;&#160;

 <B>Date: </B>1 December 2006<BR>


<P>[Voted into the WP at the September, 2008 meeting as part of
paper N2757.]</P>



<P>There are a couple of minor problems with the rvalue reference
wording in the WP.  The non-normative note in
13.10.3.2 [<A href="https://wg21.link/temp.deduct.call#3">temp.deduct.call</A>] paragraph 3 says,</P>

<BLOCKQUOTE>

[<I>Note:</I> The effect of this rule for lvalue arguments and
rvalue reference parameters is that deduction in such cases will
fail unless the function parameter is of the form <I>cv</I>
<TT>T&amp;&amp;</TT> (13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>]).
&#8212;<I>end note</I>]

</BLOCKQUOTE>

<P>It turns out that this isn't correct.  For example:</P>

<PRE>
    template &lt;class T&gt; void g(basic_string&lt;T&gt; &amp;&amp; );
    ...
    basic_string&lt;char&gt; s;
    g(s);    //<SPAN CLASS="cmnt"> Note says that it should fail, we want it to call</SPAN>
             //<SPAN CLASS="cmnt"> </SPAN>g&lt;char&gt;(basic_string&lt;char&gt;&amp;&amp;)
</PRE>

<P>Additionally, consider this case:</P>

<PRE>
    template &lt;class T&gt; void f(const T&amp;&amp;);
    ...
    int i;
    f(i);
</PRE>

<P>If we deduce <TT>T</TT> as <TT>int&amp;</TT> in this case then
<TT>f(i)</TT> calls <TT>f&lt;int&amp;&gt;(int&amp;)</TT>, which
seems counterintuitive.  We prefer that
<TT>f&lt;int&gt;(const int&amp;&amp;)</TT> be called.  Therefore,
we would like the wording clarified that the <TT>A&amp;</TT>
deduction rule in 13.10.3.2 [<A href="https://wg21.link/temp.deduct.call#3">temp.deduct.call</A>] paragraph 3
applies only to the form <TT>T&amp;&amp;</TT> and not to
<I>cv</I> <TT>T&amp;&amp;</TT> as the note currently implies.</P>

<P>These are minor tweaks to the rvalue reference wording and a
fallout from <A HREF="540.html">issue 540</A>.  In particular,
the major applications of move semantics and perfect forwarding
are not impacted with respect to the original intentions of the
rvalue reference work by these suggestions.</P>

<P><U>Suggested resolution:</U></P>

<P>Change 13.10.3.2 [<A href="https://wg21.link/temp.deduct.call#3">temp.deduct.call</A>] paragraph 3 as
follows:</P>

<BLOCKQUOTE>

<P>If <TT>P</TT> is <DEL>an rvalue reference type</DEL> <INS>of the form
<TT>T&amp;&amp;</TT>, where <TT>T</TT> is a template
parameter,</INS> and the argument is an lvalue, <DEL>the
type <TT>A&amp;</TT> is used in place of <TT>A</TT> for type
deduction</DEL> <INS><TT>T</TT> is deduced
as <TT>A&amp;</TT></INS>. [<I>Example:</I>
</P>

<PRE>
    template &lt;typename T&gt; int f(T&amp;&amp;);
    int i;
    int j = f(i); //<SPAN CLASS="cmnt"> calls </SPAN>f&lt;int&amp;&gt;(i)
    <INS>template &lt;typename T&gt; int g(const T&amp;&amp;);
    int k;
    int n = g(k); //<SPAN CLASS="cmnt"> calls </SPAN>g&lt;int&gt;(k)</INS>
</PRE>

<P>&#8212;<I>end example</I>]<DEL>[<I>Note:</I> The effect of this
rule for lvalue arguments and rvalue reference parameters is that
deduction in such cases will fail unless the function parameter
is of the form <I>cv</I> <TT>T&amp;&amp;</TT> (13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>]). &#8212;<I>end note</I>]</DEL>
</P>

</BLOCKQUOTE>

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

<P>Change 13.10.3.2 [<A href="https://wg21.link/temp.deduct.call#3">temp.deduct.call</A>] paragraph 3 as
follows:</P>

<BLOCKQUOTE>

<P>If <TT>P</TT> is <DEL>an rvalue reference type</DEL> <INS>of the form
<TT>T&amp;&amp;</TT>, where <TT>T</TT> is a template
parameter,</INS> and the argument is an lvalue, the
type <TT>A&amp;</TT> is used in place of <TT>A</TT> for type
deduction. [<I>Example:</I>
</P>

<PRE>
    template &lt;typename T&gt; int f(T&amp;&amp;);
    int i;
    int j = f(i); //<SPAN CLASS="cmnt"> calls </SPAN>f&lt;int&amp;&gt;(i)
    <INS>template &lt;typename T&gt; int g(const T&amp;&amp;);
    int k;
    int n = g(k); //<SPAN CLASS="cmnt"> calls </SPAN>g&lt;int&gt;(k)</INS>
</PRE>

<P>&#8212;<I>end example</I>]<DEL>[<I>Note:</I> The effect of this
rule for lvalue arguments and rvalue reference parameters is that
deduction in such cases will fail unless the function parameter
is of the form <I>cv</I> <TT>T&amp;&amp;</TT> (13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>]). &#8212;<I>end note</I>]</DEL>
</P>

</BLOCKQUOTE>

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