Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6610359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:49:51+00:00 2026-05-25T19:49:51+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:49:52+00:00Added an answer on May 25, 2026 at 7:49 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With a vector defined as std::vector<std::string> , Wondering why the following is valid: if
std::vector< boost::variant<std::string, int> > vec; std::string s1(abacus); int i1 = 42; vec.push_back(s1); vec.push_back(i1); std::cout
I have a vector containing simple time series data (extracted from a deSolve matrix),
ActionScript's Array and Vector classes both have a slice() method. If you don't pass
For a given vector like this, Vector<Temp> s = new Vector<Temp>(); s.add(new Temp(a,1)); s.add(new
Having a vector containing pointers to objects then using the clear function doesn't call
Suppose we have a vector/array in C++ and we wish to count which of
I have several std::vector , all of the same length. I want to sort
I have a vector that I want to insert into a set . This
If I have a vector of pairs: std::vector<std::pair<int, int> > vec; Is there and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.