This piece of code looks like disaster waiting to happen. If it has to exist for some reason, it should be stuffed with static assertions and comments to make the intent clear.
Basically, it will work only if these conditions hold:
sizeof(T) <= sizeof(unsigned int)
(because you're copying from anunsigned int
into aT
). I see this is alreay asserted, fine.T
is trivially copyable (as per the C++11std::is_trivially_copyable
trait). If your compiler doesn't support the Boost equivalent of this trait, just document the requirement.
As long as these are met, it's fine. That's what trivially copyable actually means - it can be copied byte for byte. I'd just use unsigned char
instead of char
to stress the fact that it's bytes, not characters.