From 88191480a8ad2335b3e90493875753cfdac157e2 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 19 Feb 2019 04:37:00 -0300 Subject: [PATCH] Remove undefined behaviour from literal number --- src/literal_number.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/literal_number.h b/src/literal_number.h index b900a0f..5258f00 100644 --- a/src/literal_number.h +++ b/src/literal_number.h @@ -8,6 +8,7 @@ #include "operand.h" #include "stream.h" +#include #include namespace Sirit { @@ -26,18 +27,15 @@ class LiteralNumber : public Operand { static_assert(sizeof(T) == 4 || sizeof(T) == 8); LiteralNumber* number = new LiteralNumber(std::type_index(typeid(T))); - if (number->is_32 = sizeof(T) == 4; number->is_32) { - number->raw = *reinterpret_cast(&value); - } else { - number->raw = *reinterpret_cast(&value); - } + number->is_32 = sizeof(T) == 4; + std::memcpy(&number->raw, &value, sizeof(T)); return number; } private: - const std::type_index type; - bool is_32; - u64 raw; + std::type_index type; + bool is_32{}; + u64 raw{}; }; } // namespace Sirit