C++ Logo

std-proposals

Advanced search

Re: [std-proposals] [Idea] Null-terminated string view (c_str_view / c_wstr_view) – a simpler alternative/complement to zstring_view

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 6 Feb 2026 20:42:27 -0500
On Fri, Feb 6, 2026 at 8:09 PM Henry Miller via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
>
> On Fri, Feb 6, 2026, at 14:26, Thiago Macieira via Std-Proposals wrote:
> > On Friday, 6 February 2026 08:19:37 Pacific Standard Time Sebastian Wittmeier
> > via Std-Proposals wrote:
> >> A) would be good for quick estimates, e.g. buffer space
> >>
> >> B) would be a safety-feature/limit for mutable C strings, if the NUL byte
> >> gets overwritten
> >
> > I don't think a type with "view" in the name should allow mutating, at all.
>
> Too late, std::strintg_view already has operator[] which allows mutating the string. You can't change the length of the
> string (except by getting a different string_view), but you can change the individual characters.
>
> > If you want to change the string, go back to the original buffer, which is
> > probably a std::string, std::vector, or std::span. That one will provide you
> > with the capacity.
>
> The point here is people need to deal with c-style strings in C++, the buffers you get from existing C++ containers often fall short (are not null terminated). Sure we can agree to ban (or at least make hard) particularly problematic things, but the goal is to make c strings easier to use - in particular when calling C functions, not satisfy some notion of purity. One of the things commonly done by the C functions is mutate strings, so we need to allow for it somehow. Maybe that is go back the original buffer, but what is the point of having something to make calling C easier if we can't use it to call C functions?

`basic_string` already ensures a NUL-terminator, so it's not clear to
me what more can be done. Yes, it's a sized string, so after giving it
to a C API, you'd need to get the proper length (via strlen) and set
that back into the basic string. But that's the cost of dealing with C
APIs that modify NUL-terminated strings.

`cstring_view` is a view; you cannot modify the characters it points
to via that interface (not without doing a `const_cast`, which is
inherently dangerous and we don't need to design APIs assuming
perfidy). If `basic_string` is deemed to be insufficient for working
with mutating C APIs, a view is not the tool to solve that problem.

Received on 2026-02-07 01:42:40