简体   繁体   English

缩小从int到long unsigned int {}的转换在C ++ 11中是不正确的

[英]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. 顺便说一句,我认为g ++警告是没有根据的,因为原始代码是有效的C ++ 03。 Correction: As I understand it now, the diagnostic is correct, and this is a breaking change in C++11. 更正:据我所知,诊断是正确的,这是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
};
  • you could change "value" to int 你可以将“value”改为int
  • or you could use the new "enum class" (c++11) which is strongly typed, and declare your "value" as this type. 或者您可以使用强类型的新“枚举类”(c ++ 11),并将此“值”声明为此类型。

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.

相关问题 C ++ 11:格式错误的调用是未定义的行为? - C++11: ill-formed calls are undefined behavior? 为什么(int&)0格式不正确? - Why is (int&)0 ill-formed? 在C ++ 11标准中,它表示char * p =“abc”;是不是形成了? - Where in the C++11 Standard does it say that char* p = “abc”; is ill-formed? C ++-从“ int”到“ unsigned char”的无效缩小转换 - C++ - Invalid narrowing conversion from “int” to “unsigned char” 错误:'18446744069414584320ull'从'long long unsigned int'到'int'的缩小转换{} [-Wnarrowing] - error: narrowing conversion of ‘18446744069414584320ull’ from ‘long long unsigned int’ to ‘int’ inside { } [-Wnarrowing] 缩小从 int 到 unsigned char 的转换 - Narrowing conversion from int to unsigned char 将unsigned int转换为short unsigned int - Narrowing Conversion of unsigned int to short unsigned int 从“unsigned int”到“int”的转换需要缩小转换 - conversion from 'unsigned int' to 'int' requires a narrowing conversion 错误 C2397:从“int”到“unsigned int”的转换需要缩小转换 - error C2397: conversion from 'int' to 'unsigned int' requires a narrowing conversion 在C ++中从无符号long long转换为unsigned int - Conversion from unsigned long long to unsigned int in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM