[英]Warning : left shift count >= width of type
I have declared this enum in my header file: 我在头文件中声明了这个枚举:
enum wildcard {
....
....
NW_SRC = 0x111UL << 40,
....
};
When I compile it I get the following warning: 当我编译它时,我收到以下警告:
warning: left shift count >= width of type [enabled by default]
I think this is because enum type is treated as an int
by the compiler. 我认为这是因为编译器将枚举类型视为
int
。 How to I resolve this? 我该如何解决这个问题?
You have two different problems, first the operation and then the declaration of the constant. 你有两个不同的问题,首先是操作,然后是常量的声明。
For the operation you could use the macro that is provided in inttypes.h
对于该操作,您可以使用
inttypes.h
提供的宏
UINT64_C(0x111) << 40
to have a constant of the appropriate width. 具有适当宽度的常数。
But enumeration constants are int
by definition of the standard, so this will not help you to define you an enumeration constant that is large enough to hold the value if on your platform int
is only 32 bit wide (which is very likely). 但是枚举常量是标准定义的
int
,所以这对于定义一个枚举常量无法帮助你,如果在你的平台上int
只有32位宽(这很可能),它就足以保存该值。
The UL
on your platform is probably 32 bit. 您平台上的
UL
可能是32位。 You may need to use ULL
instead: 您可能需要使用
ULL
:
enum wildcard {
....
....
NW_SRC = 0x111ULL << 40,
....
};
This will fix the warning, but the result of the expression may not necessarily fit in an enum
(see this answer for details and references to the relevant standard documents). 这将修复警告,但表达式的结果可能不一定适合
enum
(有关详细信息和相关标准文档的参考,请参阅此答案 )。
UL
is unsigned long, but long on the majority of compilers is 32-bit. UL
是无符号长的,但大多数编译器的长期是32位。 You want ULL
for unsigned long long. 你想要
ULL
的无符号长。
But as correctly pointed out by Jens Gustedt in their answer, in C an enum cannot hold values larger than an int anyway so this will not help. 但正如Jens Gustedt在答案中正确指出的那样,在C中,枚举不能保持大于int的值,所以这无济于事。 C++, in contrast, does allow enums to be represented by larger integral types.
相比之下,C ++确实允许枚举用更大的整数类型表示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.