QFLAGS : signed value is out of range for enum constant
-
Hi, All
I got following warnings for the enum value "xxxxx920P4". And I found out in the debugging that 'xxxxx920P4' is now equals to "xxxxxundefine"( 0x00000000), which causes the unexpected result. How should I resolve this problem? thank.
warning C4341: 'xxxxx920P4' : signed value is out of range for enum constant
warning C4309: 'initializing' : truncation of constant value
enum Version { xxxxxundefine = 0x00000000, xxxxx400 = 0x00000001, xxxxx401 = 0x00000002, xxxxx410 = 0x00000004, xxxxx411 = 0x00000008, xxxxx412 = 0x00000010, xxxxx420 = 0x00000020, xxxxx430 = 0x00000040, xxxxx431 = 0x00000080, xxxxx432 = 0x00000100, xxxxx440 = 0x00000200, xxxxx500 = 0x00000400, xxxxx510 = 0x00000800, xxxxx520 = 0x00001000, xxxxx521 = 0x00002000, xxxxx600 = 0x00004000, xxxxx611 = 0x00008000, xxxxx620 = 0x00010000, xxxxx621 = 0x00020000, xxxxx700 = 0x00040000, xxxxx910 = 0x00080000, xxxxx910P5 = 0x00100000, xxxxx910P6 = 0x00200000, xxxxx910P11 = 0x00400000, xxxxx910P12 = 0x00800000, xxxxx910P13 = 0x01000000, xxxxx910P14 = 0x02000000, xxxxx910P15 = 0x04000000, xxxxx910P16 = 0x08000000, xxxxx920 = 0x10000000, xxxxx920P1 = 0x20000000, xxxxx920P2 = 0x40000000, xxxxx920P3 = 0x80000000, xxxxx920P4 = 0x100000000, }; Q_DECLARE_FLAGS(Versions, Version)
@MNGL said in QFLAGS : signed value is out of range for enum constant:
warning C4341: 'xxxxx920P4' : signed value is out of range for enum constant
As @Christian-Ehrlicher already sayed,
xxxxx920P4 = 0x100000000
needs at least 33 bit, this cannot be hold in an integer which have 32 bit ;)You can try to force enum type to long long:
enum Version : qint64 { ... }
, this should works with C++ 11 at least. -
Qt version: 4.8.7
Compiler: Visual studio 2010:
Following loop will not executed. gVersions[0].verNum is "xxxxx920P4", so I guess "xxxxx920P4"'s value has been truncated and now it equals to "0x00000000".
for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)
{}
-
Qt version: 4.8.7
Compiler: Visual studio 2010:
Following loop will not executed. gVersions[0].verNum is "xxxxx920P4", so I guess "xxxxx920P4"'s value has been truncated and now it equals to "0x00000000".
for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)
{}
@MNGL said in QFLAGS : signed value is out of range for enum constant:
for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)
Well, what do you think will happen if "gVersions[i].verNum != xxxxxundefine" never becomes false?
-
@MNGL said in QFLAGS : signed value is out of range for enum constant:
for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)
Well, what do you think will happen if "gVersions[i].verNum != xxxxxundefine" never becomes false?
-
@jsulm said in QFLAGS : signed value is out of range for enum constant:
xxxxxundefine
"xxxxxundefine" is also in the gVersions[], "gVersions[i].verNum != xxxxxundefine" will become false.
@MNGL said in QFLAGS : signed value is out of range for enum constant:
will become false
Well, if you are sure.
Else your app will crash with out of bounds exception. It is always a good idea to check the index for being in range. -
Unfortunately, I'm the guy who gonna maintain this project, the guy who design this did not expect the enum value will be out of range in less than 40 items.
@MNGL said in QFLAGS : signed value is out of range for enum constant:
did not expect the enum value will be out of range in less than 40 items
Your enum is a bit-flags enum. It does not hold sequential values (1, 2, 3, 4, ...), it holds sequential bits (1, 2, 4, 8, ...). So if you only give it 32-bits it will hold... up to 32 values. 40 items is not going to fit into it!
-
@MNGL Did you try what @KroMignon suggested?
-
@MNGL Did you try what @KroMignon suggested?
-
@jsulm Yes, I have. The QFlag class only works with int or unsinged int. https://doc.qt.io/qt-5/qflags.html.
Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.
-
@MNGL Well, QBitArray and QFlag are two different things. You can't use QBitArray in code which expects QFlag.
In below post, BitArray is mentioned as alternative solution.
https://stackoverflow.com/questions/1060760/what-to-do-when-bit-mask-flags-enum-gets-too-largeI guess I have to get ride of following QFlag code and rewrite the whole thing.
if(Versions.testFlag(xxxxx920P4) )
-
@jsulm Yes, I have. The QFlag class only works with int or unsinged int. https://doc.qt.io/qt-5/qflags.html.
Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.
@MNGL said in QFLAGS : signed value is out of range for enum constant:
Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.
Just out of curiosity, it is not easier to change the "nature" of the enum and use plain values and not bit field?
Do you really need a bit field?It maybe easier to replace the bitfield enum with and plain value enum and use
QList
/QVector
on situation where you need to handle multiple values.Just my 2 cts.
-
typedef enum { Undefined = 0x00000000, H1 = 0x00000001, H2 = 0x00000002, H3 = 0x00000004, } Hardware; Q_DECLARE_FLAGS(Hardwares, Hardware) typedef struct { Version verNum ; Hardwares supportedHW ; } Settings ; const Settings gSettings[] = { { xxxxx400 , H1|H2|H3 }, { xxxxx401 , H1|H3 }, } ;
Now the "Hardwares" type is to store combinations of Hardware values. Due to the range limit, I want to change "supportedHW" to a different data structure. I tired following type:
QList<Hardware> supportedHW; //Got compiling errors:error C2552: 'supportedHW' : non-aggregates cannot be initialized with initializer list Hardware supportedHW[4] ; //pass compile, but not easy to use as QList and QFlags
Any suggestions? Thanks.
-
typedef enum { Undefined = 0x00000000, H1 = 0x00000001, H2 = 0x00000002, H3 = 0x00000004, } Hardware; Q_DECLARE_FLAGS(Hardwares, Hardware) typedef struct { Version verNum ; Hardwares supportedHW ; } Settings ; const Settings gSettings[] = { { xxxxx400 , H1|H2|H3 }, { xxxxx401 , H1|H3 }, } ;
Now the "Hardwares" type is to store combinations of Hardware values. Due to the range limit, I want to change "supportedHW" to a different data structure. I tired following type:
QList<Hardware> supportedHW; //Got compiling errors:error C2552: 'supportedHW' : non-aggregates cannot be initialized with initializer list Hardware supportedHW[4] ; //pass compile, but not easy to use as QList and QFlags
Any suggestions? Thanks.
@MNGL @KroMignon asked you whether your enum really has to contain bitfields: means 2^x values (0001, 0010, 0100, ...).
If this is not required then use just normal numbers, then your range is what an int provides and is for sure enough.