Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are std::vector::data and std::string::data different?

Vector's new method data() provides a const and non-const version.
However string's data() method only provides a const version.

I think they changed the wording about std::string so that the chars are now required to be contiguous (like std::vector).

Was std::string::data just missed? Or is the a good reason to only allow const access to a string's underlying characters?

note: std::vector::data has another nice feature, it's not undefined behavior to call data() on an empty vector. Whereas &vec.front() is undefined behavior if it's empty.

like image 931
deft_code Avatar asked Sep 22 '11 17:09

deft_code


People also ask

What is the difference between std::string and std::vector?

std::string offers a very different and much expanded interface compared to std::vector<> . While the latter is just a boring old sequence of elements, the former is actually designed to represent a string and therefore offers an assortment of string-related convenience functions.

What is the difference between a string and a vector?

The preeminent difference between vector and string is that vector can correctly contain objects, string works only on primitives. So vector provides these methods that would be useless for a string working with primitives: vector::emplace.

What is the difference between std::string and string?

There is no functionality difference between string and std::string because they're the same type.

What is std::string data?

std::string::dataReturns a pointer to an array that contains the same sequence of characters as the characters that make up the value of the string object.


2 Answers

In C++98/03 there was good reason to not have a non-const data() due to the fact that string was often implemented as COW. A non-const data() would have required a copy to be made if the refcount was greater than 1. While possible, this was not seen as desirable in C++98/03.

In Oct. 2005 the committee voted in LWG 464 which added the const and non-const data() to vector, and added const and non-const at() to map. At that time, string had not been changed so as to outlaw COW. But later, by C++11, a COW string is no longer conforming. The string spec was also tightened up in C++11 such that it is required to be contiguous, and there's always a terminating null exposed by operator[](size()). In C++03, the terminating null was only guaranteed by the const overload of operator[].

So in short a non-const data() looks a lot more reasonable for a C++11 string. To the best of my knowledge, it was never proposed.

Update

charT* data() noexcept; 

was added basic_string in the C++1z working draft N4582 by David Sankel's P0272R1 at the Jacksonville meeting in Feb. 2016.

Nice job David!

like image 116
Howard Hinnant Avatar answered Oct 01 '22 18:10

Howard Hinnant


Historically, the string data has not been const because it would prevent several common optimizations, like copy-on-write (COW). This is now, IIANM, far less common, because it behaves badly with multithreaded programs.

BTW, yes they are now required to be contiguous:

[string.require].5: 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().

Another reason might be to avoid code such as:

std::string ret; strcpy(ret.data(), "whatthe..."); 

Or any other function that returns a preallocated char array.

like image 31
rodrigo Avatar answered Oct 01 '22 18:10

rodrigo