|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4223: Deduction guides for maps are mishandling tuples and references</title> |
| 6 | +<meta property="og:title" content="Issue 4223: Deduction guides for maps are mishandling tuples and references"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4223.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="4223"><a href="lwg-active.html#4223">4223</a>. Deduction guides for maps are mishandling tuples and references</h3> |
| 66 | +<p><b>Section:</b> 23.4.1 <a href="https://wg21.link/associative.general">[associative.general]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Tomasz Kaminski <b>Opened:</b> 2025-03-14 <b>Last modified:</b> 2025-03-15</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 71 | +<p><b>Discussion:</b></p> |
| 72 | +<p> |
| 73 | +The <code class='backtick'>from_range</code> deduction guide for maps currently do not handle ranges of tuple of two elements: |
| 74 | +</p> |
| 75 | +<blockquote><pre> |
| 76 | +std::vector<int> v; |
| 77 | +auto zv = std::views::zip(v, v); |
| 78 | +std::map m4(std::from_range, zv); // <span style="color:red;font-weight:bolder">Ill-formed, no-deduction guide</span> |
| 79 | +</pre></blockquote> |
| 80 | +<p> |
| 81 | +This seems to be result of merge conflict between <a href="https://wg21.link/P2165" title=" Compatibility between tuple, pair and tuple-like objects">P2165</a> (Compatibility between tuple, pair and tuple-like objects) |
| 82 | +and <a href="https://wg21.link/P1206R4" title=" Conversions from ranges to containers">P1206R4</a> (Conversions from ranges to containers): The helper <code><i>range-key-type</i></code> and |
| 83 | +<code><i>range-mapped-type</i></code> aliases introduced by the later use the old formulation of <code class='backtick'>::first_type</code>, |
| 84 | +<code class='backtick'>::second::type</code> instead of <code class='backtick'>tuple_element</code>. |
| 85 | +<p/> |
| 86 | +Furthermore, both iterator and range deduction guides do not correctly handle iterators with a pair of references as |
| 87 | +value types, and deduce key or value type as reference, which is ill-formed: |
| 88 | +</p> |
| 89 | +<blockquote><pre> |
| 90 | +std::flat_map<int, float> fm; // iterator value_type is pair<int, float> |
| 91 | +std::map m1(fm.begin(), fm.end()); // OK, deduces std::map<int, float> |
| 92 | + |
| 93 | +auto tv = fm | std::views::transform(std::identity{}); // iterator value value_type is pair<int const&, float const&> |
| 94 | +std::map m3(tv.begin(), tv.end()); // <span style="color:red;font-weight:bolder">Ill-formed, deduces std::map<int const&, float&></span> |
| 95 | +</pre></blockquote> |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +<p id="res-4223"><b>Proposed resolution:</b></p> |
| 100 | +<p> |
| 101 | +This wording is relative to <a href="https://wg21.link/N5001">N5001</a>. |
| 102 | +</p> |
| 103 | + |
| 104 | +<blockquote class="note"> |
| 105 | +<p> |
| 106 | +[<i>Drafting note</i>: The proposed change also strips <code class='backtick'>const</code> from the value type of the <code class='backtick'>map</code>, |
| 107 | +changing the behavior of previously working code: |
| 108 | +</p> |
| 109 | +<blockquote><pre> |
| 110 | +std::pair<int const, float const> tp[2]; |
| 111 | +std::map m(std::begin(tp), std::end(tp)); // Was std::map<int, float const>, now std::map<int, float> |
| 112 | +</pre></blockquote> |
| 113 | +] |
| 114 | +</blockquote> |
| 115 | + |
| 116 | +<ol> |
| 117 | + |
| 118 | +<li><p>Modify 23.4.1 <a href="https://wg21.link/associative.general">[associative.general]</a> as indicated:</p> |
| 119 | + |
| 120 | +<blockquote> |
| 121 | +<pre> |
| 122 | +template<class InputIterator> |
| 123 | + using <i>iter-value-type</i> = |
| 124 | + typename iterator_traits<InputIterator>::value_type; // <i>exposition only</i> |
| 125 | + |
| 126 | +template<class InputIterator> |
| 127 | + using <i>iter-key-type</i> = <del>remove_const_t</del><ins>remove_cvref_t</ins>< |
| 128 | + tuple_element_t<0, <i>iter-value-type</i><InputIterator>>>; // <i>exposition only</i> |
| 129 | + |
| 130 | +template<class InputIterator> |
| 131 | + using <i>iter-mapped-type</i> = <ins>remove_cvref_t<</ins> |
| 132 | + tuple_element_t<1, <i>iter-value-type</i><InputIterator>><ins>></ins>; // <i>exposition only</i> |
| 133 | + |
| 134 | +template<class InputIterator> |
| 135 | + using <i>iter-to-alloc-type</i> = pair< |
| 136 | + add_const_t< |
| 137 | + <del>tuple_element_t<0, <i>iter-value-type</i><InputIterator>></del> |
| 138 | + <ins><i>iter-key-type</i><InputIterator></ins> |
| 139 | + >, |
| 140 | + <del>tuple_element_t<1, <i>iter-value-type</i><InputIterator>></del> |
| 141 | + <ins><i>iter-mapped-type</i><InputIterator></ins> |
| 142 | + >; // <i>exposition only</i> |
| 143 | + |
| 144 | +template<ranges::input_range Range> |
| 145 | + using <i>range-key-type</i> = |
| 146 | + <del>remove_const_t<typename ranges::range_value_t<Range>::first_type></del> |
| 147 | + <ins>remove_cvref_t<tuple_element_t<0, ranges::range_value_t<Range>>></ins>; // <i>exposition only</i> |
| 148 | + |
| 149 | +template<ranges::input_range Range> |
| 150 | + using <i>range-mapped-type</i> = |
| 151 | + <del>typename ranges::range_value_t<Range>::second_type</del> |
| 152 | + <ins>remove_cvref_t<tuple_element_t<1, ranges::range_value_t<Range>>></ins>; // <i>exposition only</i> |
| 153 | + |
| 154 | +template<ranges::input_range Range> |
| 155 | + using <i>range-to-alloc-type</i> = |
| 156 | + pair<add_const_t< |
| 157 | + <del>typename ranges::range_value_t<Range>::first_type</del> |
| 158 | + <ins><i>range-key-type</i><Range></ins> |
| 159 | + >, |
| 160 | + <del>typename ranges::range_value_t<Range>::second_type</del> |
| 161 | + <ins><i>range-mapped-type</i><Range></ins> |
| 162 | + >; // <i>exposition only</i> |
| 163 | +</pre> |
| 164 | +</blockquote> |
| 165 | +</li> |
| 166 | +</ol> |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +</body> |
| 173 | +</html> |
0 commit comments