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@eldwood.com)