From 5fa04cfa60ca8b2b99c685f13393ea8fc7ee8c25 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Thu, 17 Oct 2024 21:43:32 -0300 Subject: [PATCH] improve: add static_assert and remove reinterpret cast Uses std::bit_cast intead of reinterpret cast --- src/io/fileloader.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/io/fileloader.hpp b/src/io/fileloader.hpp index 93c02502717..863837464ae 100644 --- a/src/io/fileloader.hpp +++ b/src/io/fileloader.hpp @@ -140,9 +140,11 @@ class PropWriteStream { template void write(T add) { - char* addr = reinterpret_cast(&add); - std::span sourceSpan(addr, sizeof(T)); - std::ranges::copy(sourceSpan, std::back_inserter(buffer)); + static_assert(std::is_trivially_copyable_v, "Type T must be trivially copyable"); + + auto byteArray = std::bit_cast>(add); + std::span charSpan(byteArray); + std::ranges::copy(charSpan, std::back_inserter(buffer)); } void writeString(const std::string &str) {