On 11/19/25 14:51, Jonathan Wakely wrote:
On Tue, 18 Nov 2025 at 21:38, François Dumont<[email protected]> wrote:
Ok, here is the new patch, only fixing thestd::erase_if behavior for
the _GLIBCXX_DEBUG.
Is that right? I thought this change is to benefit direct uses of
__gnu_debug::vector notstd::vector with _GLIBCXX_DEBUG? The first new
test already passes when usingstd::vector with -D_GLIBCXX_DEBUG. In
fact, it FAILs with this patch! So this change is not helping
_GLIBCXX_DEBUG, it's making it worse.
I see the problem there, you need to check __glibcxx_erase_if in
<debug/vector>, not __cpp_lib_erase_if. The latter hasn't been defined
yet because <debug/vector> gets included before
__glibcxx_want_erase_if is defined in <vector>. You probably won't see
the FAILs for a default configuration of GCC, but if you configure
with --disable-libstdcxx-pch then it will fail.
Caught again by pch, I wonder if those should not be disabled per
default when running tests.
I did this for the 2 new tests.
Also, what is the commit message for this patch?
The original commit message from
https://gcc.gnu.org/pipermail/gcc-patches/2025-November/701007.html no
longer applies, because this is a different patch (there's no clean up
being done ... maybe everything else in the original still applies?)
Yes, I kept same message, it was not talking about modification of
std::erase_if implementation as I replicate the implementation I had put
in place for __gnu_debug::inplace_vector instead of replicating the one
for std::vector. At least now there is a test to detect this wrong change.
OK for trunk with both uses of __cpp_lib_erase_if in <debug/vector>
fixed, and an updated commit message.
Attached patch committed.
diff --git a/libstdc++-v3/include/debug/vector
b/libstdc++-v3/include/debug/vector
index 1b3486b0dc0..f952fe9255b 100644
--- a/libstdc++-v3/include/debug/vector
+++ b/libstdc++-v3/include/debug/vector
@@ -1038,6 +1038,32 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
} // namespace __detail::__variant
#endif // C++17
+#ifdef __glibcxx_erase_if // C++ >= 20 && HOSTED
+ template<typename _Tp, typename _Alloc, typename _Predicate>
+ constexpr typename __debug::vector<_Tp, _Alloc>::size_type
+ erase_if(__debug::vector<_Tp, _Alloc>& __cont, _Predicate __pred)
+ {
+ _GLIBCXX_STD_C::vector<_Tp, _Alloc>& __unsafe_cont = __cont;
+ const auto __osz = __cont.size();
+ const auto __end = __unsafe_cont.end();
+ auto __removed = std::__remove_if(__unsafe_cont.begin(), __end,
+ std::move(__pred));
+ if (__removed != __end)
+ {
+ __cont.erase(__niter_wrap(__cont.begin(), __removed),
+ __cont.end());
+ return __osz - __cont.size();
+ }
+
+ return 0;
+ }
+
+ template<typename _Tp, typename _Alloc, typename _Up = _Tp>
+ constexpr typename __debug::vector<_Tp, _Alloc>::size_type
+ erase(__debug::vector<_Tp, _Alloc>& __cont, const _Up& __value)
+ { return std::erase_if(__cont, __gnu_cxx::__ops::__equal_to(__value)); }
+#endif // __glibcxx_erase_if
+
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/include/std/vector b/libstdc++-v3/include/std/vector
index 920f852f794..375011fff69 100644
--- a/libstdc++-v3/include/std/vector
+++ b/libstdc++-v3/include/std/vector
@@ -112,19 +112,16 @@ namespace std _GLIBCXX_VISIBILITY(default)
_GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp, typename _Alloc, typename _Predicate>
- constexpr typename vector<_Tp, _Alloc>::size_type
- erase_if(vector<_Tp, _Alloc>& __cont, _Predicate __pred)
+ constexpr typename _GLIBCXX_STD_C::vector<_Tp, _Alloc>::size_type
+ erase_if(_GLIBCXX_STD_C::vector<_Tp, _Alloc>& __cont, _Predicate __pred)
{
- using namespace __gnu_cxx;
- _GLIBCXX_STD_C::vector<_Tp, _Alloc>& __ucont = __cont;
const auto __osz = __cont.size();
- const auto __end = __ucont.end();
- auto __removed = std::__remove_if(__ucont.begin(), __end,
+ const auto __end = __cont.end();
+ auto __removed = std::__remove_if(__cont.begin(), __end,
std::move(__pred));
if (__removed != __end)
{
- __cont.erase(__niter_wrap(__cont.begin(), __removed),
- __cont.end());
+ __cont.erase(__removed, __end);
return __osz - __cont.size();
}
@@ -133,8 +130,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp, typename _Alloc,
typename _Up _GLIBCXX26_DEF_VAL_T(_Tp)>
- constexpr typename vector<_Tp, _Alloc>::size_type
- erase(vector<_Tp, _Alloc>& __cont, const _Up& __value)
+ constexpr typename _GLIBCXX_STD_C::vector<_Tp, _Alloc>::size_type
+ erase(_GLIBCXX_STD_C::vector<_Tp, _Alloc>& __cont, const _Up& __value)
{ return std::erase_if(__cont, __gnu_cxx::__ops::__equal_to(__value)); }
_GLIBCXX_END_NAMESPACE_VERSION
diff --git a/libstdc++-v3/testsuite/23_containers/vector/debug/erase.cc
b/libstdc++-v3/testsuite/23_containers/vector/debug/erase.cc
new file mode 100644
index 00000000000..88ec5479464
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/debug/erase.cc
@@ -0,0 +1,28 @@
+// { dg-do run { target c++20 } }
+// { dg-add-options no_pch }
+// { dg-require-debug-mode "" }
+
+#include <vector>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+ std::vector<int> v;
+
+ for (int i = 0; i != 10; ++i)
+ v.push_back(i);
+
+ auto before = v.begin() + 4;
+ auto last = v.end() - 1;
+
+ VERIFY( std::erase(v, 6) == 1 );
+
+ VERIFY(before._M_dereferenceable());
+ VERIFY(last._M_singular());
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
diff --git
a/libstdc++-v3/testsuite/23_containers/vector/debug/invalidation/erase.cc
b/libstdc++-v3/testsuite/23_containers/vector/debug/invalidation/erase.cc
new file mode 100644
index 00000000000..e7dec568c8e
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/debug/invalidation/erase.cc
@@ -0,0 +1,29 @@
+// { dg-do run { target c++20 } }
+// { dg-add-options no_pch }
+
+#include <debug/vector>
+#include <testsuite_hooks.h>
+
+using __gnu_debug::vector;
+
+void test01()
+{
+ vector<int> v;
+
+ for (int i = 0; i != 10; ++i)
+ v.push_back(i);
+
+ auto before = v.begin() + 4;
+ auto last = v.end() -1;
+
+ VERIFY( std::erase(v, 6) == 1 );
+
+ VERIFY(before._M_dereferenceable());
+ VERIFY(last._M_singular());
+}
+
+int main()
+{
+ test01();
+ return 0;
+}