|
Boost : |
From: Daniela Engert (dani_at_[hidden])
Date: 2022-05-14 08:31:20
Am 14.05.2022 um 09:45 schrieb Rainer Deyke via Boost:
> On 14.05.22 00:42, Marshall Clow via Boost wrote:
>>
>>
>>> On May 13, 2022, at 3:19 PM, Peter Dimov via Boost
>>> <boost_at_[hidden]> wrote:
>>>
>>> Marshall Clow wrote:
>>>> On May 13, 2022, at 12:29 PM, Rainer Deyke via Boost
>>>> <boost_at_[hidden]> wrote:
>>>>> That's an issue with views in general, not just cstring_view.
>>>>>
>>>>> std::string s = "hello";
>>>>> string_view sv = s;
>>>>> assert(sv.size() == 5);
>>>>> s += "!";
>>>>> assert(sv.size() == 5); // boom
>>>>
>>>> I donât see the problem here (and when I run the code I get no
>>>> error - after
>>>> adding the missing âstd::').
>>>>
>>>> No assertion failure; no undefined behavior (unlike the
>>>> cstring_view example)
>>>
>>> Only because "hello!" fits into the small buffer, I suspect. If `s`
>>> reallocates,
>>> `sv` would be left dangling.
>>
>> Agreed.
>> But even if the string *did* reallocate, the call "assert(sv.size()
>> == 5)â is still valid and well defined.
>
> No it's not. sv.size() works by subtracting pointers, and it's only
> legal to subtract two pointers if they point into the same memory
> region. Which sv.begin() and sv.end() no longer do if s reallocates.
> It's subtle, but it's definitely undefined behavior.
>
Not really. The standard (in its current draft) is silent about the
invalidation of size() and talks only about iterators, references and
pointers with respect to the viewed object
[string.view.template.general]/2. On top of that, afaik all major
implementations have agreed on and settled on the same structure layout
as shown in the standard as exposition only. So technically, this is
unspecified.
Ciao
 Dani
-- PGP/GPG: 2CCB 3ECB 0954 5CD3 B0DB 6AA0 BA03 56A1 2C4638C5
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk