Skip to content

Implement LWG-4259 P1148R0 changed the return values of searching functions of std::basic_string on some platforms#6236

Open
YexuanXiao wants to merge 3 commits intomicrosoft:mainfrom
YexuanXiao:wide-npos
Open

Implement LWG-4259 P1148R0 changed the return values of searching functions of std::basic_string on some platforms#6236
YexuanXiao wants to merge 3 commits intomicrosoft:mainfrom
YexuanXiao:wide-npos

Conversation

@YexuanXiao
Copy link
Copy Markdown
Contributor

@YexuanXiao YexuanXiao commented Apr 8, 2026

Fixes #6208.

…nctions of std::basic_string on some platforms
@YexuanXiao YexuanXiao requested a review from a team as a code owner April 8, 2026 02:54
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Apr 8, 2026
Comment thread stl/inc/xstring Outdated
@YexuanXiao YexuanXiao changed the title Implement LWG-4259: P1148R0 changed the return values of searching fu… Implement LWG-4259 P1148R0 changed the return values of searching fu… Apr 8, 2026
@StephanTLavavej StephanTLavavej added the LWG Library Working Group issue label Apr 8, 2026
@StephanTLavavej StephanTLavavej changed the title Implement LWG-4259 P1148R0 changed the return values of searching fu… Implement LWG-4259 P1148R0 changed the return values of searching functions of std::basic_string on some platforms Apr 8, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements the LWG-4259/P1148R0 behavior change so std::basic_string searching functions return basic_string::npos (not a truncated size_t(-1)/string_view::npos) when basic_string::size_type is wider than size_t, and adds a regression test for that scenario.

Changes:

  • Add an internal helper to cast search results from size_t to basic_string::size_type, mapping size_t(-1) to basic_string::npos when needed.
  • Update all basic_string search-family functions (find*/rfind*) to use the helper.
  • Add a test using a custom allocator with a wider size_type to validate npos return values.
Show a summary per file
File Description
tests/std/tests/GH_005546_containers_size_type_cast/test.cpp Adds a wide-size_type allocator + new test cases covering basic_string search functions returning npos.
stl/inc/xstring Fixes return-value casting for basic_string searching functions when size_type is wider than size_t.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 3

Comment on lines +530 to +533
constexpr T* allocate(size_type n) {
return allocator<T>{}.allocate(static_cast<size_t>(n));
}
constexpr void deallocate(T* p, size_type n) noexcept {
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WideSizeAllocator::allocate/deallocate are declared constexpr unconditionally, but they call std::allocator<T>::allocate/deallocate, which isn't constexpr before C++20. This can make pre-C++20 builds ill-formed even if these functions are never used in a constant expression. Use the existing CONSTEXPR20 macro for these member functions (and consider calling check_alloc_in_range_of_size_t(n) before the size_t cast to avoid silent truncation, matching redifference_allocator).

Suggested change
constexpr T* allocate(size_type n) {
return allocator<T>{}.allocate(static_cast<size_t>(n));
}
constexpr void deallocate(T* p, size_type n) noexcept {
CONSTEXPR20 T* allocate(size_type n) {
check_alloc_in_range_of_size_t(n);
return allocator<T>{}.allocate(static_cast<size_t>(n));
}
CONSTEXPR20 void deallocate(T* p, size_type n) noexcept {
check_alloc_in_range_of_size_t(n);

Copilot uses AI. Check for mistakes.
Comment on lines +520 to +524
template <typename T = char>
struct WideSizeAllocator {
using value_type = T;
using size_type = unsigned long long;

Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WideSizeAllocator doesn't define allocator equality (operator==/!=). Allocators used with standard containers are expected to be equality comparable, and basic_string operations may instantiate allocator comparisons (e.g. swap/move assignment) even if this test currently doesn't. Consider adding the same style of friend constexpr bool operator== (and != when needed) used by redifference_allocator earlier in this file.

Copilot uses AI. Check for mistakes.
Comment thread stl/inc/xstring
Comment on lines +2516 to +2522
_NODISCARD static _CONSTEXPR20 size_type _Find_result_cast(const size_t _Off) noexcept {
if constexpr (sizeof(size_type) > sizeof(size_t)) { // only happens on 32-bit platforms with 64-bit size_type
if (_Off == static_cast<size_t>(-1)) {
return npos;
}
}
return static_cast<size_type>(_Off);
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_Find_result_cast takes a parameter named _Off, but it is the result from the internal _Traits_* search helpers, not an offset. Renaming the parameter (e.g. _Result/_Pos) would make the intent clearer and reduce the chance of accidental misuse when additional callers are added.

Suggested change
_NODISCARD static _CONSTEXPR20 size_type _Find_result_cast(const size_t _Off) noexcept {
if constexpr (sizeof(size_type) > sizeof(size_t)) { // only happens on 32-bit platforms with 64-bit size_type
if (_Off == static_cast<size_t>(-1)) {
return npos;
}
}
return static_cast<size_type>(_Off);
_NODISCARD static _CONSTEXPR20 size_type _Find_result_cast(const size_t _Result) noexcept {
if constexpr (sizeof(size_type) > sizeof(size_t)) { // only happens on 32-bit platforms with 64-bit size_type
if (_Result == static_cast<size_t>(-1)) {
return npos;
}
}
return static_cast<size_type>(_Result);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LWG Library Working Group issue

Projects

Status: Initial Review

Development

Successfully merging this pull request may close these issues.

LWG-4259 P1148R0 changed the return values of searching functions of std::basic_string on some platforms

4 participants