[committed] libstdc++: Fix std::vector<bool>::emplace to forward parameter
Jonathan Wakely
jwakely@redhat.com
Sun Oct 27 20:06:38 GMT 2024
If the parameter is not lvalue-convertible to bool then the current code
will fail to compile. The parameter should be forwarded to restore the
original value category.
libstdc++-v3/ChangeLog:
* include/bits/stl_bvector.h (emplace_back, emplace): Forward
parameter pack to preserve value category.
* testsuite/23_containers/vector/bool/emplace_rvalue.cc: New
test.
---
Tested x86_64-linux. Pushed to trunk.
libstdc++-v3/include/bits/stl_bvector.h | 4 ++--
.../vector/bool/emplace_rvalue.cc | 24 +++++++++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
create mode 100644 libstdc++-v3/testsuite/23_containers/vector/bool/emplace_rvalue.cc
diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
index 42261ac5915..70f69b5b5b5 100644
--- a/libstdc++-v3/include/bits/stl_bvector.h
+++ b/libstdc++-v3/include/bits/stl_bvector.h
@@ -1343,7 +1343,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
#endif
emplace_back(_Args&&... __args)
{
- push_back(bool(__args...));
+ push_back(bool(std::forward<_Args>(__args)...));
#if __cplusplus > 201402L
return back();
#endif
@@ -1353,7 +1353,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
_GLIBCXX20_CONSTEXPR
iterator
emplace(const_iterator __pos, _Args&&... __args)
- { return insert(__pos, bool(__args...)); }
+ { return insert(__pos, bool(std::forward<_Args>(__args)...)); }
#endif
protected:
diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/emplace_rvalue.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/emplace_rvalue.cc
new file mode 100644
index 00000000000..5dea2426d60
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/emplace_rvalue.cc
@@ -0,0 +1,24 @@
+// { dg-do compile { target c++11 } }
+
+#include <vector>
+
+struct S
+{
+ explicit operator bool() &&;
+};
+
+void
+test_emplace_back()
+{
+ S s;
+ std::vector<bool> v;
+ v.emplace_back(std::move(s));
+}
+
+void
+test_emplace()
+{
+ S s;
+ std::vector<bool> v;
+ v.emplace(v.begin(), std::move(s));
+}
--
2.47.0
More information about the Libstdc++
mailing list