检查相同的枚举,C++
Check same enum, C++
我现在正在抽象我的问题,但我处于这种情况。
假设我有 2 个枚举在同一个命名空间中。
namespace some_namespace {
enum class Something{
S,
O,
M,
E,
T,
H,
I,
N,
G
};
enum class Else{
E,
L,
S
};
}
现在让我们创建 2 个结构,它们具有这些枚举的一些值作为静态字段。
struct SomeStruct{
static constexpr int enumValue = static_cast<int>(some_namespace::Something::T);
};
struct AnotherStruct {
static constexpr int enumValue = static_cast<int>(some_namespace::Something::O);
};
如何检查这 2 个结构的 enumValues 是否来自同一个 enum class
?我想我应该给我的枚举字段赋予一些值,并对枚举值使用一些按位运算符来区分?有人可以帮忙吗?谢谢)
仅使用 static_casted enumValue
无法区分原始枚举(正如@Sam 所指出的)。您可以做的是引入一个新字段来存储底层枚举 class 类型并利用它来区分。
enum class E1{ One, Two };
enum class E2{Three, Four };
struct SomeStruct{
static constexpr int enumValue = static_cast<int>(E1::One);
using UnderlyingT = E1;
};
struct AnotherStruct {
static constexpr int enumValue = static_cast<int>(E2::Three);
using UnderlyingT = E2;
};
static_assert(!std::is_same_v<SomeStruct::UnderlyingT, AnotherStruct::UnderlyingT>);
如评论中所述,如果您仍想知道类型,则不想将枚举转换为 int
。
在想要隐式转换为基础类型时,可能有一些原因需要保留类型和范围。
您可以做的是将枚举包装到一个 class 中,该 class 转换为基础类型和实际枚举。
#include <type_traits>
template<typename TEnum>
class ImplicitEnumConverter final
{
public:
TEnum e;
using TUnderlyingType = std::underlying_type_t<TEnum>;
constexpr ImplicitEnumConverter(TEnum e) : e{e} {}
constexpr operator TUnderlyingType() const noexcept
{
return static_cast<TUnderlyingType>(e);
}
constexpr operator TEnum() const noexcept
{
return e;
}
};
struct SomeStruct{
static constexpr auto enumValue = ImplicitEnumConverter{some_namespace::Something::T};
};
这样,您就可以在相关的地方保留所有类型安全的信息,并且可以将其隐式转换为 int。
答案比我想象的要简单。
所以我这样声明枚举。
namespace some_namespace {
enum class Something{
S = 0,
O = 2,
M = 4,
E = 6,
T = 8,
H = 10,
I = 12,
N = 14,
G = 16
};
enum class Else{
E = 1,
L = 3,
S = 5
};
}
然后这让我检查两个整数值是否具有相同的奇偶性。于是
if((SomeStruct::enum_value + AnotherStruct::enum_value) % 2 == 0) {
// Same enum
} else {
// not same enum.
}
我现在正在抽象我的问题,但我处于这种情况。 假设我有 2 个枚举在同一个命名空间中。
namespace some_namespace {
enum class Something{
S,
O,
M,
E,
T,
H,
I,
N,
G
};
enum class Else{
E,
L,
S
};
}
现在让我们创建 2 个结构,它们具有这些枚举的一些值作为静态字段。
struct SomeStruct{
static constexpr int enumValue = static_cast<int>(some_namespace::Something::T);
};
struct AnotherStruct {
static constexpr int enumValue = static_cast<int>(some_namespace::Something::O);
};
如何检查这 2 个结构的 enumValues 是否来自同一个 enum class
?我想我应该给我的枚举字段赋予一些值,并对枚举值使用一些按位运算符来区分?有人可以帮忙吗?谢谢)
仅使用 static_casted enumValue
无法区分原始枚举(正如@Sam 所指出的)。您可以做的是引入一个新字段来存储底层枚举 class 类型并利用它来区分。
enum class E1{ One, Two };
enum class E2{Three, Four };
struct SomeStruct{
static constexpr int enumValue = static_cast<int>(E1::One);
using UnderlyingT = E1;
};
struct AnotherStruct {
static constexpr int enumValue = static_cast<int>(E2::Three);
using UnderlyingT = E2;
};
static_assert(!std::is_same_v<SomeStruct::UnderlyingT, AnotherStruct::UnderlyingT>);
如评论中所述,如果您仍想知道类型,则不想将枚举转换为 int
。
在想要隐式转换为基础类型时,可能有一些原因需要保留类型和范围。
您可以做的是将枚举包装到一个 class 中,该 class 转换为基础类型和实际枚举。
#include <type_traits>
template<typename TEnum>
class ImplicitEnumConverter final
{
public:
TEnum e;
using TUnderlyingType = std::underlying_type_t<TEnum>;
constexpr ImplicitEnumConverter(TEnum e) : e{e} {}
constexpr operator TUnderlyingType() const noexcept
{
return static_cast<TUnderlyingType>(e);
}
constexpr operator TEnum() const noexcept
{
return e;
}
};
struct SomeStruct{
static constexpr auto enumValue = ImplicitEnumConverter{some_namespace::Something::T};
};
这样,您就可以在相关的地方保留所有类型安全的信息,并且可以将其隐式转换为 int。
答案比我想象的要简单。
所以我这样声明枚举。
namespace some_namespace {
enum class Something{
S = 0,
O = 2,
M = 4,
E = 6,
T = 8,
H = 10,
I = 12,
N = 14,
G = 16
};
enum class Else{
E = 1,
L = 3,
S = 5
};
}
然后这让我检查两个整数值是否具有相同的奇偶性。于是
if((SomeStruct::enum_value + AnotherStruct::enum_value) % 2 == 0) {
// Same enum
} else {
// not same enum.
}