|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4511" status="New"> |
| 5 | +<title>Inconsistency between the deduction guide of `std::mdspan` taking `(data_handle_type, mapping_type, accessor_type)` |
| 6 | +and the corresponding constructor</title> |
| 7 | +<section><sref ref="[mdspan.mdspan.overview]"/></section> |
| 8 | +<submitter>Jiang An</submitter> |
| 9 | +<date>09 Jan 2026</date> |
| 10 | +<priority>99</priority> |
| 11 | + |
| 12 | +<discussion> |
| 13 | +<p> |
| 14 | +Currently, the following deduction guide of std::mdspan takes the data handle by reference: |
| 15 | +</p> |
| 16 | +<blockquote><pre> |
| 17 | +template<class MappingType, class AccessorType> |
| 18 | + mdspan(const typename AccessorType::data_handle_type&, const MappingType&, |
| 19 | + const AccessorType&) |
| 20 | + -> mdspan<typename AccessorType::element_type, typename MappingType::extents_type, |
| 21 | + typename MappingType::layout_type, AccessorType>; |
| 22 | +</pre></blockquote> |
| 23 | +<p> |
| 24 | +But the corresponding constructor takes the data handle by value: |
| 25 | +</p> |
| 26 | +<blockquote><pre> |
| 27 | +constexpr mdspan(data_handle_type p, const mapping_type& m, const accessor_type& a); |
| 28 | +</pre></blockquote> |
| 29 | +<p> |
| 30 | +The distinction is observable with `volatile` glvalues. E.g., in the following example, |
| 31 | +CTAD fails but explicitly specifying template arguments works |
| 32 | +(<a href="https://godbolt.org/z/fPKb9869M">demo</a>): |
| 33 | +</p> |
| 34 | +<blockquote><pre> |
| 35 | +#include <cstddef> |
| 36 | +#include <mdspan> |
| 37 | + |
| 38 | +int main() { |
| 39 | + int a[1]{}; |
| 40 | + int * volatile p = a; |
| 41 | + std::mdspan( |
| 42 | + p, |
| 43 | + std::layout_right::mapping<std::extents<std::size_t, 1>>{}, |
| 44 | + std::default_accessor<int>{}); // <span style="color:red;font-weight:bolder">error (but accidentally accepted by libc++)</span> |
| 45 | + std::mdspan<int, std::extents<std::size_t, 1>>( |
| 46 | + p, |
| 47 | + std::layout_left::mapping<std::extents<std::size_t, 1>>{}, |
| 48 | + std::default_accessor<int>{}); // OK |
| 49 | +} |
| 50 | +</pre></blockquote> |
| 51 | +<p> |
| 52 | +Given we're generally passing data handle by value, it seems better to remove <tt>const &</tt> from |
| 53 | +<tt>const typename AccessorType::data_handle_type&</tt> in the deduction guide, which is more |
| 54 | +consistent with the constructor and accept more seemingly valid uses. |
| 55 | +<p/> |
| 56 | +Note that libc++ is accidentally doing this now by forgetting adding the <tt>&</tt>, |
| 57 | +see <a href="https://github.com/llvm/llvm-project/pull/175024">llvm/llvm-project#175024</a>. |
| 58 | +</p> |
| 59 | +</discussion> |
| 60 | + |
| 61 | +<resolution> |
| 62 | +<p> |
| 63 | +This wording is relative to <paper num="N5032"/>. |
| 64 | +</p> |
| 65 | + |
| 66 | +<ol> |
| 67 | +<li><p>Modify <sref ref="[mdspan.mdspan.overview]"/>, class template <tt>mdspan</tt> synopsis, as indicated:</p> |
| 68 | + |
| 69 | +<blockquote> |
| 70 | +<pre> |
| 71 | +namespace std { |
| 72 | + […] |
| 73 | + template<class MappingType, class AccessorType> |
| 74 | + mdspan(<del>const</del> typename AccessorType::data_handle_type<del>&</del>, const MappingType&, |
| 75 | + const AccessorType&) |
| 76 | + -> mdspan<typename AccessorType::element_type, typename MappingType::extents_type, |
| 77 | + typename MappingType::layout_type, AccessorType>; |
| 78 | +} |
| 79 | +</pre> |
| 80 | +</blockquote> |
| 81 | +</li> |
| 82 | + |
| 83 | +</ol> |
| 84 | + |
| 85 | + |
| 86 | +</resolution> |
| 87 | + |
| 88 | +</issue> |
0 commit comments