<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3175: The CommonReference requirement of concept SwappableWith is not satisfied in the example</title>
<meta property="og:title" content="Issue 3175: The CommonReference requirement of concept SwappableWith is not satisfied in the example">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3175.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#C++20">C++20</a> status.</em></p>
<h3 id="3175"><a href="lwg-defects.html#3175">3175</a>. The <code>CommonReference</code> requirement of concept <code>SwappableWith</code> is not satisfied in the example</h3>
<p><b>Section:</b> 18.4.9 <a href="https://wg21.link/concept.swappable">[concept.swappable]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Kostas Kyrimis <b>Opened:</b> 2018-12-14 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>1
</p>
<p><b>View other</b> <a href="lwg-index-open.html#concept.swappable">active issues</a> in [concept.swappable].</p>
<p><b>View all other</b> <a href="lwg-index.html#concept.swappable">issues</a> in [concept.swappable].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The defect stems from the example found in sub-clause 18.4.9 <a href="https://wg21.link/concept.swappable">[concept.swappable]</a> p5:
</p>
<blockquote>
<pre>
[&hellip;]

template&lt;class T, std::SwappableWith&lt;T&gt; U&gt;
void value_swap(T&amp;&amp; t, U&amp;&amp; u) {
  ranges::swap(std::forward&lt;T&gt;(t), std::forward&lt;U&gt;(u));
}

[&hellip;]
namespace N {
  struct A { int m; };
  struct Proxy { A* a; };
  Proxy proxy(A&amp; a) { return Proxy{ &amp;a }; }
  
  void swap(A&amp; x, Proxy p) {
    ranges::swap(x.m, p.a-&gt;m);
  }
  void swap(Proxy p, A&amp; x) { swap(x, p); } <i>// satisfy symmetry requirement</i>
}

int main() {
  [&hellip;]
  N::A a1 = { 5 }, a2 = { -5 };
  value_swap(a1, proxy(a2)); // diagnostic manifests here(#1)
  assert(a1.m == -5 &amp;&amp; a2.m == 5);
}
</pre>
</blockquote>
<p>
The call to <code>value_swap(a1, proxy(a2))</code> resolves to [<code>T</code> = <code>N::A&amp;</code>, <code>U</code> = <code>N::Proxy</code>]
The compiler will issue a diagnostic for #1 because:
</p>
<ol>
<li><p>rvalue <code>proxy(a2)</code> is not swappable</p></li>
<li><p>concept <code>SwappableWith&lt;T, U&gt;</code> requires <code>N::A</code> and <code>Proxy</code> to model
<code>CommonReference&lt;const remove_reference_t&lt;T&gt;&amp;, const remove_reference_t&lt;U&gt;&amp;&gt;</code> It follows 
from the example that there is no common reference for [<code>T</code> = <code>N::A&amp;</code>, <code>U</code> = <code>N::Proxy</code>]</p></li>
</ol>

<p><i>[2019-06-20; Casey Carter comments and provides improved wording]</i></p>

<p>
The purpose of the <code>CommonReference</code> requirements in the cross-type concepts is to provide a 
sanity check. The fact that two types satisfy a single-type concept, have a common reference type 
that satisfies that concept, and implement cross-type operations required by the cross-type flavor 
of that concept very strongly suggests the programmer intends them to model the cross-type concept. 
It's an opt-in that requires some actual work, so it's unlikely to be inadvertent.
<p/>
The <code>CommonReference&lt;const T&amp;, const U&amp;&gt;</code> pattern makes sense for the comparison 
concepts which require that all variations of <code>const</code> and value category be comparable: we 
use <code>const</code> lvalues to trigger the "implicit expression variation" wording in 
18.2 <a href="https://wg21.link/concepts.equality">[concepts.equality]</a>. <code>SwappableWith</code>, however, doesn't care about implicit expression 
variations: it only needs to witness that we can exchange the values denoted by two reference-y 
expressions <code>E1</code> and <code>E2</code>. This suggests that <code>CommonReference&lt;decltype((E1)), 
decltype((E2))&gt;</code> is a more appropriate requirement than the current 
<code>CommonReference&lt;const remove_reference_t&lt;&hellip;&gt;</code> mess that was blindly copied 
from the comparison concepts.
<p/>
We must change the definition of "exchange the values" in 18.4.9 <a href="https://wg21.link/concept.swappable">[concept.swappable]</a> &mdash; 
which refers to the common reference type &mdash; consistently.
</p>

<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4791">N4791</a>.</p>

<ol>
<li><p>Change 18.4.9 <a href="https://wg21.link/concept.swappable">[concept.swappable]</a> as indicated:</p>

<blockquote>
<p>
-3- [&hellip;]
</p>
<blockquote>
<pre>
template&lt;class T&gt;
  concept Swappable = requires(T&amp; a, T&amp; b) { ranges::swap(a, b); };
  
template&lt;class T, class U&gt;
  concept SwappableWith =
  CommonReference&lt;<ins>T, U</ins><del>const remove_reference_t&lt;T&gt;&amp;, const remove_reference_t&lt;U&gt;&amp;</del>&gt; &amp;&amp;
  requires(T&amp;&amp; t, U&amp;&amp; u) {
    ranges::swap(std::forward&lt;T&gt;(t), std::forward&lt;T&gt;(t));
    ranges::swap(std::forward&lt;U&gt;(u), std::forward&lt;U&gt;(u));
    ranges::swap(std::forward&lt;T&gt;(t), std::forward&lt;U&gt;(u));
    ranges::swap(std::forward&lt;U&gt;(u), std::forward&lt;T&gt;(t));
  };
</pre>
</blockquote>
<p>
-4- [&hellip;]
<p/>
-5- [<i>Example:</i> User code can ensure that the evaluation of <code>swap</code> calls is performed in an appropriate context
under the various conditions as follows:
</p>
<blockquote>
<pre>
#include &lt;cassert&gt;
#include &lt;concepts&gt;
#include &lt;utility&gt;

namespace ranges = std::ranges;

template&lt;class T, std::SwappableWith&lt;T&gt; U&gt;
void value_swap(T&amp;&amp; t, U&amp;&amp; u) {
  ranges::swap(std::forward&lt;T&gt;(t), std::forward&lt;U&gt;(u));
}

template&lt;std::Swappable T&gt;
void lv_swap(T&amp; t1, T&amp; t2) {
  ranges::swap(t1, t2);
}

namespace N {
  struct A { int m; };
  struct Proxy { 
    A* a;
    <ins>Proxy(A&amp; a) : a{&amp;a} {}</ins>
    <ins>friend void swap(Proxy&amp;&amp; x, Proxy&amp;&amp; y) {
      ranges::swap(x.a, y.a);
    }</ins>
  };
  Proxy proxy(A&amp; a) { return Proxy{ &amp;a }; }
  void swap(A&amp; x, Proxy p) {
    ranges::swap(x.m, p.a->m);
  }
  void swap(Proxy p, A&amp; x) { swap(x, p); } // satisfy symmetry requirement
}

int main() {
  int i = 1, j = 2;
  lv_swap(i, j);
  assert(i == 2 &amp;&amp; j == 1);
  N::A a1 = { 5 }, a2 = { -5 };
  value_swap(a1, proxy(a2));
  assert(a1.m == -5 &amp;&amp; a2.m == 5);
}
</pre>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>
<p><i>[2020-01-16 Priority set to 1 after discussion on the reflector.]</i></p>


<p><i>[2020-02-10 Move to Immediate Monday afternoon in Prague]</i></p>



<p id="res-3175"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4820">N4820</a>.</p>

<ol>
<li><p>Change 18.4.9 <a href="https://wg21.link/concept.swappable">[concept.swappable]</a> as indicated:</p>

<blockquote>
<p>
-1- Let <code>t1</code> and <code>t2</code> be equality-preserving expressions that denote distinct equal objects 
of type <code>T</code>, and let <code>u1</code> and <code>u2</code> similarly denote distinct equal objects of type 
<code>U</code>. [<i>Note:</i> <code>t1</code> and <code>u1</code> can denote distinct objects, or the same object. 
&mdash; <i>end note</i>] An operation <i>exchanges the values</i> denoted by <code>t1</code> and <code>u1</code> if 
and only if the operation modifies neither <code>t2</code> nor <code>u2</code> and:
</p>
<ol style="list-style-type:none">
<li><p>(1.1) &mdash; If <code>T</code> and <code>U</code> are the same type, the result of the operation is that 
<code>t1</code> equals <code>u2</code> and <code>u1</code> equals <code>t2</code>.</p></li>
<li><p>(1.2) &mdash; If <code>T</code> and <code>U</code> are different types <del>that model 
<code>CommonReference&lt;const T&amp;, const U&amp;&gt;</code></del><ins>and 
<code>CommonReference&lt;decltype((t1)), decltype((u1))&gt;</code> is modeled</ins>, the result of the 
operation is that <code>C(t1)</code> equals <code>C(u2)</code> and <code>C(u1)</code> equals <code>C(t2)</code> where 
<code>C</code> is <code>common_reference_t&lt;<del>const T&amp;, 
const U&amp;</del><ins>decltype((t1)), decltype((u1))</ins>&gt;</code>.</p></li>
</ol>
<p>
-2- [&hellip;]
<p/>
-3- [&hellip;]
</p>
<blockquote>
<pre>
template&lt;class T&gt;
  concept Swappable = requires(T&amp; a, T&amp; b) { ranges::swap(a, b); };
  
template&lt;class T, class U&gt;
  concept SwappableWith =
  CommonReference&lt;<ins>T, U</ins><del>const remove_reference_t&lt;T&gt;&amp;, const remove_reference_t&lt;U&gt;&amp;</del>&gt; &amp;&amp;
  requires(T&amp;&amp; t, U&amp;&amp; u) {
    ranges::swap(std::forward&lt;T&gt;(t), std::forward&lt;T&gt;(t));
    ranges::swap(std::forward&lt;U&gt;(u), std::forward&lt;U&gt;(u));
    ranges::swap(std::forward&lt;T&gt;(t), std::forward&lt;U&gt;(u));
    ranges::swap(std::forward&lt;U&gt;(u), std::forward&lt;T&gt;(t));
  };
</pre>
</blockquote>
<p>
-4- [&hellip;]
<p/>
-5- [<i>Example:</i> User code can ensure that the evaluation of <code>swap</code> calls is performed in an appropriate context
under the various conditions as follows:
</p>
<blockquote>
<pre>
#include &lt;cassert&gt;
#include &lt;concepts&gt;
#include &lt;utility&gt;

namespace ranges = std::ranges;

template&lt;class T, std::SwappableWith&lt;T&gt; U&gt;
void value_swap(T&amp;&amp; t, U&amp;&amp; u) {
  ranges::swap(std::forward&lt;T&gt;(t), std::forward&lt;U&gt;(u));
}

template&lt;std::Swappable T&gt;
void lv_swap(T&amp; t1, T&amp; t2) {
  ranges::swap(t1, t2);
}

namespace N {
  struct A { int m; };
  struct Proxy { 
    A* a;
    <ins>Proxy(A&amp; a) : a{&amp;a} {}</ins>
    <ins>friend void swap(Proxy x, Proxy y) {
      ranges::swap(*x.a, *y.a);
    }</ins>
  };
  Proxy proxy(A&amp; a) { return Proxy{ <del>&amp;</del>a }; }
  <del>void swap(A&amp; x, Proxy p) {
    ranges::swap(x.m, p.a->m);
  }
  void swap(Proxy p, A&amp; x) { swap(x, p); } <i>// satisfy symmetry requirement</i></del>
}

int main() {
  int i = 1, j = 2;
  lv_swap(i, j);
  assert(i == 2 &amp;&amp; j == 1);
  N::A a1 = { 5 }, a2 = { -5 };
  value_swap(a1, proxy(a2));
  assert(a1.m == -5 &amp;&amp; a2.m == 5);
}
</pre>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
