An option is to get the data pointer, cast it to your new type and assign it to your target vector:
std::basic_string<char16_t> src;
const uint8_t *begin = reinterpret_cast<uint8_t const*>(src.data());
const uint8_t *end = reinterpret_cast<uint8_t const*>(src.data() + src.size());
std::vector<uint8_t> dst(begin,end);
This is one of the few cases where reinterpret_cast
is a perfectly valid choice. From cppreference (highlights added by me):
Unlike static_cast
, but like const_cast
, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type
.