|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4259" status="New"> |
| 5 | +<title>P1148R0 changed the return values of searching functions of `std::basic_string` on some platforms</title> |
| 6 | +<section><sref ref="[string.find]"/></section> |
| 7 | +<submitter>Jiang An</submitter> |
| 8 | +<date>5 May 2025</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +<paper num="P1148R0"/> respecified the searching functions of `std::basic_string` to return |
| 14 | +corresponding string view type's `npos` member constant (equal to `std::size_t(-1)`), converted |
| 15 | +to the string type `S`'s member `S::size_type`, when the search fails. Before the change, |
| 16 | +`S::npos` (equal to `S::size_type(-1)`) was returned on failure. |
| 17 | +<p/> |
| 18 | +On platforms where `std::size_t` isn't the widest unsigned integer type (e.g. on usual 32-bit |
| 19 | +platforms), the return value can change. Because there can be an allocator with a wider size_type, |
| 20 | +and when the basic_string type `S` uses such an allocator, `S::size_type` is specified to be that |
| 21 | +type, which in turn makes `S::size_type(std::size_t(-1))` not equal to `S::size_type(-1)`. |
| 22 | +<p/> |
| 23 | +Do we want to restore the old return values? |
| 24 | +</p> |
| 25 | +</discussion> |
| 26 | + |
| 27 | +<resolution> |
| 28 | +<p> |
| 29 | +This wording is relative to <paper num="N5008"/>. |
| 30 | +</p> |
| 31 | +<ol> |
| 32 | + |
| 33 | +<li><p>Modify <sref ref="[string.find]"/> as indicated:</p> |
| 34 | + |
| 35 | +<blockquote> |
| 36 | +<pre> |
| 37 | +template<class T> |
| 38 | + constexpr size_type find(const T& t, size_type pos = 0) const noexcept(<i>see below</i>); |
| 39 | +[…] |
| 40 | +template<class T> |
| 41 | + constexpr size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept(<i>see below</i>); |
| 42 | +</pre> |
| 43 | +<blockquote> |
| 44 | +<p> |
| 45 | +-2- <i>Constraints</i>: […] |
| 46 | +<p/> |
| 47 | +-3- <i>Effects</i>: Let <tt><i>G</i></tt> be the name of the function. Equivalent to: |
| 48 | +</p> |
| 49 | +<blockquote><pre> |
| 50 | +basic_string_view<charT, traits> s = *this, sv = t; |
| 51 | +<del>return s.<i>G</i>(sv, pos);</del> |
| 52 | +<ins>if (auto result = s.<i>G</i>(sv, pos); result == size_t(-1)) |
| 53 | + return npos; |
| 54 | +else |
| 55 | + return result;</ins> |
| 56 | +</pre></blockquote> |
| 57 | +</blockquote> |
| 58 | +</blockquote> |
| 59 | +</li> |
| 60 | +</ol> |
| 61 | +</resolution> |
| 62 | + |
| 63 | +</issue> |
0 commit comments