[gjs/msvc.partial.fix: 22/23] enum-utils.h: Fix type ambiguity on Visual Studio
- From: Chun-wei Fan <fanchunwei src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/msvc.partial.fix: 22/23] enum-utils.h: Fix type ambiguity on Visual Studio
- Date: Mon, 8 Mar 2021 09:04:00 +0000 (UTC)
commit bf6c74c214f8c74fb18fb6a50e4a24aeeab803f5
Author: Chun-wei Fan <fanchunwei src gnome org>
Date: Mon Mar 8 16:56:12 2021 +0800
enum-utils.h: Fix type ambiguity on Visual Studio
Make the wrapper use the underlying type on Visual Studio builds, so
that we can pinpoint on the "right" bitwise operator to use on Visual
Studio builds, for the class enumeration(s) we may use.
Also, on the same token, define casts for Visual Studio for the wrapped
types for use on the '|=' and '&=' operators, as well as the '&' and '|'
operators that will be used in value assignments so that the compiler
does not get upset when converting between the underlying type and the
class enumeration type.
gjs/enum-utils.h | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
---
diff --git a/gjs/enum-utils.h b/gjs/enum-utils.h
index 80044b6d..679e8c16 100644
--- a/gjs/enum-utils.h
+++ b/gjs/enum-utils.h
@@ -6,6 +6,16 @@
#include <type_traits>
+#if defined (_MSC_VER) && !defined (__clang__)
+# define ENUM_WRAP_UNDERLYING_TYPE std::conditional_t<is_class<EnumType>(),
std::underlying_type_t<EnumType>, void>
+# define CAST_WRAPPED(x) static_cast<EnumType>(x)
+# define CAST_WRAPPED_T(T,x) static_cast<T>(x)
+#else
+# define ENUM_WRAP_UNDERLYING_TYPE std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>(), void>
+# define CAST_WRAPPED(x) (x)
+# define CAST_WRAPPED_T(T,x) (x)
+#endif
+
namespace GjsEnum {
template <typename T>
@@ -31,8 +41,7 @@ struct WrapperImpl {
};
template <class EnumType>
-using Wrapper =
- std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>, void>;
+using Wrapper = ENUM_WRAP_UNDERLYING_TYPE;
} // namespace GjsEnum
template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
@@ -60,7 +69,7 @@ template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped&> operator|=(
EnumType& first, // NOLINT(runtime/references)
EnumType const& second) {
- first = (first | second);
+ first = CAST_WRAPPED(first | second);
return reinterpret_cast<Wrapped&>(first);
}
@@ -68,7 +77,7 @@ template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped&> operator&=(
EnumType& first, // NOLINT(runtime/references)
EnumType const& second) {
- first = (first & second);
+ first = CAST_WRAPPED(first & second);
return reinterpret_cast<Wrapped&>(first);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]