On 24.04.2018 10:28, bol...@cylonHQ.com wrote:
> On Tue, 24 Apr 2018 00:12:11 +0300
> Paavo Helde <
myfir...@osa.pri.ee> wrote:
>> For type char, addressof() is the same as &.
>>
>> I agree that &s[0] is a bit ugly, but it did work in all known
>> implementations AFAIK even before C++11 officially forced the strings to
>> use contiguous storage.
>
> Would it nor have made more sense for c_str() to return a pointer to mutable
> memory rather than taking an address of the first element
Consider whether the c_str() buffer should be the string's own main
buffer, so that changes to one of them is directly visible in the other.
If so, then for C++98 and C++03 this would impose a contiguous buffer
requirement. At the time it was envisioned that implementors might
choose a non-contiguous main buffer. It turned out that they didn't, so
C++11 imposed a contiguous buffer requirement, but at the time that was
not known: keeping the possibility of non-contiguous buffer open was
considered important enough to, well, to keep that possibility open.
But if changes to the c_str() buffer should not be reflected in the main
buffer, then a mutable c_str() buffer would require dynamic allocation
and copying of the string contents also in the case where, at the
moment, the main string buffer was contiguous. This would be at odds
with the C++ principle of not paying for what you don't use.
One might however envision wording that would allow, but not require,
changes to be reflected in the main buffer.
But that would make code less portable, and brittle, and possibly
depending on the dynamic internal state of the string for correctness.
> which seems a pretty strange way to get the address?
It's the ordinary way to get the address of an array in C and C++,
except that for raw arrays one usually lets type decays do the job.
There is nothing strange whatsoever about it.
What's /strange/ is doing it in some other way.
Cheeers & hth.,
- Alf