vendredi 23 janvier 2015

Error when parsing buffer to create a tuple

I am trying to build a tuple by reading from a buffer. Here is my code:



template<class T>
T read_from_stream(char *& stream)
{
T value;
memcpy(&value, stream, sizeof(T));
stream += sizeof(T);
return value;
}


template <typename ... Args>
tuple<Args...> parse(char * buffer)
{
return tuple<Args...>{read_from_stream<Args>(buffer)...};
}


and I use it like



auto tup = parse<float, int, char>(buf);


Now suppose the added data in buffer is in sequence float, int, char... the read_from_stream gets called in the reverse order i.e for char first then for int and then for float. I am having to specify the function template parameter types in the reverse order (parse) to read the data correctly. I want the order to be preserved. What am I doing wrong?


Aucun commentaire:

Enregistrer un commentaire