This task might seem esoteric and dangerous but it's for some highly optimized code (admittedly I yet have to do actual benching). However I have an std::vector<unsigned long long int>
and want to turn it into a std::vector<unsigned short>
where each of the long long ints (64 bit each) forms four separate shorts (16 bit each). That means the actual memory can remain untouched, just the container has to change.
Already know that I can achieve this by memcopying into a new array:
std::vector<unsigned short> shorts_vec(long_long_vec.size()*4);
std::memcpy(&shorts_vec[0], &long_long_vec[0], sizeof(unsigned long long int) * long_long_vec.size());
But I wonder whether there is an in-place solution that does not require copying.