The following code works as intended:
template <typename T>
inline char* Utils::PushData(char* destination, T data)
{
memcpy(destination, &data, sizeof(data));
destination += sizeof(T);
return destination;
}
However, when I use this:
template <typename T>
inline char* Utils::PushData(char* destination, T data)
{
memcpy(destination, &data, sizeof(T));
destination += sizeof(T);
return destination;
}
the model I’m building gets messed up. Why would this happen? Surely sizeof(T) and sizeof(data) should be the same, right? Or have I misunderstood what sizeof actually gets the size of?
edit
I wasn’t really too bothered with this one, just thought it was odd. I am using memcpy because I need to ensure the my data is packed with out padding, I also trust my self (the only person who will use this code unless I do decide to take it further) to know to only use it with basic types, and I don’t even call this function directly, I would call a function that would take data in a format that makes more sense from the calling location. I would call this template function using something like…
inline char* Utils::PushXYZ(char* destination, float xValue, float yValue, float zValue)
{
destination = PushData<float>(destination, xValue);
destination = PushData<float>(destination, yValue);
return PushData<float>(destination, zValue);
}
That’s not necessarily the case. Consider this one:
This will yield
sizeof(char[1])when applied toT, andsizeof(char*)when applied todata. Depending on your code, you may have weird instantiations of your template, which could have causes such a case.