<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3995: Issue with custom index conversion in &lt;mdspan&gt;</title>
<meta property="og:title" content="Issue 3995: Issue with custom index conversion in &lt;mdspan&gt;">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3995.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#New">New</a> status.</em></p>
<h3 id="3995"><a href="lwg-active.html#3995">3995</a>. Issue with custom index conversion in <code>&lt;mdspan&gt;</code></h3>
<p><b>Section:</b> 23.7.3 <a href="https://wg21.link/views.multidim">[views.multidim]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2023-10-11 <b>Last modified:</b> 2023-10-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Currently, <code>std::layout_<i>meow</i>::mapping::operator()</code> has the following definition (23.7.3.4.5.3 <a href="https://wg21.link/mdspan.layout.left.obs">[mdspan.layout.left.obs]</a>):
</p>
<blockquote>
<pre>
template&lt;class... Indices&gt;
  constexpr index_type operator()(Indices... i) const noexcept;</pre>
<blockquote>
<p>
-2- <i>Constraints</i>:
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; <code>sizeof...(Indices) == extents_type::rank()</code> is <code>true</code>,</p></li>
<li><p>(2.2) &mdash; <code>(is_convertible_v&lt;Indices, index_type&gt; &amp;&amp; ...)</code> is <code>true</code>, and</p></li>
<li><p>(2.3) &mdash; <code>(is_nothrow_constructible_v&lt;index_type, Indices&gt; &amp;&amp; ...)</code> is <code>true</code>.</p></li>       
</ol>
<p>
<i>Preconditions</i>: <code>extents_type::<i>index-cast</i>(i)</code> is a multidimensional index in <code><i>extents_</i></code> 
(23.7.3.1 <a href="https://wg21.link/mdspan.overview">[mdspan.overview]</a>).
<p/>
<i>Effects</i>: Let <code>P</code> be a parameter pack such that
</p>
<blockquote><pre>
is_same_v&lt;index_sequence_for&lt;Indices...&gt;, index_sequence&lt;P...&gt;&gt;
</pre></blockquote>
<p>  
is <code>true</code>. Equivalent to:
</p>
<blockquote><pre>
return ((static_cast&lt;index_type&gt;(i) * stride(P)) + ... + 0);
</pre></blockquote>
</blockquote>
</blockquote>
<p>
Above, <code>is_convertible_v&lt;Indices, index_type&gt;</code> implies that <code>index_type</code> can be constructed through 
rvalue-qualified conversion operators. However, we cast the lvalue <code>i</code> in the return statement, which makes the 
expression possibly ill-formed. The same goes for <code>extents_type::<i>index-cast(i)</i></code>.
</p>
<p>
However, if we use <code>std::move</code> before casting, this will result in the rvalue-qualified conversion operator 
being called in <i>Preconditions</i> via <code>extents_type::<i>index-cast(i)</i></code> before the mapping index is actually calculated,
so that the expression may no longer be valid. And such an issue already exists in <code>mdspan::operator[]</code>.
</p>
<p>
In addition, the variadic version of <code>mdspan::operator[]</code> constraints 
<code>is_convertible_v&lt;OtherIndexTypes, index_type&gt;</code>, but its <code>array</code>/<code>span</code> version constraints 
<code>is_convertible_v&lt;const OtherIndexType&amp;, index_type&gt;</code>.
<p/>
This seems to bring inconsistency as <code>mdspan[arr]</code> may not necessarily guarantee <code>mdspan[arr[i]...]</code>.
</p>
<p>
I think we should unanimously require that custom indexes can be converted to <code>index_type</code> via <code>const</code> 
lvalue references, which eliminates the worry of conversion expiration.
</p>

<p><i>[2023-10-30; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
"P4 - doesn't affect 'normal' uses of custom index types.
Only affects expert users that interface with the mapping directly,
because <code>mdspan</code> does the conversions."
</p>



<p id="res-3995"><b>Proposed resolution:</b></p>





</body>
</html>
