From a74ba1cd594860c265190a9749665048e4979e3d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 28 Apr 2018 15:24:41 -0400 Subject: [PATCH 1/4] file_util: Remove compiler version checks around is_trivially_copyable() The minimum clang/GCC versions we support already support this. We can also remove is_standard_layout(), as fread and fwrite only require the type to be trivially copyable. --- src/common/file_util.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/common/file_util.h b/src/common/file_util.h index 57ff8b179..d202cab07 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -175,12 +175,8 @@ public: template size_t ReadArray(T* data, size_t length) { - static_assert(std::is_standard_layout(), - "Given array does not consist of standard layout objects"); -#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) static_assert(std::is_trivially_copyable(), "Given array does not consist of trivially copyable objects"); -#endif if (!IsOpen()) { m_good = false; @@ -196,12 +192,8 @@ public: template size_t WriteArray(const T* data, size_t length) { - static_assert(std::is_standard_layout(), - "Given array does not consist of standard layout objects"); -#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) static_assert(std::is_trivially_copyable(), "Given array does not consist of trivially copyable objects"); -#endif if (!IsOpen()) { m_good = false; From bf964ac6e9e324dc512c0a57ad648a4e0d884c6a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 7 Aug 2018 13:31:57 -0400 Subject: [PATCH 2/4] common: Convert type traits templates over to variable template versions where applicable Uses the C++17 inline variable variants --- src/common/alignment.h | 4 ++-- src/common/bit_set.h | 2 +- src/common/file_util.h | 16 ++++++++++------ src/common/hash.h | 4 ++-- src/common/x64/xbyak_util.h | 2 +- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/common/alignment.h b/src/common/alignment.h index b77da4a92..b9dd38746 100644 --- a/src/common/alignment.h +++ b/src/common/alignment.h @@ -9,13 +9,13 @@ namespace Common { template constexpr T AlignUp(T value, size_t size) { - static_assert(std::is_unsigned::value, "T must be an unsigned value."); + static_assert(std::is_unsigned_v, "T must be an unsigned value."); return static_cast(value + (size - value % size) % size); } template constexpr T AlignDown(T value, size_t size) { - static_assert(std::is_unsigned::value, "T must be an unsigned value."); + static_assert(std::is_unsigned_v, "T must be an unsigned value."); return static_cast(value - value % size); } diff --git a/src/common/bit_set.h b/src/common/bit_set.h index fe22b3b32..a5139f751 100644 --- a/src/common/bit_set.h +++ b/src/common/bit_set.h @@ -96,7 +96,7 @@ static inline int LeastSignificantSetBit(u64 val) { template class BitSet { - static_assert(!std::is_signed::value, "BitSet should not be used with signed types"); + static_assert(!std::is_signed_v, "BitSet should not be used with signed types"); public: // A reference to a particular bit, returned from operator[]. diff --git a/src/common/file_util.h b/src/common/file_util.h index d202cab07..f62ab6cfd 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -174,8 +174,8 @@ public: bool Close(); template - size_t ReadArray(T* data, size_t length) { - static_assert(std::is_trivially_copyable(), + size_t ReadArray(T* data, size_t length) const { + static_assert(std::is_trivially_copyable_v, "Given array does not consist of trivially copyable objects"); if (!IsOpen()) { @@ -192,7 +192,7 @@ public: template size_t WriteArray(const T* data, size_t length) { - static_assert(std::is_trivially_copyable(), + static_assert(std::is_trivially_copyable_v, "Given array does not consist of trivially copyable objects"); if (!IsOpen()) { @@ -207,17 +207,21 @@ public: return items_written; } - size_t ReadBytes(void* data, size_t length) { + template + size_t ReadBytes(T* data, size_t length) const { + static_assert(std::is_trivially_copyable_v, "T must be trivially copyable"); return ReadArray(reinterpret_cast(data), length); } - size_t WriteBytes(const void* data, size_t length) { + template + size_t WriteBytes(const T* data, size_t length) { + static_assert(std::is_trivially_copyable_v, "T must be trivially copyable"); return WriteArray(reinterpret_cast(data), length); } template size_t WriteObject(const T& object) { - static_assert(!std::is_pointer::value, "Given object is a pointer"); + static_assert(!std::is_pointer_v, "WriteObject arguments must not be a pointer"); return WriteArray(&object, 1); } diff --git a/src/common/hash.h b/src/common/hash.h index 73c326980..2c761e545 100644 --- a/src/common/hash.h +++ b/src/common/hash.h @@ -28,7 +28,7 @@ static inline u64 ComputeHash64(const void* data, size_t len) { */ template static inline u64 ComputeStructHash64(const T& data) { - static_assert(std::is_trivially_copyable(), + static_assert(std::is_trivially_copyable_v, "Type passed to ComputeStructHash64 must be trivially copyable"); return ComputeHash64(&data, sizeof(data)); } @@ -38,7 +38,7 @@ template struct HashableStruct { // In addition to being trivially copyable, T must also have a trivial default constructor, // because any member initialization would be overridden by memset - static_assert(std::is_trivial(), "Type passed to HashableStruct must be trivial"); + static_assert(std::is_trivial_v, "Type passed to HashableStruct must be trivial"); /* * We use a union because "implicitly-defined copy/move constructor for a union X copies the * object representation of X." and "implicitly-defined copy assignment operator for a union X diff --git a/src/common/x64/xbyak_util.h b/src/common/x64/xbyak_util.h index 0f52f704b..ec76e0a47 100644 --- a/src/common/x64/xbyak_util.h +++ b/src/common/x64/xbyak_util.h @@ -34,7 +34,7 @@ inline bool IsWithin2G(const Xbyak::CodeGenerator& code, uintptr_t target) { template inline void CallFarFunction(Xbyak::CodeGenerator& code, const T f) { - static_assert(std::is_pointer(), "Argument must be a (function) pointer."); + static_assert(std::is_pointer_v, "Argument must be a (function) pointer."); size_t addr = reinterpret_cast(f); if (IsWithin2G(code, addr)) { code.call(f); From f3f4871275aff3d4c29fdef679456956aa5244f5 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Wed, 5 Sep 2018 02:19:55 +0200 Subject: [PATCH 3/4] Fix compilation errors --- src/common/file_util.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/file_util.h b/src/common/file_util.h index f62ab6cfd..3e7faa43b 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -174,7 +174,7 @@ public: bool Close(); template - size_t ReadArray(T* data, size_t length) const { + size_t ReadArray(T* data, size_t length) { static_assert(std::is_trivially_copyable_v, "Given array does not consist of trivially copyable objects"); @@ -208,7 +208,7 @@ public: } template - size_t ReadBytes(T* data, size_t length) const { + size_t ReadBytes(T* data, size_t length) { static_assert(std::is_trivially_copyable_v, "T must be trivially copyable"); return ReadArray(reinterpret_cast(data), length); } From c7d8ffd14fc4314e1590c2a2d879daa7b74453e0 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Wed, 5 Sep 2018 02:24:44 +0200 Subject: [PATCH 4/4] Make bitfield assignment operator public This change needs to be made to get the code compiling again. It was suggested after a conversation with Lioncash. The conversation can be seen here: https://user-images.githubusercontent.com/20753089/45064197-b6107800-b0b2-11e8-9db8-f696299fb86a.PNG --- src/common/bit_field.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/common/bit_field.h b/src/common/bit_field.h index 0cc0a1be0..c5bb757de 100644 --- a/src/common/bit_field.h +++ b/src/common/bit_field.h @@ -111,12 +111,6 @@ template struct BitField { private: - // We hide the copy assigment operator here, because the default copy - // assignment would copy the full storage value, rather than just the bits - // relevant to this particular bit field. - // We don't delete it because we want BitField to be trivially copyable. - BitField& operator=(const BitField&) = default; - // StorageType is T for non-enum types and the underlying type of T if // T is an enumeration. Note that T is wrapped within an enable_if in the // former case to workaround compile errors which arise when using @@ -128,6 +122,8 @@ private: using StorageTypeU = std::make_unsigned_t; public: + BitField& operator=(const BitField&) = default; + /// Constants to allow limited introspection of fields if needed static constexpr size_t position = Position; static constexpr size_t bits = Bits;