So, operator[] does not directly say that s[s.size()] should be the character after s[s.size()-1] in memory. Seems worded to avoid this requirement.
But s.data() indicates that s.data()+k == &s[k] and s.data() should return a pointer.
Ignoring the seeming standard defect of using & on CharT above rather than std::addressof , is the implementation free to return another CharT (say, one on a secure page or in ROM) for s[s.size()] before the first call to s.data() ? (Obviously, this can cause the buffer to end on a read-only page with zero on it, I'm talking about a different situation)
To be explicit:
As far as I can tell, if s.data() never called (and the compiler can prove it), then s[s.size()] need not be adjacent to the rest of the buffer.
Can std::addressof(s[s.size()]) change after calling s.data() , and the implementation will comply with the standard (as long as s.data()+k == &s[k] has .data() evaluated before [] , but the compiler has the right to ensure its execution). Or are there demands for immutability that I don't see?
c ++ stdstring language-lawyer c ++ 14 c ++ 17
Yakk
source share