Skip to content

Instantly share code, notes, and snippets.

@danlark1
Last active April 13, 2020 17:02
Embed
What would you like to do?
template <class T>
T Deserialize(const void* memory) noexcept {
static_assert(std::is_trivially_copyable_v<T>);
T t;
memcpy(std::addressof(t), memory, sizeof(T));
return t;
}
// std::remove_reference_t for non-deduced context to prevent such code to blow below:
// char first = f(); char second = g();
// Serialize(first - second, to) (int will be deduced)
template <class T>
void Serialize(const std::remove_reference_t<T>& t, void* memory) noexcept {
static_assert(std::is_trivially_copyable_v<T>);
memcpy(to, std::addressof(t), sizeof(T));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment