From 0da84c2c69541a5cb6ecce9542bc66ecdf2f5376 Mon Sep 17 00:00:00 2001 From: oltolm Date: Thu, 26 Jun 2025 12:47:00 +0200 Subject: [PATCH] Misc: use concepts instead of SFINAE --- common/EnumOps.h | 52 +++++++++---------- common/Error.cpp | 4 +- common/StringUtil.h | 18 ++++--- common/emitter/x86types.h | 2 +- pcsx2/GS/GSLzma.h | 3 +- pcsx2/GS/GSRingHeap.h | 2 +- .../SW/GSDrawScanlineCodeGenerator.all.h | 4 +- .../SW/GSSetupPrimCodeGenerator.all.h | 4 +- pcsx2/StateWrapper.h | 9 ++-- 9 files changed, 54 insertions(+), 44 deletions(-) diff --git a/common/EnumOps.h b/common/EnumOps.h index 9dde681ed6..018d54a404 100644 --- a/common/EnumOps.h +++ b/common/EnumOps.h @@ -7,9 +7,9 @@ // Template function for casting enumerations to their underlying type template -typename std::underlying_type::type enum_cast(Enumeration E) +std::underlying_type_t enum_cast(Enumeration E) { - return static_cast::type>(E); + return static_cast>(E); } namespace detail @@ -25,67 +25,67 @@ namespace detail Enum value; constexpr enum_bool_helper(Enum value): value(value) {} constexpr operator Enum() const { return value; } - constexpr operator bool() const { return static_cast(static_cast::type>(value)); } + constexpr operator bool() const { return static_cast(static_cast>(value)); } }; }; #define MARK_ENUM_AS_FLAGS(T) template<> struct detail::enum_is_flags : public std::true_type {} template -constexpr typename std::enable_if::value, Enum>::type -operator|(Enum lhs, Enum rhs) noexcept + requires detail::enum_is_flags::value +constexpr Enum operator|(Enum lhs, Enum rhs) noexcept { - using underlying = typename std::underlying_type::type; + using underlying = std::underlying_type_t; return static_cast(static_cast(lhs) | static_cast(rhs)); } template -constexpr typename std::enable_if::value, detail::enum_bool_helper>::type -operator&(Enum lhs, Enum rhs) noexcept + requires detail::enum_is_flags::value +constexpr detail::enum_bool_helper operator&(Enum lhs, Enum rhs) noexcept { - using underlying = typename std::underlying_type::type; + using underlying = std::underlying_type_t; return static_cast(static_cast(lhs) & static_cast(rhs)); } template -constexpr typename std::enable_if::value, Enum>::type -operator^(Enum lhs, Enum rhs) noexcept + requires detail::enum_is_flags::value +constexpr Enum operator^(Enum lhs, Enum rhs) noexcept { - using underlying = typename std::underlying_type::type; + using underlying = std::underlying_type_t; return static_cast(static_cast(lhs) ^ static_cast(rhs)); } template -constexpr typename std::enable_if::value, Enum&>::type -operator|=(Enum& lhs, Enum rhs) noexcept + requires detail::enum_is_flags::value +constexpr Enum& operator|=(Enum& lhs, Enum rhs) noexcept { return lhs = lhs | rhs; } template -constexpr typename std::enable_if::value, Enum&>::type -operator&=(Enum& lhs, Enum rhs) noexcept + requires detail::enum_is_flags::value +constexpr Enum& operator&=(Enum& lhs, Enum rhs) noexcept { return lhs = lhs & rhs; } template -constexpr typename std::enable_if::value, Enum&>::type -operator^=(Enum& lhs, Enum rhs) noexcept + requires detail::enum_is_flags::value +constexpr Enum& operator^=(Enum& lhs, Enum rhs) noexcept { return lhs = lhs ^ rhs; } -template -constexpr typename std::enable_if::value, bool>::type -operator!(Enum e) noexcept +template + requires detail::enum_is_flags::value +constexpr bool operator!(Enum e) noexcept { - return !static_cast::type>(e); + return !static_cast>(e); } -template -constexpr typename std::enable_if::value, Enum>::type -operator~(Enum e) noexcept +template + requires detail::enum_is_flags::value +constexpr Enum operator~(Enum e) noexcept { - return static_cast(~static_cast::type>(e)); + return static_cast(~static_cast>(e)); } diff --git a/common/Error.cpp b/common/Error.cpp index ab87de79fe..53801bb388 100644 --- a/common/Error.cpp +++ b/common/Error.cpp @@ -14,8 +14,8 @@ // Platform-specific includes #if defined(_WIN32) #include "RedtapeWindows.h" -static_assert(std::is_same::value, "DWORD is unsigned long"); -static_assert(std::is_same::value, "HRESULT is long"); +static_assert(std::is_same_v, "DWORD is unsigned long"); +static_assert(std::is_same_v, "HRESULT is long"); #endif Error::Error() = default; diff --git a/common/StringUtil.h b/common/StringUtil.h index 636ccdec06..c4b22165ee 100644 --- a/common/StringUtil.h +++ b/common/StringUtil.h @@ -75,7 +75,8 @@ namespace StringUtil } /// Wrapper around std::from_chars - template ::value, bool> = true> + template + requires std::is_integral_v inline std::optional FromChars(const std::string_view str, int base = 10) { T value; @@ -86,7 +87,8 @@ namespace StringUtil return value; } - template ::value, bool> = true> + template + requires std::is_integral_v inline std::optional FromChars(const std::string_view str, int base, std::string_view* endptr) { T value; @@ -103,7 +105,8 @@ namespace StringUtil return value; } - template ::value, bool> = true> + template + requires std::is_floating_point_v inline std::optional FromChars(const std::string_view str) { T value; @@ -114,7 +117,8 @@ namespace StringUtil return value; } - template ::value, bool> = true> + template + requires std::is_floating_point_v inline std::optional FromChars(const std::string_view str, std::string_view* endptr) { T value; @@ -132,7 +136,8 @@ namespace StringUtil } /// Wrapper around std::to_chars - template ::value, bool> = true> + template + requires std::is_integral_v inline std::string ToChars(T value, int base = 10) { // to_chars() requires macOS 10.15+. @@ -154,7 +159,8 @@ namespace StringUtil #endif } - template ::value, bool> = true> + template + requires std::is_floating_point_v inline std::string ToChars(T value) { // No to_chars() in older versions of libstdc++/libc++. diff --git a/common/emitter/x86types.h b/common/emitter/x86types.h index d0a2f8c95e..86ff6be08b 100644 --- a/common/emitter/x86types.h +++ b/common/emitter/x86types.h @@ -44,7 +44,7 @@ namespace x86Emitter template static __fi bool is_s8(T imm) { - return (s8)imm == (typename std::make_signed::type)imm; + return (s8)imm == (std::make_signed_t)imm; } template diff --git a/pcsx2/GS/GSLzma.h b/pcsx2/GS/GSLzma.h index 9a00a542d4..8af0f54d51 100644 --- a/pcsx2/GS/GSLzma.h +++ b/pcsx2/GS/GSLzma.h @@ -120,7 +120,8 @@ namespace GSDumpTypes #undef DEF_GIFReg // clang-format on - template ::type = true> + template + requires(sizeof(Input) == sizeof(Output)) static constexpr Output BitCast(Input input) { Output output; diff --git a/pcsx2/GS/GSRingHeap.h b/pcsx2/GS/GSRingHeap.h index bb83e05f66..dddfff6ce2 100644 --- a/pcsx2/GS/GSRingHeap.h +++ b/pcsx2/GS/GSRingHeap.h @@ -262,7 +262,7 @@ public: template typename _unique_if::_unique_array_unknown_bound make_unique(size_t count) { - typedef typename std::remove_extent::type Base; + typedef std::remove_extent_t Base; return UniquePtr(make_array(count)); } diff --git a/pcsx2/GS/Renderers/SW/GSDrawScanlineCodeGenerator.all.h b/pcsx2/GS/Renderers/SW/GSDrawScanlineCodeGenerator.all.h index 575552a7ed..1c575105b3 100644 --- a/pcsx2/GS/Renderers/SW/GSDrawScanlineCodeGenerator.all.h +++ b/pcsx2/GS/Renderers/SW/GSDrawScanlineCodeGenerator.all.h @@ -25,8 +25,8 @@ class GSDrawScanlineCodeGenerator : public GSNewCodeGenerator { using XYm = DRAW_SCANLINE_VECTOR_REGISTER; - constexpr static bool isXmm = std::is_same::value; - constexpr static bool isYmm = std::is_same::value; + constexpr static bool isXmm = std::is_same_v; + constexpr static bool isYmm = std::is_same_v; constexpr static int wordsize = 8; constexpr static int vecsize = isXmm ? 16 : 32; constexpr static int vecsizelog = isXmm ? 4 : 5; diff --git a/pcsx2/GS/Renderers/SW/GSSetupPrimCodeGenerator.all.h b/pcsx2/GS/Renderers/SW/GSSetupPrimCodeGenerator.all.h index 8533f72b8c..50545eba58 100644 --- a/pcsx2/GS/Renderers/SW/GSSetupPrimCodeGenerator.all.h +++ b/pcsx2/GS/Renderers/SW/GSSetupPrimCodeGenerator.all.h @@ -26,8 +26,8 @@ class GSSetupPrimCodeGenerator : public GSNewCodeGenerator using Xmm = Xbyak::Xmm; using Ymm = Xbyak::Ymm; - constexpr static bool isXmm = std::is_same::value; - constexpr static bool isYmm = std::is_same::value; + constexpr static bool isXmm = std::is_same_v; + constexpr static bool isYmm = std::is_same_v; constexpr static int vecsize = isXmm ? 16 : 32; constexpr static int dsize = isXmm ? 4 : 8; diff --git a/pcsx2/StateWrapper.h b/pcsx2/StateWrapper.h index 05272d5a46..649ab26b75 100644 --- a/pcsx2/StateWrapper.h +++ b/pcsx2/StateWrapper.h @@ -104,7 +104,8 @@ public: u32 GetVersion() const { return m_version; } /// Overload for integral or floating-point types. Writes bytes as-is. - template || std::is_floating_point_v, int> = 0> + template + requires std::is_integral_v || std::is_floating_point_v void Do(T* value_ptr) { if (m_mode == Mode::Read) @@ -120,7 +121,8 @@ public: } /// Overload for enum types. Uses the underlying type. - template , int> = 0> + template + requires std::is_enum_v void Do(T* value_ptr) { using TType = std::underlying_type_t; @@ -142,7 +144,8 @@ public: } /// Overload for POD types, such as structs. - template && std::is_trivial_v, int> = 0> + template + requires std::is_standard_layout_v && std::is_trivial_v void DoPOD(T* value_ptr) { if (m_mode == Mode::Read)