Title
Must elements of a string be contiguous?
Status
cd1
Section
[basic.string]
Submitter
Matt Austern

Created on 2005-11-15.00:00:00 last changed 172 months ago

Messages

Date: 2010-10-21.18:28:33

Rationale:

Not standardizing this existing practice does not give implementors more freedom. We thought it might a decade ago. But the vendors have spoken both with their implementations, and with their voice at the LWG meetings. The implementations are going to be contiguous no matter what the standard says. So the standard might as well give string clients more design choices.

Date: 2010-10-21.18:28:33

Proposed resolution:

Add the following text to the end of [basic.string], paragraph 2.

The characters in a string are stored contiguously, meaning that if s is a basic_string<charT, Allocator>, then it obeys the identity &*(s.begin() + n) == &*s.begin() + n for all 0 <= n < s.size().

Date: 2005-11-15.00:00:00

Issue 69, which was incorporated into C++03, mandated that the elements of a vector must be stored in contiguous memory. Should the same also apply to basic_string?

We almost require contiguity already. Clause [multiset] defines operator[] as data()[pos]. What's missing is a similar guarantee if we access the string's elements via the iterator interface.

Given the existence of data(), and the definition of operator[] and at in terms of data, I don't believe it's possible to write a useful and standard- conforming basic_string that isn't contiguous. I'm not aware of any non-contiguous implementation. We should just require it.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg2965
2010-10-21 18:28:33adminsetmessages: + msg2964
2005-11-15 00:00:00admincreate