Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2022-05-13 18:57:39


Marshall Clow wrote:
> >> If you’re careful and don’t do much with it, it can hand you back a
> >> null terminated string.
> >
> > ???
> >
> > 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

The main use of cstring_view, like string_view, is as a parameter (and
return) type. So if you have a function

void f1( cstring_view csv );

it's true that if inside f1 you write to some random character this may
invalidate csv's promise to be null-terminated, but I see little salient
difference between this and

void f2( char const* csv ); // pre: csv is null-terminated char seq

where f2 writing to a carefully chosen char may also invalidate
the precondition.

Typing "cstring_view" is merely a different way of spelling out the
"pre" of f2.

Similarly,

cstring_view g1();

is an alternative way of spelling

char const* g2(); // post: the return value is a null-terminated char seq


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