Zero line, is it really dictated by the standard?

Discussion

  • It is known that from C ++ 11 and above std::basic_stringit is considered that internal storage buffers with a trailing character are complete.

  • The main reason for this change, among other things, was that the previous definition std::basic_stringallowed only very limited concurrent access to strings and, therefore, limited performance for multi-threaded applications. (You std::basic_stringcan read more about the reasons for the changes in proposal N2534 ).

  • However, after reading the standard, I could not find a quote that explicitly stated that it std::basic_stringshould have an internal storage buffer with a null character.

  • The only implicit quote I found is §21.4.7.1 / 1 & 3 basic_string accessors [string.accessors]:

const charT* c_str() const noexcept;

     

const charT* data() const noexcept;

     

1Returns: a pointer psuch that p + i == &operator[](i)for each iin [0,size()].   3Required: The program should not change any values ​​stored in the character array.

  • I assume that due to efficiency considerations and since they §21.4.7.1/3require the program not to modify the returned buffer, most executors in std::basic_string::c_str()and std::basic_string::data()return an internal buffer with a terminating null character.

  • However, the standard does not indicate anywhere that the buffer that should be returned std::basic_string::c_str()and std::basic_string::data()should be an internal storage buffer std::basic_string, and not some completed copy with a null character.

Questions:

  • - , std::basic_string ?
  • (.. № 1 - - ), , std::basic_string , , , ++ 11 ?
+4
2

21.4.5:

const_reference operator[](size_type pos) const; reference operator[](size_type pos);

1 : pos <= size().

2 : *(begin() + pos), pos < size(), T charT(); .

, s[s.size()] NUL.

NUL - . 21.4.1/5 :

char basic_string . basic_string s &*(s.begin() + n) == &*s.begin() + n n , 0 <= n < s.size().

, n < s.size(), s.size(). , char* p = &s[0]; NUL - , , p[s.size()] .

, data(), c_str() operator[] NUL - . , -, .

+5

++ 11, c_str() data() (, , ). c_str() , data() . ( ) copy-on-write, , c_str(), .

c_str() . , , ++ 11 .

+1

All Articles