0

I got following warnings for the enum value xxxxx920P4 building a qt C++ project. 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?

I'm using Qt version: 4.8.7 and Visual studio 2010.

The following loop will not be 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++)
{
}

Doc for Qt Qflags class: https://doc.qt.io/qt-5/qflags.html

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)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Ming
  • 379
  • 1
  • 6
  • 20

1 Answers1

3

The last value is too long to fit into a a non-long int. While newer compilers may try to choose a larger integral type for it, some older ones would complain. GCC tends to do the former unless you choose type explicitly. Note that second to last value 0x80000000 or any combination of masks involving it would not fit into int, it's essentially a negative -2147483648.

 enum Version : long long  
 {
     //...
     xxxxx920P4   = 0x100000000,
 }; 

PS. Apparently Qt5 doesn't support something different from (unsigned)int: https://doc.qt.io/qt-5/qflags.html

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • In below post, BitArray is mentioned as alternative solution. https://stackoverflow.com/questions/1060760/what-to-do-when-bit-mask-flags-enum-gets-too-large I guess I have to get ride of following QFlag code and rewrite the whole thing. Anyone can provide a BitArray example? Thanks. – Ming Jul 19 '21 at 07:56
  • @Ming that requires context... What really you are using it for? In general one even wouldn't need any special type, long long is enough. QFlag is an abstraction meant to be used with framework and visual editor. BitArray is .Net/asp thing but I had wrote similar stuff – Swift - Friday Pie Jul 20 '21 at 08:07