
Am Wednesday 23 December 2009 01:40:39 schrieb Slawomir Lisznianski:
template <typename T> struct envelope { size_t len = sizeof(T); // <--- compiler may pad here, hence this doesn't work... T body; };
So, given an arbitrary type T, we want to create an envelope that will have the size of T immediately before it, with no padding between len and body.
The user would then be able to:
envelope<MyStruct> env;
env.body.x = ...
send(env);
you can't make the compiler skip alignment, so if you want to keep the user
interface, you have to redirect the accesses to a char array.
something like this:
template
I'm curious if anyone has run into something similar and have a clean solution. Our code currently has to defer to temporary buffers and memcpy'ing data back to it.
not sure how "clean" that solution is. it might technically even be undefined behaviour.