0

I'm maintaining a project which contains following enum type definition. enum values are used in a combobox.

  1. Why this enum type is defined in hex like this, for performance improvement?

  2. xxxxx920P3 is actually a negative value -2147483648 and xxxxx920P2 is positive, it causes the conditional code to fail.The next value will be twice bigger as the xxxxx920P3, so any alternative solution for this enum definition rule? thanks.

  3. It is a QT c++ project. Can I define a enum type to ULONGLONG?

        enum Version
         {
             xxxxx        = 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,
         };
    
    Versions newVersions = (Version)mComboBox->itemData(inIndex).toUInt();
     if ( newVersions < xxxxx500) //now newVersions is a negative value
     {
    
     }
     else
     {
    
     }
    
Ming
  • 379
  • 1
  • 6
  • 20

1 Answers1

1

In Standard C enumerators have type int and the value must be in range of int. The 0x80000000 is out of range for int, this is a constraint violation that requires a diagnostic.

So what is happening depends on your compiler. The compiler implements an extension of its own devising for enumerators out of range for int. Based on the evidence you posted, the compiler gives that enumerator a value of INT_MIN (a large negative number).

You will have to design your code to take this into account, e.g. have a specific branch of the verstion test for xxxxx920P3.


When an enumeration contains a bunch of one-bit flags it's usually so that they can be combined together, e.g. xxxxx600 | xxxxx700 | xxxxx432 giving the ability to have a single value that represents any sized set of elements.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Thanks. M.M. it is a qt C++ project. Will xxxxx920P3 pass the testFlag() function? eg. if(m_P.versions().testFlag(xxxxx920P3) . – Ming Jun 21 '21 at 06:54
  • @Ming no idea, it would depend on how that function was defined – M.M Jun 21 '21 at 10:57
  • Could u pls check out this question? thanks. https://stackoverflow.com/questions/68404924/qt-qflags-signed-value-is-out-of-range-for-enum-constant – Ming Jul 16 '21 at 07:59