Boost logo

Boost :

From: Rainer Deyke (rdeyke_at_[hidden])
Date: 2022-05-13 19:29:06


On 13.05.22 20:39, Marshall Clow via Boost wrote:
> On May 13, 2022, at 11:18 AM, Peter Dimov via Boost <boost_at_[hidden]> wrote:
>> In what scenarios will it not give you a null-terminated string?
>
> char arr[6] = “hello";
> cstring_view csv(arr);
> assert(strlen(csv.data())) == 5);
> arr[5] = ‘!’;
> assert(strlen(csv.data())) == 5); // boom
>
> — Marshall
>
> PS. It promises to give you a null-terminated string, but has no way to actually guarantee that.

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

It is the responsibility of the creator of a view to ensure that the
object being viewed does not change in a way that breaks the invariants
of the view while the view is in use.

-- 
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