<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 976</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="976"></A><H4>976.
  
Deduction for <TT>const T&amp;</TT> conversion operators
</H4>
<B>Section: </B>13.10.3.4&#160; [<A href="https://wg21.link/temp.deduct.conv">temp.deduct.conv</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Jens Maurer
 &#160;&#160;&#160;

 <B>Date: </B>1 October, 2009<BR>


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



<P>Consider this program:</P>

<PRE>
    struct F {
       template&lt;class T&gt;
       operator const T&amp;() { static T t; return t; }
    };

    int main() {
       F f;
       int i = f;   // ill-formed
    }
</PRE>

<P>It's ill-formed, because according to 13.10.3.4 [<A href="https://wg21.link/temp.deduct.conv">temp.deduct.conv</A>],
we try to match <TT>const T</TT> with <TT>int</TT>.</P>

<P>(The reference got removed from <TT>P</TT> because of paragraph 3,
but the <TT>const</TT> isn't removed, because bullet 2.3
comes before paragraph 3 and thus isn't applied any more.)</P>

<P>Changing the declaration of the conversion operator to</P>

<PRE>
   operator T&amp;() { ... }
</PRE>

<P>makes the program compile, which is counter-intuitive to me:
I'm in an rvalue (read-only) context, and I can use a conversion
to <TT>T&amp;</TT>, but I can't use a conversion to <TT>const T&amp;</TT>?</P>

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

<P>Change 13.10.3.4 [<A href="https://wg21.link/temp.deduct.conv">temp.deduct.conv</A>] paragraphs 1-3 as follows,
inserting a new paragraph between the current paragraphs 1 and 2:</P>

<BLOCKQUOTE>

<P>Template argument deduction is done by comparing the return
type of the conversion function template (call it
<TT>P</TT><INS>; 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>] for the
determination of that type</INS>) with the type that is required
as the result of the conversion (call it <TT>A</TT>) as described
in 13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>].</P>

<P><INS>If <TT>P</TT> is a reference type, the type referred to
by <TT>P</TT> is used in place of <TT>P</TT> for type deduction
and for any further references to or transformations of
<TT>P</TT> in the remainder of this section.</INS></P>

<P>If <TT>A</TT> is not a reference type:</P>

<UL>
<LI><P>If <TT>P</TT> is an array type, the pointer type
produced by the array-to-pointer standard conversion
(7.3.3 [<A href="https://wg21.link/conv.array">conv.array</A>]) is used in place of <TT>P</TT> for type
deduction; otherwise,</P></LI>

<LI><P>If <TT>P</TT> is a function type, the pointer type
produced by the function-to-pointer standard conversion
(7.3.4 [<A href="https://wg21.link/conv.func">conv.func</A>]) is used in place of <TT>P</TT> for
type deduction; otherwise,</P></LI>

<LI><P>If <TT>P</TT> is a cv-qualified type, the top level cv-qualifiers
of <TT>P</TT>'s type are ignored for type deduction.</P></LI>

</UL>

<P>If <TT>A</TT> is a cv-qualified type, the top level
cv-qualifiers of <TT>A</TT>'s type are ignored for type
deduction.  If <TT>A</TT> is a reference type, the type referred
to by <TT>A</TT> is used for type deduction. <DEL>If <TT>P</TT>
is a reference type, the type referred to by <TT>P</TT> is used
for type deduction.</DEL>
</P>

</BLOCKQUOTE>

<P>(This resolution also resolves issues <A HREF="493.html">493</A>
and <A HREF="913.html">913</A>.)</P>

<P><I>[Drafting note: This change intentionally reverses the
resolution of <A HREF="322.html">issue 322</A> (and applies it in
a different form).]</I></P>

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