Title
basic_string is missing non-const data()
Status
resolved
Section
[basic.string]
Submitter
Michael Bradshaw

Created on 2014-05-27.00:00:00 last changed 96 months ago

Messages

Date: 2016-05-22.17:52:38

Proposed resolution:

This wording is relative to N3936.

  1. Change class template basic_string synopsis, [basic.string], as indicated:

    namespace std {
      template<class charT, class traits = char_traits<charT>,
      class Allocator = allocator<charT> >
      class basic_string {
      public:
        […]
        // 21.4.7, string operations:
        const charT* c_str() const noexcept;
        const charT* data() const noexcept;
        charT* data() noexcept;
        allocator_type get_allocator() const noexcept;
        […]
      };
    }
    
  2. Add the following sequence of paragraphs following [string.accessors] p3, as indicated:

    charT* data() noexcept;
    

    -?- Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].

    -?- Complexity: Constant time.

    -?- Requires: The program shall not alter the value stored at p + size().

Date: 2016-05-22.00:00:00

[ 2016-05-22 ]

Marshall says: this issue has been resolved by P0272R1.

Date: 2015-03-29.21:26:39

[ 2015-02 Cologne ]

Back to LEWG.

Date: 2014-05-27.00:00:00

Regarding [basic.string], std::basic_string<charT>::data() returns a const charT* [string.accessors]. While this method is convenient, it doesn't quite match std::array<T>::data() [array.data] or std::vector<T>::data() [vector.data], both of which provide two versions (that return T* or const T*). An additional data() method can be added to std::basic_string that returns a charT* so it can be used in similar situations that std::array and std::vector can be used. Without a non-const data() method, std::basic_string has to be treated specially in code that is otherwise oblivious to the container type being used.

Adding a charT* return type to data() would be equivalent to doing &str[0] or &str.front().

Small discussion on the issue can be found here and in the std-discussion thread (which didn't get too much attention).

This requires a small change to std::basic_string's definition in [basic.string] to add the method to std::basic_string, and another small change in [string.accessors] to define the new method.

History
Date User Action Args
2016-05-22 17:52:38adminsetmessages: + msg8146
2016-05-22 17:52:38adminsetstatus: lewg -> resolved
2015-03-29 21:26:39adminsetmessages: + msg7280
2015-03-29 21:26:39adminsetstatus: new -> lewg
2014-06-03 13:40:56adminsetmessages: + msg6980
2014-05-27 00:00:00admincreate