<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3194: ConvertibleTo prose does not match code</title>
<meta property="og:title" content="Issue 3194: ConvertibleTo prose does not match code">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3194.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="3194"><a href="lwg-defects.html#3194">3194</a>. <code>ConvertibleTo</code> prose does not match code</h3>
<p><b>Section:</b> 18.4.4 <a href="https://wg21.link/concept.convertible">[concept.convertible]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Hubert Tong  <b>Opened:</b> 2019-03-05 <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.convertible">active issues</a> in [concept.convertible].</p>
<p><b>View all other</b> <a href="lwg-index.html#concept.convertible">issues</a> in [concept.convertible].</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 prose in <a href="https://wg21.link/n4800">N4800</a> subclause  [concept.convertibleto] indicates 
that the requirement is for an expression of a particular type and value category to be both implicitly and explicitly 
convertible to some other type. However, for a type
</p>
<blockquote><pre>
struct A { A(const A&amp;) = delete; };
</pre></blockquote>
<p>
<code>ConvertibleTo&lt;const A, A&gt;</code> would be <code>false</code> despite the following being okay:
</p>
<blockquote><pre>
const A f();

A test() {
  static_cast&lt;A&gt;(f());
  return f();
}
</pre></blockquote>

<p><i>[2019-03-15 Priority set to 1 after reflector discussion]</i></p>


<p><i>[2019-07-14 Tim adds PR based on discussion in 2019-07-09 LWG telecon]</i></p>


<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4820">N4820</a>, and also resolves LWG <a href="lwg-defects.html#3151" title="ConvertibleTo rejects conversions from array and function types (Status: Resolved)">3151</a><sup><a href="https://cplusplus.github.io/LWG/issue3151" title="Latest snapshot">(i)</a></sup>.</p>

<ol>
<li><p>Modify  [concept.convertibleto] as indicated:</p>

<blockquote>
<p>
-1- The <code>ConvertibleTo</code> concept requires <del>an</del><ins>a glvalue</ins> expression of a particular type and
value category to be both implicitly and explicitly convertible to some other type. The implicit and explicit conversions
are required to produce equal results.
</p>

<pre>
template&lt;class From, class To&gt;
  concept ConvertibleTo =
    is_convertible_v&lt;From, To&gt; &amp;&amp;
    requires(<ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins> (&amp;f)()) {
      static_cast&lt;To&gt;(f());
    };
</pre>
<blockquote>
<p>
-2- Let <code>test</code> be the invented function:
</p>
<blockquote>
<pre>
To test(<ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins> (&amp;f)()) {
  return f();
}
</pre>
</blockquote>
<p>
for some types <code>From</code> and <code>To</code>, and let <code>f</code> be a function with no arguments and return type
<code><ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins></code> such that <code>f()</code> is equality-preserving.
<code>From</code> and <code>To</code> model <code>ConvertibleTo&lt;From, To&gt;</code> only if:
</p>
<ol style="list-style-type:none">
<li><p>(2.1) &mdash; <code>To</code> is not an object or reference-to-object type, or <code>static_cast&lt;To&gt;(f())</code>
is equal to <code>test(f)</code>.</p></li>
<li><p>(2.2) &mdash; <code><ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins></code> is not a reference-to-object type, or</p>
<ol style="list-style-type:none">
<li><p>(2.2.1) &mdash; If <code><ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins></code> is an rvalue reference 
to a non const-qualified type, the resulting state of the object referenced by <code>f()</code> after either above expression is valid
but unspecified (16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>).</p></li>
<li><p>(2.2.2) &mdash; Otherwise, the object referred to by <code>f()</code> is not modified by either above expression.</p></li>
</ol>
</li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2019-09-23; Daniel adjusts wording to working draft changes]</i></p>

<p>
Due to the concept renaming caused by <a href="https://wg21.link/p1754r1">P1754R1</a> the proposed
wording is outdated and needs adjustments.
</p>
<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4830">N4830</a>, and also resolves LWG <a href="lwg-defects.html#3151" title="ConvertibleTo rejects conversions from array and function types (Status: Resolved)">3151</a><sup><a href="https://cplusplus.github.io/LWG/issue3151" title="Latest snapshot">(i)</a></sup>.</p>

<ol>
<li><p>Modify 18.4.4 <a href="https://wg21.link/concept.convertible">[concept.convertible]</a> as indicated:</p>

<blockquote>
<p>
-1- The <code>convertible_to</code> concept requires <del>an</del><ins>a glvalue</ins> expression of a particular type and
value category to be both implicitly and explicitly convertible to some other type. The implicit and explicit conversions
are required to produce equal results.
</p>

<pre>
template&lt;class From, class To&gt;
  concept convertible_to =
    is_convertible_v&lt;From, To&gt; &amp;&amp;
    requires(<ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins> (&amp;f)()) {
      static_cast&lt;To&gt;(f());
    };
</pre>
<blockquote>
<p>
-2- Let <code>test</code> be the invented function:
</p>
<blockquote>
<pre>
To test(<ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins> (&amp;f)()) {
  return f();
}
</pre>
</blockquote>
<p>
for some types <code>From</code> and <code>To</code>, and let <code>f</code> be a function with no arguments and return type
<code><ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins></code> such that <code>f()</code> is equality-preserving.
<code>From</code> and <code>To</code> model <code>convertible_to&lt;From, To&gt;</code> only if:
</p>
<ol style="list-style-type:none">
<li><p>(2.1) &mdash; <code>To</code> is not an object or reference-to-object type, or <code>static_cast&lt;To&gt;(f())</code>
is equal to <code>test(f)</code>.</p></li>
<li><p>(2.2) &mdash; <code><ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins></code> is not a reference-to-object type, or</p>
<ol style="list-style-type:none">
<li><p>(2.2.1) &mdash; If <code><ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins></code> is an rvalue reference 
to a non const-qualified type, the resulting state of the object referenced by <code>f()</code> after either above expression is valid
but unspecified (16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>).</p></li>
<li><p>(2.2.2) &mdash; Otherwise, the object referred to by <code>f()</code> is not modified by either above expression.</p></li>
</ol>
</li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>
<p><i>[2019-11-06 Tim updates PR based on discussion in Belfast LWG evening session]</i></p>

<p>
"glvalue" is incorrect because we want to allow testing <code>convertible_to&lt;void, void&gt;</code>. It's also less than clear
how the "expression" and "a particular type" in the first sentence correspond to the parameters of the concept.
</p>

<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4835">N4835</a>, and also resolves LWG <a href="lwg-defects.html#3151" title="ConvertibleTo rejects conversions from array and function types (Status: Resolved)">3151</a><sup><a href="https://cplusplus.github.io/LWG/issue3151" title="Latest snapshot">(i)</a></sup>.</p>

<ol>
<li><p>Modify 18.4.4 <a href="https://wg21.link/concept.convertible">[concept.convertible]</a> as indicated:</p>

<blockquote>
<p>
-1- The <code>convertible_to</code> concept <ins>for types <code>From</code> and <code>To</code></ins> requires an expression
<ins><code>E</code> such that <code>decltype((E))</code> is <code>add_rvalue_reference_t&lt;From&gt;</code></ins>
<del>of a particular type and value category</del> to be both implicitly and explicitly convertible to 
<del>some other type</del> <ins><code>To</code></ins>. The implicit and explicit conversions are required to produce equal results.
</p>

<pre>
template&lt;class From, class To&gt;
  concept convertible_to =
    is_convertible_v&lt;From, To&gt; &amp;&amp;
    requires(<ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins> (&amp;f)()) {
      static_cast&lt;To&gt;(f());
    };
</pre>
<blockquote>
<p>
-2- Let <ins><code>FromR</code> be <code>add_rvalue_reference_t&lt;From&gt;</code> and</ins> <code>test</code> be the invented function:
</p>
<blockquote>
<pre>
To test(From<ins>R</ins> (&amp;f)()) {
  return f();
}
</pre>
</blockquote>
<p>
for some types <code>From</code> and <code>To</code>, and let <code>f</code> be a function with no arguments and return type
<code>From<ins>R</ins></code> such that <code>f()</code> is equality-preserving.
<code>From</code> and <code>To</code> model <code>convertible_to&lt;From, To&gt;</code> only if:
</p>
<ol style="list-style-type:none">
<li><p>(2.1) &mdash; <code>To</code> is not an object or reference-to-object type, or <code>static_cast&lt;To&gt;(f())</code>
is equal to <code>test(f)</code>.</p></li>
<li><p>(2.2) &mdash; <code>From<ins>R</ins></code> is not a reference-to-object type, or</p>
<ol style="list-style-type:none">
<li><p>(2.2.1) &mdash; If <code>From<ins>R</ins></code> is an rvalue reference 
to a non const-qualified type, the resulting state of the object referenced by <code>f()</code> after either above expression is valid
but unspecified (16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>).</p></li>
<li><p>(2.2.2) &mdash; Otherwise, the object referred to by <code>f()</code> is not modified by either above expression.</p></li>
</ol>
</li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>
<p><i>[2019-11-09 Tim rephrased first sentence based on discussion in Belfast LWG Saturday session]</i></p>

<p><i>[Status to Tentatively ready after Belfast LWG Saturday session]</i></p>



<p id="res-3194"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4835">N4835</a>, and also resolves LWG <a href="lwg-defects.html#3151" title="ConvertibleTo rejects conversions from array and function types (Status: Resolved)">3151</a><sup><a href="https://cplusplus.github.io/LWG/issue3151" title="Latest snapshot">(i)</a></sup>.</p>

<ol>
<li><p>Modify 18.4.4 <a href="https://wg21.link/concept.convertible">[concept.convertible]</a> as indicated:</p>

<blockquote>
<p>
-1- <ins>Given types <code>From</code> and <code>To</code> and an expression <code>E</code> such that <code>decltype((E))</code>
is <code>add_rvalue_reference_t&lt;From&gt;</code>, <code>convertible_to&lt;From, To&gt;</code></ins>
<del>The <code>convertible_to</code> concept</del> requires <ins><code>E</code></ins>
<del>an expression of a particular type and value category</del> to be both implicitly and explicitly convertible to 
<del>some other</del> type<ins> <code>To</code></ins>. The implicit and explicit conversions are required to produce equal results.
</p>

<pre>
template&lt;class From, class To&gt;
  concept convertible_to =
    is_convertible_v&lt;From, To&gt; &amp;&amp;
    requires(<ins>add_rvalue_reference_t&lt;</ins>From<ins>&gt;</ins> (&amp;f)()) {
      static_cast&lt;To&gt;(f());
    };
</pre>
<blockquote>
<p>
-2- Let <ins><code>FromR</code> be <code>add_rvalue_reference_t&lt;From&gt;</code> and</ins> <code>test</code> be the invented function:
</p>
<blockquote>
<pre>
To test(From<ins>R</ins> (&amp;f)()) {
  return f();
}
</pre>
</blockquote>
<p>
for some types <code>From</code> and <code>To</code>, and let <code>f</code> be a function with no arguments and return type
<code>From<ins>R</ins></code> such that <code>f()</code> is equality-preserving.
<code>From</code> and <code>To</code> model <code>convertible_to&lt;From, To&gt;</code> only if:
</p>
<ol style="list-style-type:none">
<li><p>(2.1) &mdash; <code>To</code> is not an object or reference-to-object type, or <code>static_cast&lt;To&gt;(f())</code>
is equal to <code>test(f)</code>.</p></li>
<li><p>(2.2) &mdash; <code>From<ins>R</ins></code> is not a reference-to-object type, or</p>
<ol style="list-style-type:none">
<li><p>(2.2.1) &mdash; If <code>From<ins>R</ins></code> is an rvalue reference 
to a non const-qualified type, the resulting state of the object referenced by <code>f()</code> after either above expression is valid
but unspecified (16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>).</p></li>
<li><p>(2.2.2) &mdash; Otherwise, the object referred to by <code>f()</code> is not modified by either above expression.</p></li>
</ol>
</li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
