Re: Converting between vectors
Andrea Crotti wrote:
I need in short to serialize vector of different types into a stream of
chars, which is then sent over the network.
Boost unfortunately is not the answer since I can't use it in the
project.
Until now I used the function below, but I found out that is terribly
buggy, since it doesn't work in all cases of type conversion and even
size.
Can you please be more specific?
But I can't find a way to fix it and to make it general, any idea /hint?
Thanks,
Andrea
#include <assert.h>
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
template <typename INP, typename OUT>
std::vector<OUT> vectorConversion(const std::vector<INP>& input)
{
size_t size_inp = sizeof(INP);
size_t size_out = sizeof(OUT);
// make sure the sizeof are multiples between them, otherwise it doesn't work!
assert(((size_inp % size_out) == 0) || ((size_out % size_inp) == 0));
std::vector<OUT> res;
size_t size = (size_inp * input.size()) / size_out;
It looks to me like you want to pack or unpack the values in input to
res. If that's the case the above line will not work in all cases.
Consider if size_inp == 2 and input.size() == 3 and size_out == 4.
res.resize(size);
// is it memcpy a good idea in this case?
memcpy(&res[0], &input[0], size_inp * input.size());
return res;
}
LR