2 ms·
pedantically, the cast itself is not UB. Dereferencing a pointer which isn't compatible with the dynamic type of the underlying data would be UB, but in this ca
by gpderetta 5y ago
pedantically, the cast itself is not UB. Dereferencing a pointer which isn't compatible with the dynamic type of the underlying data would be UB, but in this case the actual data is coming from outside the process (the OS usually), and wasn't even necessarily written in C++, so talking about the type of the underlying data is really not well defined (this is similar to reinterpret casting the data coming from read() to whatever is the structure representing the layout of the data).
Accessing the data using two different types would be problematic, except that if the other type is char, it is still fine as char is allowed to alias anything.
so tldr; as the programmer you can posit that the underlying data is indeed char8_t and the reinterpret cast is valid. You can also read it as char and would still be safe.
- Kranar 5y agoYes, you are correct. That said the dereferencing happens in std::u8string_view's constructor, which will dereference in search of the NULL character to compute the size.