Will std :: string always end with zero in C ++ 11?

In a 2008 publication on his website, Herb Sutter states the following:

There is an active suggestion for further tightening this in C ++ 0x and requires rejection of the zero sequence and, possibly, the prohibition of copy-to-write implementation, for concurrency reasons. Heres the paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html . I think that one or both proposals in this document are likely to be accepted, but it is clearly visible at the next or two meetings.

I know that C ++ 11 now guarantees that the contents of std :: string will be stored contiguously, but did they apply the above in the final project?

Is it now safe to use something like &str[0] ?

+44
c ++ string language-lawyer c ++ 11 null-terminated
May 20 '11 at 20:20
source share
1 answer

Yes. In C ++ 0x FDIS 21.4.7.1/1, std::basic_string::c_str() should return

a pointer p such that p + i == &operator[](i) for each i in [0,size()] .

This means that for string s pointer returned by s.c_str() must be the same as the address of the starting character in the string ( &s[0] ).

+46
May 20 '11 at 20:30
source share



All Articles