Boost logo

Boost :

From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2019-09-15 06:06:09


On 2019-09-15 01:13, Vinnie Falco via Boost wrote:
>
> Would this be OK?
>
> template <std::size_t N>
> class static_string
> {
> mutable char buf_[N+1];
> public:
> ...
> char const*
> c_str() const noexcept
> {
> buf_[size()] = '\0';
> return buf_;
> }
> };

No, this won't work with const storage. You won't be able to use it in
non-writable memory and such strings won't be placed in const sections
of the executable and therefore won't be shared between processes.

> Or we could just leave it the way that it is, and pay for keeping the
> string null terminated at all times, that's not so bad really.

Yes, since you have the buffer size for the terminating zero anyway,
maintaining the terminating zero on string modification is the way to go.


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