Boost logo

Boost :

From: Rainer Deyke (rdeyke_at_[hidden])
Date: 2022-05-14 13:12:52


On 14.05.22 10:31, Daniela Engert via Boost wrote:
> Am 14.05.2022 um 09:45 schrieb Rainer Deyke via Boost:
>> 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.

I did not know that. Still, almost every other use of an invalidated
string_view results in undefined behavior.

assert(sv[0] == "h"); // boom
assert(sv == "hello"); // boom

-- 
Rainer Deyke (rainerd_at_[hidden])

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk