-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Add missed constexpr
to erase(_if)
in <string>
#129666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc++] Add missed constexpr
to erase(_if)
in <string>
#129666
Conversation
@llvm/pr-subscribers-libcxx Author: A. Jiang (frederick-vs-ja) Changes
This patch implements the missed changes that were not tracked in a specific paper. Full diff: https://github.com/llvm/llvm-project/pull/129666.diff 4 Files Affected:
diff --git a/libcxx/docs/ReleaseNotes/21.rst b/libcxx/docs/ReleaseNotes/21.rst
index e7cfa625a132c..663514ce36681 100644
--- a/libcxx/docs/ReleaseNotes/21.rst
+++ b/libcxx/docs/ReleaseNotes/21.rst
@@ -52,6 +52,8 @@ Improvements and New Features
- Updated formatting library to Unicode 16.0.0.
+- ``std::erase`` and ``std::erase_if`` overloads for ``std::basic_string`` are made ``constexpr`` in C++20 and later.
+
Deprecations and Removals
-------------------------
diff --git a/libcxx/include/string b/libcxx/include/string
index 419e3eee6746e..8cbd859e70e73 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -516,10 +516,10 @@ basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
template<class charT, class traits, class Allocator, class U>
-typename basic_string<charT, traits, Allocator>::size_type
+constexpr typename basic_string<charT, traits, Allocator>::size_type
erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
template<class charT, class traits, class Allocator, class Predicate>
-typename basic_string<charT, traits, Allocator>::size_type
+constexpr typename basic_string<charT, traits, Allocator>::size_type
erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
typedef basic_string<char> string;
@@ -4022,7 +4022,7 @@ getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Al
# if _LIBCPP_STD_VER >= 20
template <class _CharT, class _Traits, class _Allocator, class _Up>
-inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type
+_LIBCPP_HIDE_FROM_ABI inline constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type
erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
auto __old_size = __str.size();
__str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
@@ -4030,7 +4030,7 @@ erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
}
template <class _CharT, class _Traits, class _Allocator, class _Predicate>
-inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type
+_LIBCPP_HIDE_FROM_ABI inline constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type
erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred) {
auto __old_size = __str.size();
__str.erase(std::remove_if(__str.begin(), __str.end(), __pred), __str.end());
diff --git a/libcxx/test/std/strings/strings.erasure/erase.pass.cpp b/libcxx/test/std/strings/strings.erasure/erase.pass.cpp
index 35139dd6ad407..a5a542b40c52a 100644
--- a/libcxx/test/std/strings/strings.erasure/erase.pass.cpp
+++ b/libcxx/test/std/strings/strings.erasure/erase.pass.cpp
@@ -11,7 +11,7 @@
// <string>
// template <class charT, class traits, class Allocator, class U>
-// typename basic_string<charT, traits, Allocator>::size_type
+// constexpr typename basic_string<charT, traits, Allocator>::size_type
// erase(basic_string<charT, traits, Allocator>& c, const U& value);
#include <string>
@@ -22,7 +22,7 @@
#include "min_allocator.h"
template <class S, class U>
-void test0(S s, U val, S expected, std::size_t expected_erased_count) {
+constexpr void test0(S s, U val, S expected, std::size_t expected_erased_count) {
ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase(s, val)));
assert(expected_erased_count == std::erase(s, val));
LIBCPP_ASSERT(s.__invariants());
@@ -30,7 +30,7 @@ void test0(S s, U val, S expected, std::size_t expected_erased_count) {
}
template <class S>
-void test() {
+constexpr void test() {
test0(S(""), 'a', S(""), 0);
test0(S("a"), 'a', S(""), 1);
@@ -64,10 +64,17 @@ void test() {
test0(S("aba"), opt('c'), S("aba"), 0);
}
-int main(int, char**) {
+constexpr bool test() {
test<std::string>();
test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>>();
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
return 0;
}
diff --git a/libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp b/libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp
index 5bedd394ee578..0f1c3fcf675eb 100644
--- a/libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp
+++ b/libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp
@@ -11,7 +11,7 @@
// <string>
// template <class charT, class traits, class Allocator, class Predicate>
-// typename basic_string<charT, traits, Allocator>::size_type
+// constexpr typename basic_string<charT, traits, Allocator>::size_type
// erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred);
#include <string>
@@ -21,7 +21,7 @@
#include "min_allocator.h"
template <class S, class Pred>
-void test0(S s, Pred p, S expected, std::size_t expected_erased_count) {
+constexpr void test0(S s, Pred p, S expected, std::size_t expected_erased_count) {
ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase_if(s, p)));
assert(expected_erased_count == std::erase_if(s, p));
LIBCPP_ASSERT(s.__invariants());
@@ -29,7 +29,7 @@ void test0(S s, Pred p, S expected, std::size_t expected_erased_count) {
}
template <typename S>
-void test() {
+constexpr void test() {
auto isA = [](auto ch) { return ch == 'a'; };
auto isB = [](auto ch) { return ch == 'b'; };
auto isC = [](auto ch) { return ch == 'c'; };
@@ -66,10 +66,17 @@ void test() {
test0(S("aba"), True, S(""), 3);
}
-int main(int, char**) {
+constexpr bool test() {
test<std::string>();
test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>>();
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
return 0;
}
|
`std::erase(_if)` for `basic_string` were made `constexpr` in C++20 by cplusplus/draft@2c1ab97 as follow-up changes of P0980R1. This patch implements the missed changes that were not tracked in a specific paper.
bf96e93
to
073d679
Compare
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/186/builds/7039 Here is the relevant piece of the build log for the reference
|
…29666) `std::erase(_if)` for `basic_string` were made `constexpr` in C++20 by cplusplus/draft@2c1ab97 as follow-up changes of P0980R1. This patch implements the missed changes that were not tracked in a specific paper.
std::erase(_if)
forbasic_string
were madeconstexpr
in C++20 by cplusplus/draft@2c1ab97 as follow-up changes of P0980R1.This patch implements the missed changes that were not tracked in a specific paper.