A function to handle multiple datatypes

If the code for all data types effectively just works out the size and address of the memory occupied by the data, it makes no odds which approach you take and could even be reduced to a macro. The important thing in my mind is whether you expect to ever need to apply this function to something where the decision about which bytes to serialise needs to be type-specific. For example, to serialise a string value it would be sensible to measure the string length rather than just output the pointer, or the first char pointed to. When exporting a struct or class that contains pointers, it might be necessary to do a deep copy rather than a shallow one. Same for any other non-trivial data structures.

However, if you're mainly interested in coping with primitive variables that have different sizes, all those considerations are irrelevant.