Skip to content

Commit e739ce2

Browse files
[libc++] Add missed constexpr to erase(_if) in <string> (#129666)
`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.
1 parent 27901ce commit e739ce2

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

libcxx/include/string

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,10 @@ basic_istream<charT, traits>&
516516
getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
517517
518518
template<class charT, class traits, class Allocator, class U>
519-
typename basic_string<charT, traits, Allocator>::size_type
519+
constexpr typename basic_string<charT, traits, Allocator>::size_type
520520
erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
521521
template<class charT, class traits, class Allocator, class Predicate>
522-
typename basic_string<charT, traits, Allocator>::size_type
522+
constexpr typename basic_string<charT, traits, Allocator>::size_type
523523
erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
524524
525525
typedef basic_string<char> string;
@@ -4022,15 +4022,15 @@ getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Al
40224022

40234023
# if _LIBCPP_STD_VER >= 20
40244024
template <class _CharT, class _Traits, class _Allocator, class _Up>
4025-
inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type
4025+
inline _LIBCPP_HIDE_FROM_ABI constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type
40264026
erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
40274027
auto __old_size = __str.size();
40284028
__str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
40294029
return __old_size - __str.size();
40304030
}
40314031

40324032
template <class _CharT, class _Traits, class _Allocator, class _Predicate>
4033-
inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type
4033+
inline _LIBCPP_HIDE_FROM_ABI constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type
40344034
erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred) {
40354035
auto __old_size = __str.size();
40364036
__str.erase(std::remove_if(__str.begin(), __str.end(), __pred), __str.end());

libcxx/test/std/strings/strings.erasure/erase.pass.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// <string>
1212

1313
// template <class charT, class traits, class Allocator, class U>
14-
// typename basic_string<charT, traits, Allocator>::size_type
14+
// constexpr typename basic_string<charT, traits, Allocator>::size_type
1515
// erase(basic_string<charT, traits, Allocator>& c, const U& value);
1616

1717
#include <string>
@@ -22,15 +22,15 @@
2222
#include "min_allocator.h"
2323

2424
template <class S, class U>
25-
void test0(S s, U val, S expected, std::size_t expected_erased_count) {
25+
constexpr void test0(S s, U val, S expected, std::size_t expected_erased_count) {
2626
ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase(s, val)));
2727
assert(expected_erased_count == std::erase(s, val));
2828
LIBCPP_ASSERT(s.__invariants());
2929
assert(s == expected);
3030
}
3131

3232
template <class S>
33-
void test() {
33+
constexpr void test() {
3434
test0(S(""), 'a', S(""), 0);
3535

3636
test0(S("a"), 'a', S(""), 1);
@@ -64,10 +64,17 @@ void test() {
6464
test0(S("aba"), opt('c'), S("aba"), 0);
6565
}
6666

67-
int main(int, char**) {
67+
constexpr bool test() {
6868
test<std::string>();
6969
test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
7070
test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>>();
7171

72+
return true;
73+
}
74+
75+
int main(int, char**) {
76+
test();
77+
static_assert(test());
78+
7279
return 0;
7380
}

libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// <string>
1212

1313
// template <class charT, class traits, class Allocator, class Predicate>
14-
// typename basic_string<charT, traits, Allocator>::size_type
14+
// constexpr typename basic_string<charT, traits, Allocator>::size_type
1515
// erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred);
1616

1717
#include <string>
@@ -21,15 +21,15 @@
2121
#include "min_allocator.h"
2222

2323
template <class S, class Pred>
24-
void test0(S s, Pred p, S expected, std::size_t expected_erased_count) {
24+
constexpr void test0(S s, Pred p, S expected, std::size_t expected_erased_count) {
2525
ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase_if(s, p)));
2626
assert(expected_erased_count == std::erase_if(s, p));
2727
LIBCPP_ASSERT(s.__invariants());
2828
assert(s == expected);
2929
}
3030

3131
template <typename S>
32-
void test() {
32+
constexpr void test() {
3333
auto isA = [](auto ch) { return ch == 'a'; };
3434
auto isB = [](auto ch) { return ch == 'b'; };
3535
auto isC = [](auto ch) { return ch == 'c'; };
@@ -66,10 +66,17 @@ void test() {
6666
test0(S("aba"), True, S(""), 3);
6767
}
6868

69-
int main(int, char**) {
69+
constexpr bool test() {
7070
test<std::string>();
7171
test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
7272
test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>>();
7373

74+
return true;
75+
}
76+
77+
int main(int, char**) {
78+
test();
79+
static_assert(test());
80+
7481
return 0;
7582
}

0 commit comments

Comments
 (0)