|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4239: flat_map's transparent comparator no longer works for string literals</title> |
| 6 | +<meta property="og:title" content="Issue 4239: flat_map's transparent comparator no longer works for string literals"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4239.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="4239"><a href="lwg-active.html#4239">4239</a>. <code class='backtick'>flat_map</code>'s transparent comparator no longer works for string literals</h3> |
| 66 | +<p><b>Section:</b> 23.6.8.7 <a href="https://wg21.link/flat.map.modifiers">[flat.map.modifiers]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Hui Xie <b>Opened:</b> 2025-03-29 <b>Last modified:</b> 2025-03-29</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View other</b> <a href="lwg-index-open.html#flat.map.modifiers">active issues</a> in [flat.map.modifiers].</p> |
| 71 | +<p><b>View all other</b> <a href="lwg-index.html#flat.map.modifiers">issues</a> in [flat.map.modifiers].</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 | +According to the spec, the following code should hard error |
| 76 | +</p> |
| 77 | +<blockquote><pre> |
| 78 | +std::flat_map<std::string, int, std::less<>> m; |
| 79 | +m.try_emplace("abc", 5); // <span style="color:red;font-weight:bolder">hard error</span> |
| 80 | +</pre></blockquote> |
| 81 | +<p> |
| 82 | +The reason is that we specify in 23.6.8.7 <a href="https://wg21.link/flat.map.modifiers">[flat.map.modifiers]</a> p21 the effect to be |
| 83 | +as if <code class='backtick'>ranges::upper_bound</code> is called. |
| 84 | +</p> |
| 85 | + |
| 86 | +<p> |
| 87 | +<code class='backtick'>ranges::upper_bound</code> requires <code class='backtick'>indirect_strict_weak_order</code>, which requires the comparator to be |
| 88 | +invocable for all combinations. In this case, it requires |
| 89 | +</p> |
| 90 | +<blockquote><pre> |
| 91 | +const char (&)[4] < const char (&)[4] |
| 92 | +</pre></blockquote> |
| 93 | +<p> |
| 94 | +to be well-formed, which is no longer the case in C++26 after <a href="https://wg21.link/P2865R6">P2865R6</a>. |
| 95 | +<p/> |
| 96 | +We should just use <code class='backtick'>std::upper_bound</code> instead. libstdc++ already uses <code class='backtick'>std::upper_bound</code>. |
| 97 | +libc++ uses <code class='backtick'>ranges::upper_bound</code> but clang has not yet implemented <a href="https://wg21.link/P2865" title=" Remove Deprecated Array Comparisons from C++26">P2865</a> |
| 98 | +properly. |
| 99 | +</p> |
| 100 | + |
| 101 | + |
| 102 | +<p id="res-4239"><b>Proposed resolution:</b></p> |
| 103 | +<p> |
| 104 | +This wording is relative to <a href="https://wg21.link/N5008">N5008</a>. |
| 105 | +</p> |
| 106 | + |
| 107 | +<ol> |
| 108 | + |
| 109 | +<li><p>Modify 23.6.8.7 <a href="https://wg21.link/flat.map.modifiers">[flat.map.modifiers]</a> as indicated:</p> |
| 110 | + |
| 111 | +<blockquote> |
| 112 | +<pre> |
| 113 | +template<class K, class... Args> |
| 114 | + constexpr pair<iterator, bool> try_emplace(K&& k, Args&&... args); |
| 115 | +template<class K, class... Args> |
| 116 | + constexpr iterator try_emplace(const_iterator hint, K&& k, Args&&... args); |
| 117 | +</pre> |
| 118 | +<blockquote> |
| 119 | +<p> |
| 120 | +-19- <i>Constraints</i>: […] |
| 121 | +<p/> |
| 122 | +-20- <i>Preconditions</i>: […] |
| 123 | +<p/> |
| 124 | +-21- <i>Effects</i>: If the map already contains an element whose key is equivalent to <code class='backtick'>k</code>, |
| 125 | +<code class='backtick'>*this</code> and <code class='backtick'>args...</code> are unchanged. Otherwise equivalent to: |
| 126 | +</p> |
| 127 | +<blockquote><pre> |
| 128 | +auto key_it = <ins>upper_bound(<i>c</i>.keys.begin(), <i>c</i>.keys.end(), k, <i>compare</i>)</ins><del>ranges::upper_bound(<i>c</i>.keys, k, <i>compare</i>)</del>; |
| 129 | +auto value_it = <i>c</i>.values.begin() + distance(<i>c</i>.keys.begin(), key_it); |
| 130 | +<i>c</i>.keys.emplace(key_it, std::forward<K>(k)); |
| 131 | +<i>c</i>.values.emplace(value_it, std::forward<Args>(args)...); |
| 132 | +</pre></blockquote> |
| 133 | +</blockquote> |
| 134 | +</blockquote> |
| 135 | + |
| 136 | +</li> |
| 137 | + |
| 138 | +</ol> |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +</body> |
| 145 | +</html> |
0 commit comments