Skip to content

Commit

Permalink
improve: add static_assert and remove reinterpret cast
Browse files Browse the repository at this point in the history
Uses std::bit_cast intead of reinterpret cast
  • Loading branch information
dudantas committed Oct 18, 2024
1 parent afa875b commit 5fa04cf
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/io/fileloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ class PropWriteStream {

template <typename T>
void write(T add) {
char* addr = reinterpret_cast<char*>(&add);
std::span<const char> sourceSpan(addr, sizeof(T));
std::ranges::copy(sourceSpan, std::back_inserter(buffer));
static_assert(std::is_trivially_copyable_v<T>, "Type T must be trivially copyable");

auto byteArray = std::bit_cast<std::array<char, sizeof(T)>>(add);
std::span<const char> charSpan(byteArray);
std::ranges::copy(charSpan, std::back_inserter(buffer));
}

void writeString(const std::string &str) {
Expand Down

0 comments on commit 5fa04cf

Please sign in to comment.