I'm mostly interested in the general case of:
T& tr = SomeSource();
T* tp = &tr;
T t = tp[5];
The specific example that provoked this question (and seems to have been asked befor) is:
std::string s = "01234567";
char *c = &s[0];
// Do something with it.
c[4] = 'f'; // Is this valid or undefined behavior
Context:
- Writing to std::string::data is explicitly UB.
- std::string::operator[] can return references that writing to is UB (e.g. out of bounds).
- The intent of the motivating case is to avoid a C function to populate a char* and end up with that in a C++ std::string without having to copy it.