Q:

Copy a basic_string to vector

Please suggest an efficient way to copy bytes from a basic_string< char16_t> to vector< uint8_t>.
I am not concerned with the encoding, and just want to copy the bytes to the bytearray. It will later be interpreted with the correct encoding downstream.
Thanks.

A:

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. 

