I'm reading type aliasing rules but can't figure out if this code has UB in it:
std::vector<std::byte> vec = {std::byte{'a'}, std::byte{'b'}};
auto sv = std::string_view(reinterpret_cast<char*>(vec.data()), vec.size());
std::cout << sv << '\n';
I'm fairly sure it does not, but I often get surprised by C++.
Is reinterpret_cast
between char*
, unsigned char*
and std::byte*
always allowed?
Additionally, is addition of const
legal in such cast, e.g:
std::array<char, 2> arr = {'a', 'b'};
auto* p = reinterpret_cast<const std::byte*>(arr.data());
Again, I suspect it is legal since it says
AliasedType is the (possibly cv-qualified) signed or unsigned variant of DynamicType
but I would like to be sure with reinterpret_cast
ing once and for all.