有没有办法使“enum”类型变为无符号?
有没有办法使 enum
类型变为无符号?以下代码向我发出有关有符号/无符号比较的警告。
enum EEE {
X1 = 1
};
int main()
{
size_t x = 2;
EEE t = X1;
if ( t < x ) std::cout << "ok" << std::endl;
return 0;
}
我试图强制编译器使用以下枚举的无符号基础类型:
enum EEE {
X1 = 1,
XN = 18446744073709551615LL
// I've tried XN = UINT_MAX (in Visual Studio). Same warning.
};
但这仍然给出警告。
将常量更改为 UINT_MAX
使其可以按照标准在 GNU C++ 中工作。看来是VS的一个bug。感谢詹姆斯的提示。
Is there a way to make enum
type to be unsigned? The following code gives me a warning about signed/unsigned comparison.
enum EEE {
X1 = 1
};
int main()
{
size_t x = 2;
EEE t = X1;
if ( t < x ) std::cout << "ok" << std::endl;
return 0;
}
I've tried to force compiler to use unsigned underlying type for enum with the following:
enum EEE {
X1 = 1,
XN = 18446744073709551615LL
// I've tried XN = UINT_MAX (in Visual Studio). Same warning.
};
But that still gives the warning.
Changing constant to UINT_MAX
makes it working in GNU C++ as should be according to the standard. Seems to be a bug in VS. Thanks to James for hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试:
如果没有
U
,则整数文字是有符号的。(当然,假设您的实现支持
long long
;我认为它是这样的,因为原始问题使用LL
;否则,您可以使用UL
一个长
)。You might try:
Without the
U
, the integer literal is signed.(This of course assumes your implementation supports
long long
; I assume it does since the original question usesLL
; otherwise, you can useUL
for along
).当前版本的 C++ 中没有。 C++0x 将提供强类型枚举。
目前,您可以使用
if ( static_cast(t) < x )
来删除警告。Not in the current version of C++. C++0x will provide strongly typed enums.
For the time being, you can use
if ( static_cast<size_t>(t) < x )
to remove the warning.如果您想比较它,您也可以重载运算符,
但是您必须对右侧的任何整数类型执行此操作。否则,如果你这样做
e < 2
这将是不明确的:编译器可以使用与左侧完全匹配但需要在右侧进行转换的operator<
,或其内置运算符,需要对左侧与右侧完全匹配。所以最终,我会放以下版本:
不是很好:)但至少你的枚举的用户很随和。但是,如果您最终不想与普通的
int
进行比较,而是与一些有意义的值进行比较,我会按照其他人的建议进行操作,并添加另一个具有值2
,并为其命名。这样,警告也会消失。You could also overload the operators if you want to compare it
However you have to do that dance for any integer type on the right side. Otherwise if you do
e < 2
it would be ambiguous: The compiler could use youroperator<
matching the left side exactly but needing a conversion on the right side, or its built-in operator, needing a promotion for the left side and matching the rigth side exactly.So ultimately, i would put the following versions:
Not very nice :) But at least the user of your enumeration has easy going. However if you ultimately don't want to compare against ordinary
int
but against some meaningful value, i would do what some other guy proposed, and add another enumerator that has as value2
, and name it. That way, warnings will go away too.根据 C++ 枚举是有符号的还是无符号的?
你的编译器可以选择 enum 是否有符号,尽管有一些评论说在 C++0x 中你将能够指定它是无符号的。
According to Are C++ enums signed or unsigned?
your compiler gets to choose whether enum is signed or not, though there are some comments saying that in C++0x you will be able to specify that it is unsigned.
根据 MSDN 上的 C++ 枚举声明:
Per C++ Enumeration Declarations on MSDN:
为什么不
或者
Why not
or