<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3974: mdspan::operator[] should not copy OtherIndexTypes</title>
<meta property="og:title" content="Issue 3974: mdspan::operator[] should not copy OtherIndexTypes">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3974.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="3974"><a href="lwg-defects.html#3974">3974</a>. <code>mdspan::operator[]</code> should not copy <code>OtherIndexTypes</code></h3>
<p><b>Section:</b> 23.7.3.6.3 <a href="https://wg21.link/mdspan.mdspan.members">[mdspan.mdspan.members]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2023-08-12 <b>Last modified:</b> 2023-11-22</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>
The wording for <code>mdspan</code>'s <code>operator[]</code> overloads that accept <code>span</code> and <code>array</code> is in 
23.7.3.6.3 <a href="https://wg21.link/mdspan.mdspan.members">[mdspan.mdspan.members]</a> paragraphs 5 and 6:
</p>
<blockquote><pre>
template&lt;class OtherIndexType&gt;
  constexpr reference operator[](span&lt;OtherIndexType, rank()&gt; indices) const;
template&lt;class OtherIndexType&gt;
  constexpr reference operator[](const array&lt;OtherIndexType, rank()&gt;&amp; indices) const;
</pre>
<p>
-5- <i>Constraints</i>:
</p>
<ol style="list-style-type: none">
<li><p>(5.1) &mdash; <code>is_convertible_v&lt;const OtherIndexType&amp;, index_type&gt;</code> is <code>true</code>, and</p></li>
<li><p>(5.2) &mdash; <code>is_nothrow_constructible_v&lt;index_type, const OtherIndexType&amp;&gt;</code> is <code>true</code>.</p></li>
</ol>
<p>
-6- <i>Effects</i>: Let <code>P</code> be a parameter pack such that
</p>
<blockquote><pre>
is_same_v&lt;make_index_sequence&lt;rank()&gt;, index_sequence&lt;P...&gt;&gt;
</pre></blockquote>
<p>
is <code>true</code>. Equivalent to:
</p>
<blockquote><pre>
return operator[](as_const(indices[P])...);
</pre></blockquote>
</blockquote>
<p>
The equivalent code calls the other <code>operator[]</code> overload:
</p>
<blockquote><pre>
template&lt;class... OtherIndexTypes&gt;
  constexpr reference operator[](OtherIndexTypes... indices) const;
</pre></blockquote>
<p>
with a pack of <code>const OtherIndexType</code> lvalues, but we notably haven't required <code>OtherIndexTypes</code> to be copyable &mdash; 
we only require that we can convert them to <code>index_type</code>. While one could argue that the use in "<i>Effects</i>: equivalent to" 
implies a requirement of copyability, it's odd that this implicit requirement would be the only requirement for copyable 
<code>OtherIndexTypes</code> in the spec. We could fix this by changing the <code>operator[]</code> overload accepting <code>OtherIndexTypes</code> 
to take them by <code>const&amp;</code>, but that would be inconsistent with virtually every other place in the spec where types 
convertible to <code>index_type</code> are taken by-value. I think the best localized fix is to perform the conversion to <code>index_type</code> 
in the "<i>Effects</i>: equivalent to" code so the actual arguments have type <code>index_type</code> which we know is copyable.
</p>

<p><i>[2023-11-02; Reflector poll]</i></p>

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

<p><i>[2023-11-11 Approved at November 2023 meeting in Kona. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3974"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4958" title=" Working Draft, Programming Languages — C++">N4958</a>.
</p>

<ol>

<li><p>Modify 23.7.3.6.3 <a href="https://wg21.link/mdspan.mdspan.members">[mdspan.mdspan.members]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class OtherIndexType&gt;
  constexpr reference operator[](span&lt;OtherIndexType, rank()&gt; indices) const;
template&lt;class OtherIndexType&gt;
  constexpr reference operator[](const array&lt;OtherIndexType, rank()&gt;&amp; indices) const;
</pre>
<blockquote>
<p>
-5- <i>Constraints</i>:
</p>
<ol style="list-style-type: none">
<li><p>(5.1) &mdash; <code>is_convertible_v&lt;const OtherIndexType&amp;, index_type&gt;</code> is <code>true</code>, and</p></li>
<li><p>(5.2) &mdash; <code>is_nothrow_constructible_v&lt;index_type, const OtherIndexType&amp;&gt;</code> is <code>true</code>.</p></li>
</ol>
<p>
-6- <i>Effects</i>: Let <code>P</code> be a parameter pack such that
</p>
<blockquote><pre>
is_same_v&lt;make_index_sequence&lt;rank()&gt;, index_sequence&lt;P...&gt;&gt;
</pre></blockquote>
<p>
is <code>true</code>. Equivalent to:
</p>
<blockquote><pre>
return operator[](<ins>extents_type::<i>index-cast</i>(</ins>as_const(indices[P])<ins>)</ins>...);
</pre></blockquote>
</blockquote>
</blockquote>

</li>
</ol>





</body>
</html>
