<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4071: reference_wrapper comparisons are not SFINAE-friendly</title>
<meta property="og:title" content="Issue 4071: reference_wrapper comparisons are not SFINAE-friendly">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4071.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#WP">WP</a> status.</em></p>
<h3 id="4071"><a href="lwg-defects.html#4071">4071</a>. <code>reference_wrapper</code> comparisons are not SFINAE-friendly</h3>
<p><b>Section:</b> 22.10.6.6 <a href="https://wg21.link/refwrap.comparisons">[refwrap.comparisons]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2024-04-19 <b>Last modified:</b> 2024-07-08</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<a href="https://wg21.link/P2944R3" title=" Comparisons for reference_wrapper">P2944R3</a> added these hidden friends to <code class='backtick'>reference_wrapper</code>:
</p>
<pre><code>
   friend constexpr <em>synth-three-way-result</em>&lt;T&gt; operator&lt;=&gt;(reference_wrapper, reference_wrapper);
   friend constexpr <em>synth-three-way-result</em>&lt;T&gt; operator&lt;=&gt;(reference_wrapper, const T&amp;);
   friend constexpr <em>synth-three-way-result</em>&lt;T&gt; operator&lt;=&gt;(reference_wrapper, reference_wrapper&lt;const T&gt;);
</code></pre>

<p>
These functions are not templates, and so their declarations are ill-formed
for any type that does have any comparison operators, e.g.
<pre><code>
    struct A { } a;
    std::reference_wrapper&lt;A&gt; r(a);
</code></pre>
</p>
<p>
Instantiating <code>reference_wrapper&lt;A&gt;</code> will instantiate
the declarations of the hidden friends, which will attempt to determine the
return types of the <code>operator&lt;=&gt;</code> functions.
That fails because <em><code class='backtick'>synth-three-way</code></em> is constrained
and can't be called with arguments of type <code class='backtick'>A</code>.
</p>

<p>
This can be solved by changing those functions into templates, so they
aren't instantiated eagerly, e.g.,
<pre><code>
    <ins>template&lt;class U = T&gt;</ins>
    friend constexpr <em>synth-three-way-result</em>&lt;<del>T</del><ins>U</ins>&gt; operator&lt;=&gt;(reference_wrapper, reference_wrapper);
</code></pre>
or by giving them a deduced return type (so that it isn't instantiated eagerly)
and constraining them to only be callable when valid:
<pre><code>
    friend constexpr <del><em>synth-three-way-result</em>&lt;T&gt;</del><ins>auto</ins> operator&lt;=&gt;(reference_wrapper x, reference_wrapper y)
    <ins>requires requires (const T t) { <em>synth-three-way</em>(t, t); }</ins>
</code></pre>
The second alternative is used in the proposed resolution.
</p>
<p>
In practice the <em>requires-clause</em> can be implemented more simply
(and efficiently) by checking the constraints of <em>synth-three-way</em>
directly:
<pre><code>    requires (const T t) { { t &lt; t } -> <em>boolean-testable</em>; }
</code></pre>
but when specified in prose in a <em>Constraints</em>: element it seems
clearer to just use <code><em>synth-three-way</em>(x.get(), y.get())</code>.
</p>

<p>The proposed resolution has been committed to libstdc++'s master branch.</p>

<p><i>[2024-05-08; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after eight votes in favour during reflector poll.
</p>

<p><i>[St. Louis 2024-06-29; Status changed: Voting &rarr; WP.]</i></p>



<p id="res-4071"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4981" title=" Working Draft, Programming Languages — C++">N4981</a>.
</p>
<ol>
<li><p>Modify 22.10.6.1 <a href="https://wg21.link/refwrap.general">[refwrap.general]</a> as indicated:</p>

<blockquote>
<pre><code>
   // [refwrap.comparisons], comparisons
   friend constexpr bool operator==(reference_wrapper, reference_wrapper);
   friend constexpr bool operator==(reference_wrapper, const T&amp;);
   friend constexpr bool operator==(reference_wrapper, reference_wrapper&lt;const T&gt;);

   friend constexpr <del><em>synth-three-way-result</em>&lt;T&gt;</del><ins>auto</ins> operator&lt;=&gt;(reference_wrapper, reference_wrapper);
   friend constexpr <del><em>synth-three-way-result</em>&lt;T&gt;</del><ins>auto</ins> operator&lt;=&gt;(reference_wrapper, const T&amp;);
   friend constexpr <del><em>synth-three-way-result</em>&lt;T&gt;</del><ins>auto</ins> operator&lt;=&gt;(reference_wrapper, reference_wrapper&lt;const T&gt;);
</code></pre>
</blockquote>

</li>
<li><p>Modify 22.10.6.6 <a href="https://wg21.link/refwrap.comparisons">[refwrap.comparisons]</a> as indicated:</p>
<blockquote>
<pre><code>
friend constexpr <del><em>synth-three-way-result</em>&lt;T&gt;</del><ins>auto</ins> operator&lt;=&gt;(reference_wrapper x, reference_wrapper y);
</code></pre>

<p>
<ins>-?-
<em>Constraints</em>:
The expression
<code><em>synth-three-way</em>(x.get(), y.get())</code>
is well-formed.
</ins>
</p>

<p>-7-
<em>Returns</em>: <code><em>synth-three-way</em>(x.get(), y.get())</code>.
</p>

<pre><code>
friend constexpr <del><em>synth-three-way-result</em>&lt;T&gt;</del><ins>auto</ins> operator&lt;=&gt;(reference_wrapper x, const T&amp; y);
</code></pre>

<p>
<ins>-?-
<em>Constraints</em>:
The expression
<code><em>synth-three-way</em>(x.get(), y)</code>
is well-formed.
</ins>
</p>

<p>-8-
<em>Returns</em>: <code><em>synth-three-way</em>(x.get(), y)</code>.
</p>

<pre><code>
friend constexpr <del><em>synth-three-way-result</em>&lt;T&gt;</del><ins>auto</ins> operator&lt;=&gt;(reference_wrapper x, reference_wrapper&lt;const T&gt; y);
</code></pre>

<p>-9-
<em>Constraints</em>: <code>is_const_v&lt;T&gt;</code> is <code class='backtick'>false</code>.
<ins>
The expression
<code><em>synth-three-way</em>(x.get(), y.get())</code>
is well-formed.
</ins>
</p>

<p>-10-
<em>Returns</em>: <code><em>synth-three-way</em>(x.get(), y.get())</code>.
</p>
</blockquote>
</li>
</ol>






</body>
</html>
