diff --git a/libcxx/include/string b/libcxx/include/string index 419e3eee6746e..ea9ba24084a3b 100644 --- a/libcxx/include/string +++ b/libcxx/include/string @@ -516,10 +516,10 @@ basic_istream& getline(basic_istream& is, basic_string& str); template -typename basic_string::size_type +constexpr typename basic_string::size_type erase(basic_string& c, const U& value); // C++20 template -typename basic_string::size_type +constexpr typename basic_string::size_type erase_if(basic_string& c, Predicate pred); // C++20 typedef basic_string string; @@ -4022,7 +4022,7 @@ getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Al # if _LIBCPP_STD_VER >= 20 template -inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type +inline _LIBCPP_HIDE_FROM_ABI 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 -inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type +inline _LIBCPP_HIDE_FROM_ABI 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 @@ // // template -// typename basic_string::size_type +// constexpr typename basic_string::size_type // erase(basic_string& c, const U& value); #include @@ -22,7 +22,7 @@ #include "min_allocator.h" template -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 -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(); test, min_allocator>>(); test, test_allocator>>(); + 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 @@ // // template -// typename basic_string::size_type +// constexpr typename basic_string::size_type // erase_if(basic_string& c, Predicate pred); #include @@ -21,7 +21,7 @@ #include "min_allocator.h" template -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 -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(); test, min_allocator>>(); test, test_allocator>>(); + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + return 0; }