
Herve Bronnimann:
On Wednesday, December 05, 2007, at 12:30PM, "Peter Dimov" <pdimov@pdimov.com> wrote:
The standard doesn't guarantee that a std::string is a contiguous array, but all existing implementations are. You can use &s[0] and take the risk, or you can prefer dispatching on whether s.begin() returns a pointer.
Peter: Section 21.3.1 [string.require] paragraph 3, page 578, of the current working draft:
The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().
Isn't that what Robert is asking for? Is this a new requirement (i.e., not in C++03)? Indeed, I can't see it in C++03.
I think that it's new for C++0x, but even so, this requirement is a strong indication that every std::string is already contiguous. So using &s[0] or &*s.begin() instead of const_cast'ing data() would definitely be a step forward.