[gcc r15-11191] libstdc++: Fix incorrect move in flat_map::_M_try_emplace [PR125374]
Patrick Palka
ppalka@gcc.gnu.org
Thu May 21 17:20:28 GMT 2026
https://gcc.gnu.org/g:c9cc091338b13835afbb0f5d68bfde80da172fd2
commit r15-11191-gc9cc091338b13835afbb0f5d68bfde80da172fd2
Author: Patrick Palka <ppalka@redhat.com>
Date: Tue May 19 13:43:10 2026 -0400
libstdc++: Fix incorrect move in flat_map::_M_try_emplace [PR125374]
PR libstdc++/125374
libstdc++-v3/ChangeLog:
* include/std/flat_map (_Flat_map_impl::_M_try_emplace): Forward
instead of unconditionally moving __k when inserting it.
* testsuite/23_containers/flat_map/1.cc (test10): New test.
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
(cherry picked from commit 6d114645fa1c9831a109c8a8934685f49364818a)
Diff:
---
libstdc++-v3/include/std/flat_map | 2 +-
libstdc++-v3/testsuite/23_containers/flat_map/1.cc | 16 ++++++++++++++++
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/flat_map b/libstdc++-v3/include/std/flat_map
index e48c3eae07fd..9956738ea630 100644
--- a/libstdc++-v3/include/std/flat_map
+++ b/libstdc++-v3/include/std/flat_map
@@ -482,7 +482,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return {iterator{this, __key_it}, false};
auto __guard = _M_make_clear_guard();
- __key_it = _M_cont.keys.insert(__key_it, std::move(__k));
+ __key_it = _M_cont.keys.insert(__key_it, std::forward<_Key2>(__k));
__value_it = _M_cont.values.begin() + (__key_it - _M_cont.keys.begin());
_M_cont.values.emplace(__value_it, std::forward<_Args>(__args)...);
__guard._M_disable();
diff --git a/libstdc++-v3/testsuite/23_containers/flat_map/1.cc b/libstdc++-v3/testsuite/23_containers/flat_map/1.cc
index 2f4ffa2d1bd4..f762a64797fb 100644
--- a/libstdc++-v3/testsuite/23_containers/flat_map/1.cc
+++ b/libstdc++-v3/testsuite/23_containers/flat_map/1.cc
@@ -12,6 +12,7 @@
#include <testsuite_allocator.h>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
+#include <string>
#include <tuple>
struct Gt {
@@ -272,6 +273,20 @@ test09()
using value_type = std::pair<int, int>;
}
+void
+test10()
+{
+ // PR libstdc++/125374 - flat_map unconditionally moves from lvalue keys in
+ // _M_try_emplace
+ std::flat_map<std::string, int, std::less<>> m;
+ std::string k = "hello";
+ m[k] = 1;
+ VERIFY (k == "hello");
+ k = "world";
+ m.try_emplace(k, 2);
+ VERIFY (k == "world");
+}
+
int
main()
{
@@ -287,4 +302,5 @@ main()
test07();
test08();
test09();
+ test10();
}
More information about the Libstdc++-cvs
mailing list