|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4537" status="New"> |
| 5 | +<title>Improve `define_static_array`</title> |
| 6 | +<section> |
| 7 | +<sref ref="[meta.define.static]"/> |
| 8 | +</section> |
| 9 | +<submitter>Hewill Kang</submitter> |
| 10 | +<date>09 Mar 2026</date> |
| 11 | +<priority>99</priority> |
| 12 | + |
| 13 | +<discussion> |
| 14 | +<p> |
| 15 | +`define_static_array` can transform any input range, even a non-contiguous one, into a `span`. |
| 16 | +<p/> |
| 17 | +However, the returned `span` always has a dynamic extent. If the size of the input range is known at |
| 18 | +compile time, returning a static `span` seems reasonable and may offer slight runtime efficiency. |
| 19 | +</p> |
| 20 | +</discussion> |
| 21 | + |
| 22 | +<resolution> |
| 23 | +<p> |
| 24 | +This wording is relative to <paper num="N5032"/>. |
| 25 | +</p> |
| 26 | + |
| 27 | +<ol> |
| 28 | + |
| 29 | +<li><p>Modify <sref ref="[meta.syn]"/>, header <tt><meta></tt> synopsis, as indicated:</p> |
| 30 | + |
| 31 | +<blockquote> |
| 32 | +<pre> |
| 33 | +#include <compare> // <i>see <sref ref="[compare.syn]"/></i> |
| 34 | +#include <initializer_list> // <i>see <sref ref="[initializer.list.syn]"/></i> |
| 35 | + |
| 36 | +namespace std { |
| 37 | + […] |
| 38 | + // <i><sref ref="[meta.define.static]"/>, promoting to static storage</i> |
| 39 | + […] |
| 40 | + template<ranges::input_range R> |
| 41 | + consteval span<const ranges::range_value_t<R><ins>, <i>see below</i></ins>> define_static_array(R&& r); |
| 42 | + […] |
| 43 | +} |
| 44 | +</pre> |
| 45 | +</blockquote> |
| 46 | +</li> |
| 47 | + |
| 48 | +<li><p>Modify <sref ref="[meta.define.static]"/> as indicated:</p> |
| 49 | + |
| 50 | +<blockquote> |
| 51 | +<pre> |
| 52 | +template<ranges::input_range R> |
| 53 | + consteval span<const ranges::range_value_t<R><ins>, <i>see below</i></ins>> define_static_array(R&& r); |
| 54 | +</pre> |
| 55 | +<blockquote> |
| 56 | +<p> |
| 57 | +-16- <i>Effects</i>: Equivalent to: |
| 58 | +</p> |
| 59 | +<blockquote> |
| 60 | +<pre> |
| 61 | +using T = ranges::range_value_t<R>; |
| 62 | +meta::info array = meta::reflect_constant_array(r); |
| 63 | +if (meta::is_array_type(meta::type_of(array))) { |
| 64 | + return span<const T<ins>, <i>see below</i></ins>>(meta::extract<const T*>(array), meta::extent(meta::type_of(array))); |
| 65 | +} else { |
| 66 | + return span<const T<ins>, <i>see below</i></ins>>(); |
| 67 | +} |
| 68 | +</pre> |
| 69 | +</blockquote> |
| 70 | +<p> |
| 71 | +<ins>-?- <i>Remarks</i>: The second template argument of the returned `span` type is |
| 72 | +<code>static_cast<size_t>(ranges::size(r))</code> if `ranges::size(r)` is a constant |
| 73 | +expression, and `dynamic_extent` otherwise.</ins> |
| 74 | +</p> |
| 75 | +</blockquote> |
| 76 | +</blockquote> |
| 77 | +</li> |
| 78 | + |
| 79 | +</ol> |
| 80 | +</resolution> |
| 81 | + |
| 82 | +</issue> |
0 commit comments