[英]narrowing conversion from int to long unsigned int {} is ill-formed in C++11
when I run the below code - I'm getting the warning "narrowing conversion from int to long unsigned int inside {} is ill-formed in C++11 [-Wnarrowing]. I'm using GNU 4.8 compiler. 当我运行下面的代码时 - 我收到警告“缩小从int转换为long unsigned int inside {}在C ++ 11 [-Wnarrowing]中的格式不正确。我正在使用GNU 4.8编译器。
typedef struct TableEntry
{
unsigned long value;
const char *label;
} TableEntry;
enum FunctionType
{
NORMAL = 0,
RANGE = 1
};
TableEntry functionTypes[] =
{
{NORMAL, "NORMAL"},
{RANGE, "RANGE"}
};
I don't understand why compiler is considering enum as an int? 我不明白为什么编译器将enum视为int?
Is this a bug in GCC 4.8? 这是GCC 4.8中的错误吗? Is there any workaround?
有没有解决方法? Any help is appreciated.
任何帮助表示赞赏。
If practical do: 如果可行的话:
enum FunctionType
{
NORMAL = 0,
RANGE = 1
};
typedef struct TableEntry
{
FunctionType value;
const char *label;
} TableEntry;
TableEntry functionTypes[] =
{
{NORMAL, "NORMAL"},
{RANGE, "RANGE"}
};
Otherwise, change the type in the struct to int
, or explicitly base the enumeration on the same type as in the struct. 否则,将struct中的类型更改为
int
,或者将枚举显式基于与struct中相同的类型。
Btw., I the think g++ warning is unfounded and wrong, since the original code is valid C++03. Correction: As I understand it now, the diagnostic is correct, and this is a breaking change in C++11. 顺便说一句,我认为g ++警告是没有根据的,因为原始代码是有效的C ++ 03。
更正:据我所知,诊断是正确的,这是C ++ 11中的一个重大变化。 I just couldn't believe it.
我简直不敢相信。
About the naming convention: those all uppercase identifiers are good for Java programmers (who are used to that convention), but in C++ they increase the chances of inadvertent text substitution. 关于命名约定:所有大写标识符都适用于Java程序员(习惯于该约定),但在C ++中,它们增加了无意中文本替换的可能性。 There are also the aesthetic considerations.
还有审美方面的考虑因素。 Much win in reserving all uppercase for macros.
在保留宏的全部大写方面取得了很大的成功。
Usually underlying type of unscoped enumerations is int ( it can be any integral type that can represent all values of enumerators). 通常底层类型的无范围枚举是int(它可以是任何可以表示枚举器的所有值的整数类型)。
However I do not see any narrowing conversion because type unsigned long can represent all values of type int. 但是我没有看到任何缩小转换,因为unsigned long类型可以表示int类型的所有值。
EDIT: It seems I am wrong because I found an example in the Standard that contradicts my assumption 编辑:我似乎错了,因为我发现标准中的一个例子与我的假设相矛盾
unsigned int ui1 = {-1}; // error: narrows
So unsigned int can not be initialized by using an initializer list that contains a negative number. 因此,无法使用包含负数的初始化列表初始化unsigned int。
So to avoid the warning the enumeration could be written as 因此,为了避免警告,枚举可以写成
enum FunctionType : unsigned int // or unsigned long
{
NORMAL = 0,
RANGE = 1
};
Since you're using c++11, you could declare your enum like so: 由于你使用的是c ++ 11,你可以像这样声明你的枚举:
enum Enum2 : unsigned char;
That should force the enum to work. 这应该迫使枚举工作。 That said, IDEONE has no warnings/errors for your posted code.
也就是说,IDEONE对您发布的代码没有警告/错误。 Could just be GCC being overly pedantic.
可能只是海湾合作委员会过分迂腐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.