<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 495</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="495"></A><H4>495.
  
Overload resolution with template and non-template conversion functions
</H4>
<B>Section: </B>12.2.4&#160; [<A href="https://wg21.link/over.match.best">over.match.best</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Nathan Sidwell
 &#160;&#160;&#160;

 <B>Date: </B>20 Dec 2004<BR>


<P>[Voted into WP at July, 2009 meeting.]</P>

<P>
The overload resolution rules for ranking a template against a
non-template function differ for conversion functions in a
surprising way.  12.2.4 [<A href="https://wg21.link/over.match.best">over.match.best</A>] lists four checks,
the last three concern this report.  For the non-conversion
operator case, checks 2 and 3 are applicable, whereas for the
conversion operator case checks 3 and 4 are applicable. Checks 2
and 4 concern the ranking of argument and return value conversion
sequences respectively.  Check 3 concerns only the templatedness
of the functions being ranked, and will prefer a non-template to
a template.  Notice that this check happens after argument
conversion sequence ranking, but <I>before</I> return value
conversion sequence ranking.  This has the effect of always
selecting a non-template conversion operator, as the following
example shows:
</P>

<PRE>
    struct C
    {
      inline operator int () { return 1; }
      template &lt;class T&gt; inline operator T () { return 0; }
    };

    inline long f (long x) { return x; }

    int
    main (int argc, char *argv[])
    {
      return f (C ());
    }
</PRE>

<P>
The non-templated <TT>C::operator int</TT> function will be
selected, rather than the apparently better
<TT>C::operator long&lt;long&gt;</TT> instantiation.  This is a
surprise, and resulted in a bug report where the user expected
the template to be selected.  In addition some C++ compilers have
implemented the overload ranking as if checks 3 and 4 were
transposed.
</P>

<P>
Is this ordering accidental, or is there a rationale?
</P>

<P><B>Notes from the April, 2005 meeting:</B></P>

<P>The CWG agreed that the template/non-template distinction should
be the final tie-breaker.</P>

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

<P>In the second bulleted list of 12.2.4 [<A href="https://wg21.link/over.match.best#1">over.match.best</A>] paragraph 1,
 move the second and third bullets to the end of the list,
to read as follows:</P>

<BLOCKQUOTE>

<UL>

<LI><P>for some argument <I>j</I>, ICS<I>j</I>(<TT>F1</TT>) is a
better conversion sequence than ICS<I>j</I>(<TT>F2</TT>), or, if not
that,</P></LI>

<LI><P>the context is an initialization by user-defined conversion
(see 9.5 [<A href="https://wg21.link/dcl.init">dcl.init</A>], 12.2.2.6 [<A href="https://wg21.link/over.match.conv">over.match.conv</A>], and
12.2.2.7 [<A href="https://wg21.link/over.match.ref">over.match.ref</A>]) and the standard conversion sequence
from the return type of <TT>F1</TT> to the destination type (i.e., the
type of the entity being initialized) is a better conversion sequence
than the standard conversion sequence from the return type
of <TT>F2</TT> to the destination type, [<I>Example:</I> ...
&#8212;<I>end example</I>] or, if not that,</P></LI>

<P><LI>
<TT>F1</TT> is a non-template function and <TT>F2</TT> is a
function template specialization, or, if not that,</LI></P>

<LI><P>
<TT>F1</TT> and <TT>F2</TT> are function template
specializations, and the function template for <TT>F1</TT> is more
specialized than the template for <TT>F2</TT> according to the partial
ordering rules described in 13.7.7.3 [<A href="https://wg21.link/temp.func.order">temp.func.order</A>].</P></LI>

</UL>

</BLOCKQUOTE>

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