Converting between vectors
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.
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;
res.resize(size);
// is it memcpy a good idea in this case?
memcpy(&res[0], &input[0], size_inp * input.size());
return res;
}
int main()
{
std::vector<int> vec(5, 1);
std::vector<long> lvec = vectorConversion<int, long>(vec);
std::vector<int> vec2 = vectorConversion<long, int>(lvec);
assert(vec == vec2);
return 0;
}