You do not copy the source data of the target, but only change which object the targetData pointer points to.
Something like this will work:
Payload* targetPayload = reinterpret_cast<Payload*>(targetData); *targetPayload = *sourceData;
Obtaining ownership of the source payload using a smart pointer is probably a bad idea - if the calling code is written to handle exceptions correctly, then it will delete the object on error, so a smart pointer will mean that it will be deleted twice. If the calling code is not written right for handling exceptions, then it is your job to write code that cannot throw an exception that a smart pointer does not work with.
(since it is different between pointers, you can use static_cast, but I prefer reinterpret_cast, because void * can be anything, and reinterpret_cast tells other developers that something potentially dangerous is happening.)
source share