Skip to content

Instantly share code, notes, and snippets.

@danlark1
Last active April 13, 2020 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danlark1/c6b10b98efe768b77f6d2cc99703ada3 to your computer and use it in GitHub Desktop.
Save danlark1/c6b10b98efe768b77f6d2cc99703ada3 to your computer and use it in GitHub Desktop.
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