Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,8 @@ public:
# if _LIBCPP_STD_VER >= 23
template <class _Op>
_LIBCPP_HIDE_FROM_ABI constexpr void resize_and_overwrite(size_type __n, _Op __op) {
using __result_type = decltype(std::move(__op)(data(), auto(__n)));
static_assert(__integer_like<__result_type>, "Operation return type must be integer-like");
size_type __sz = size();
size_type __cap = capacity();
if (__n > __cap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <memory>
#include <string>

Expand Down Expand Up @@ -77,17 +78,65 @@ constexpr bool test() {
return true;
}

void test_value_categories() {
constexpr bool test_value_categories() {
std::string s;
s.resize_and_overwrite(10, [](char*&&, std::size_t&&) { return 0; });
LIBCPP_ASSERT(is_string_asan_correct(s));
s.resize_and_overwrite(10, [](char* const&, const std::size_t&) { return 0; });
LIBCPP_ASSERT(is_string_asan_correct(s));
struct RefQualified {
int operator()(char*, std::size_t) && { return 0; }
constexpr int operator()(char*, std::size_t) && { return 0; }
};
s.resize_and_overwrite(10, RefQualified{});
LIBCPP_ASSERT(is_string_asan_correct(s));
return true;
}

constexpr bool test_integer_like_return_types() {
std::string s;

s.resize_and_overwrite(10, [](char* p, std::size_t n) -> int {
(void)n;
std::fill(p, p + 5, 'a');
return 5;
});
assert(s.size() == 5);

s.resize_and_overwrite(10, [](char* p, std::size_t n) -> unsigned int {
(void)n;
std::fill(p, p + 3, 'b');
return 3u;
});
assert(s.size() == 3);

s.resize_and_overwrite(10, [](char* p, std::size_t n) -> long {
(void)n;
std::fill(p, p + 7, 'c');
return 7l;
});
assert(s.size() == 7);

s.resize_and_overwrite(20, [](char* p, std::size_t n) -> unsigned long {
(void)n;
std::fill(p, p + 4, 'd');
return 4ul;
});
assert(s.size() == 4);

s.resize_and_overwrite(15, [](char* p, std::size_t n) -> long long {
(void)n;
std::fill(p, p + 8, 'e');
return 8ll;
});
assert(s.size() == 8);

s.resize_and_overwrite(10, [](char* p, std::size_t n) -> unsigned long long {
std::fill(p, p + n, 'f');
return n;
});
assert(s.size() == 10);

return true;
}

int main(int, char**) {
Expand All @@ -105,5 +154,12 @@ int main(int, char**) {
test<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>>();
static_assert(test<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>>());
#endif

test_value_categories();
test_integer_like_return_types();

static_assert(test_value_categories());
static_assert(test_integer_like_return_types());

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <string>

// template<class Operation>
// void resize_and_overwrite(size_type n, Operation op)

// Verify that the operation's return type must be integer-like

#include <string>

void test_bool_return_type() {
std::string s;
s.resize_and_overwrite(10, [](char*, std::size_t) {
return true; // expected-error-re@*:* {{{{(static_assertion|static assertion)}}{{.*}}integer-like}}
});
}

void test_pointer_return_type() {
std::string s;
s.resize_and_overwrite(10, [](char* p, std::size_t) {
return p; // expected-error-re@*:* {{{{(static_assertion|static assertion)}}{{.*}}integer-like}}
// expected-error@*:* {{cannot initialize}}
});
}

void test_float_return_type() {
std::string s;
s.resize_and_overwrite(10, [](char*, std::size_t) {
return 5.0f; // expected-error-re@*:* {{{{(static_assertion|static assertion)}}{{.*}}integer-like}}
});
}
Loading