|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4241: ranges::for_each(_n) should be less constrained</title> |
| 6 | +<meta property="og:title" content="Issue 4241: ranges::for_each(_n) should be less constrained"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4241.html"> |
| 9 | +<meta property="og:type" content="website"> |
| 10 | +<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png"> |
| 11 | +<meta property="og:image:alt" content="C++ logo"> |
| 12 | +<style> |
| 13 | + p {text-align:justify} |
| 14 | + li {text-align:justify} |
| 15 | + pre code.backtick::before { content: "`" } |
| 16 | + pre code.backtick::after { content: "`" } |
| 17 | + blockquote.note |
| 18 | + { |
| 19 | + background-color:#E0E0E0; |
| 20 | + padding-left: 15px; |
| 21 | + padding-right: 15px; |
| 22 | + padding-top: 1px; |
| 23 | + padding-bottom: 1px; |
| 24 | + } |
| 25 | + ins {background-color:#A0FFA0} |
| 26 | + del {background-color:#FFA0A0} |
| 27 | + table.issues-index { border: 1px solid; border-collapse: collapse; } |
| 28 | + table.issues-index th { text-align: center; padding: 4px; border: 1px solid; } |
| 29 | + table.issues-index td { padding: 4px; border: 1px solid; } |
| 30 | + table.issues-index td:nth-child(1) { text-align: right; } |
| 31 | + table.issues-index td:nth-child(2) { text-align: left; } |
| 32 | + table.issues-index td:nth-child(3) { text-align: left; } |
| 33 | + table.issues-index td:nth-child(4) { text-align: left; } |
| 34 | + table.issues-index td:nth-child(5) { text-align: center; } |
| 35 | + table.issues-index td:nth-child(6) { text-align: center; } |
| 36 | + table.issues-index td:nth-child(7) { text-align: left; } |
| 37 | + table.issues-index td:nth-child(5) span.no-pr { color: red; } |
| 38 | + @media (prefers-color-scheme: dark) { |
| 39 | + html { |
| 40 | + color: #ddd; |
| 41 | + background-color: black; |
| 42 | + } |
| 43 | + ins { |
| 44 | + background-color: #225522 |
| 45 | + } |
| 46 | + del { |
| 47 | + background-color: #662222 |
| 48 | + } |
| 49 | + a { |
| 50 | + color: #6af |
| 51 | + } |
| 52 | + a:visited { |
| 53 | + color: #6af |
| 54 | + } |
| 55 | + blockquote.note |
| 56 | + { |
| 57 | + background-color: rgba(255, 255, 255, .10) |
| 58 | + } |
| 59 | + } |
| 60 | +</style> |
| 61 | +</head> |
| 62 | +<body> |
| 63 | +<hr> |
| 64 | +<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> |
| 65 | +<h3 id="4241"><a href="lwg-active.html#4241">4241</a>. <code class='backtick'>ranges::for_each(_n)</code> should be less constrained</h3> |
| 66 | +<p><b>Section:</b> 26.6.5 <a href="https://wg21.link/alg.foreach">[alg.foreach]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Jiang An <b>Opened:</b> 2025-04-08 <b>Last modified:</b> 2025-04-13</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View other</b> <a href="lwg-index-open.html#alg.foreach">active issues</a> in [alg.foreach].</p> |
| 71 | +<p><b>View all other</b> <a href="lwg-index.html#alg.foreach">issues</a> in [alg.foreach].</p> |
| 72 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 73 | +<p><b>Discussion:</b></p> |
| 74 | +<p> |
| 75 | +Currently, <code class='backtick'>ranges::for_each(_n)</code> are constrained with <code class='backtick'>indirectly_unary_invocable</code>, |
| 76 | +which doesn't meet the actual use of the range elements. These algorithms are only |
| 77 | +expected to invoke the callable object with the possibly projected elements, and not |
| 78 | +to use any element as the value type. Moreover, <code class='backtick'>indirectly_unary_invocable</code> requires |
| 79 | +the callable object to be copy constructible, which might be undesired because the |
| 80 | +corresponding <code class='backtick'>std::for_each(_n)</code> only require move constructibilty. |
| 81 | +<p/> |
| 82 | +LWG <a href="lwg-active.html#4171" title="P2609R3 breaks code that uses views::zip and get<T> (Status: New)">4171</a><sup><a href="https://cplusplus.github.io/LWG/issue4171" title="Latest snapshot">(i)</a></sup> talked about the breakage around <code class='backtick'>ranges::for_each</code> introduced |
| 83 | +by <a href="https://wg21.link/P2609R3" title=" Relaxing Ranges Just A Smidge">P2609R3</a>. P2609R3 looks like a reasonable fix as long as the affected |
| 84 | +algorithms potentially use the intermediate element values copied as |
| 85 | +<code>std::iter_value_t<I></code>. However, when the algorithm is not expected to or |
| 86 | +even required not to do this, P2609R3 can bring unexpected impacts. It seems that |
| 87 | +constraints around <code class='backtick'>iter_value_t</code> should be avoided for such an algorithm. |
| 88 | +</p> |
| 89 | + |
| 90 | + |
| 91 | +<p id="res-4241"><b>Proposed resolution:</b></p> |
| 92 | +<p> |
| 93 | +This wording is relative to <a href="https://wg21.link/N5008">N5008</a>. |
| 94 | +</p> |
| 95 | + |
| 96 | +<ol> |
| 97 | + |
| 98 | +<li><p>Modify 26.4 <a href="https://wg21.link/algorithm.syn">[algorithm.syn]</a>, header <code><algorithm></code> synopsis, as indicated:</p> |
| 99 | + |
| 100 | +<blockquote> |
| 101 | +<pre> |
| 102 | +[…] |
| 103 | +namespace ranges { |
| 104 | + template<class I, class F> |
| 105 | + using for_each_result = in_fun_result<I, F>; |
| 106 | + |
| 107 | + template<input_iterator I, sentinel_for<I> S, class Proj = identity, |
| 108 | + <del>indirectly_unary_invocable<projected<I, Proj>></del><ins>move_constructible</ins> Fun> |
| 109 | + <ins>requires invocable<Fun&, iter_reference_t<projected<I, Proj>>></ins> |
| 110 | + constexpr for_each_result<I, Fun> |
| 111 | + for_each(I first, S last, Fun f, Proj proj = {}); |
| 112 | + template<input_range R, class Proj = identity, |
| 113 | + <del>indirectly_unary_invocable<projected<iterator_t<R>, Proj>></del><ins>move_constructible</ins> Fun> |
| 114 | + <ins>requires invocable<Fun&, iter_reference_t<projected<iterator_t<R>, Proj>>></ins> |
| 115 | + constexpr for_each_result<borrowed_iterator_t<R>, Fun> |
| 116 | + for_each(R&& r, Fun f, Proj proj = {}); |
| 117 | +} |
| 118 | +[…] |
| 119 | +namespace ranges { |
| 120 | + template<class I, class F> |
| 121 | + using for_each_n_result = in_fun_result<I, F>; |
| 122 | + |
| 123 | + template<input_iterator I, class Proj = identity, |
| 124 | + <del>indirectly_unary_invocable<projected<I, Proj>></del><ins>move_constructible</ins> Fun> |
| 125 | + <ins>requires invocable<Fun&, iter_reference_t<projected<I, Proj>>></ins> |
| 126 | + constexpr for_each_n_result<I, Fun> |
| 127 | + for_each_n(I first, iter_difference_t<I> n, Fun f, Proj proj = {}); |
| 128 | +} |
| 129 | +[…] |
| 130 | +</pre> |
| 131 | +</blockquote> |
| 132 | + |
| 133 | +</li> |
| 134 | + |
| 135 | +<li><p>Modify 26.6.5 <a href="https://wg21.link/alg.foreach">[alg.foreach]</a> as indicated:</p> |
| 136 | + |
| 137 | +<blockquote> |
| 138 | +<pre> |
| 139 | +template<input_iterator I, sentinel_for<I> S, class Proj = identity, |
| 140 | + <del>indirectly_unary_invocable<projected<I, Proj>></del><ins>move_constructible</ins> Fun> |
| 141 | + <ins>requires invocable<Fun&, iter_reference_t<projected<I, Proj>>></ins> |
| 142 | + constexpr ranges::for_each_result<I, Fun> |
| 143 | + ranges::for_each(I first, S last, Fun f, Proj proj = {}); |
| 144 | +template<input_range R, class Proj = identity, |
| 145 | + <del>indirectly_unary_invocable<projected<iterator_t<R>, Proj>></del><ins>move_constructible</ins> Fun> |
| 146 | + <ins>requires invocable<Fun&, iter_reference_t<projected<iterator_t>R>, Proj>>></ins> |
| 147 | + constexpr ranges::for_each_result<borrowed_iterator_t<R>, Fun> |
| 148 | + ranges::for_each(R&& r, Fun f, Proj proj = {}); |
| 149 | +</pre> |
| 150 | +<blockquote> |
| 151 | +<p> |
| 152 | +[…] |
| 153 | +<p/> |
| 154 | +<del>-15- [<i>Note 6</i>: The overloads in namespace <code class='backtick'>ranges</code> require <code class='backtick'>Fun</code> to model <code class='backtick'>copy_constructible</code>. — <i>end note</i>]</del> |
| 155 | +</p> |
| 156 | +</blockquote> |
| 157 | +<p> |
| 158 | +[…] |
| 159 | +</p> |
| 160 | +<pre> |
| 161 | +template<input_iterator I, class Proj = identity, |
| 162 | + <del>indirectly_unary_invocable<projected<I, Proj>></del><ins>move_constructible</ins> Fun> |
| 163 | + <ins>requires invocable<Fun&, iter_reference_t<projected<I, Proj>>></ins> |
| 164 | + constexpr ranges::for_each_n_result<I, Fun> |
| 165 | + ranges::for_each_n(I first, iter_difference_t<I> n, Fun f, Proj proj = {}); |
| 166 | +</pre> |
| 167 | +<blockquote> |
| 168 | +<p> |
| 169 | +[…] |
| 170 | +<p/> |
| 171 | +<del>-30- [<i>Note 11</i>: The overload in namespace <code class='backtick'>ranges</code> requires <code class='backtick'>Fun</code> to model <code class='backtick'>copy_constructible</code>. — <i>end note</i>]</del> |
| 172 | +</p> |
| 173 | +</blockquote> |
| 174 | +</blockquote> |
| 175 | + |
| 176 | +</li> |
| 177 | + |
| 178 | +</ol> |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +</body> |
| 185 | +</html> |
0 commit comments