From aac6e38da228004b1eadf1cd691d64f44aafa5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sun, 2 Jan 2022 21:59:58 +0100 Subject: [PATCH] Core/Algorithm: Improve SafeCast to handle references --- include/Nazara/Core/Algorithm.hpp | 2 +- include/Nazara/Core/Algorithm.inl | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/Nazara/Core/Algorithm.hpp b/include/Nazara/Core/Algorithm.hpp index 001661b33..15c59f00c 100644 --- a/include/Nazara/Core/Algorithm.hpp +++ b/include/Nazara/Core/Algorithm.hpp @@ -36,7 +36,7 @@ namespace Nz template void HashCombine(std::size_t& seed, const T& v); template bool IsPowerOfTwo(T value); template T ReverseBits(T integer); - template To SafeCast(From value); + template To SafeCast(From&& value); template constexpr auto UnderlyingCast(T value) -> std::underlying_type_t; template diff --git a/include/Nazara/Core/Algorithm.inl b/include/Nazara/Core/Algorithm.inl index a8d10a113..d564ef606 100644 --- a/include/Nazara/Core/Algorithm.inl +++ b/include/Nazara/Core/Algorithm.inl @@ -50,9 +50,9 @@ namespace Nz decltype(auto) AccessByOffset(void* basePtr, std::size_t offset) { if constexpr (std::is_lvalue_reference_v) - return *reinterpret_cast*>(static_cast(basePtr) + offset); + return *reinterpret_cast*>(static_cast(basePtr) + offset); else if constexpr (std::is_pointer_v) - return reinterpret_cast(static_cast(basePtr) + offset); + return reinterpret_cast(static_cast(basePtr) + offset); else static_assert(AlwaysFalse(), "AccessByOffset requires a reference or pointer type"); } @@ -71,9 +71,9 @@ namespace Nz static_assert(std::is_lvalue_reference_v || std::is_pointer_v); if constexpr (std::is_lvalue_reference_v) - return *reinterpret_cast*>(static_cast(basePtr) + offset); + return *reinterpret_cast*>(static_cast(basePtr) + offset); else if constexpr (std::is_pointer_v) - return reinterpret_cast(static_cast(basePtr) + offset); + return reinterpret_cast(static_cast(basePtr) + offset); else static_assert(AlwaysFalse(), "AccessByOffset requires a reference or pointer type"); } @@ -296,7 +296,7 @@ namespace Nz } template - To SafeCast(From value) + To SafeCast(From&& value) { #ifdef NAZARA_COMPILER_MSVC // Disable unreachable code warnings