开发者

overriding enumeration base type using pragma or code change

Problem:

I am using a big C/C++ code base which works on gcc & visual studio compilers where enum base type is by default 32-bit(integer type).

This code also has lots of inline + embedded assembly which treats enum as integer type and enum data is used as 32-bit flags in many cases.

When compiled this code with realview ARM RVCT 2.2 compiler, we started getting many issues since realview compiler decides enum base type automatically based on the value an enum is set to. http://www.keil.com/support/man/docs/armccref/armccref_Babjddhe.htm

For example,

Consider the below enum,

enum Scale {
    TimesOne, //0
    TimesTwo, //1
    TimesFour, //2
    TimesEight, //3
};

This enum is used as a 32-bit flag. but compiler optimizes it to unsigned char type for this enum.

Using --enum_is_int compiler option is not a g开发者_运维问答ood solution for our case, since it converts all the enum's to 32-bit which will break interaction with any external code compiled without --enum_is_int.

This is warning i found in RVCT compilers & Library guide,

The --enum_is_int option is not recommended for general use and is not required for ISO-compatible source. Code compiled with this option is not compliant with the ABI for the ARM Architecture (base standard) [BSABI], and incorrect use might result in a failure at runtime. This option is not supported by the C++ libraries.

Question

How to convert all enum's base type (by hand-coded changes) to use 32-bit without affecting value ordering?

enum Scale {
    TimesOne=0x00000000,
    TimesTwo, // 0x00000001
    TimesFour, // 0x00000002
    TimesEight, //0x00000003
};

I tried the above change. But compiler optimizes this also for our bad luck. :(

There is some syntax in .NET like

enum Scale: int

Is this a ISO C++ standard and ARM compiler lacks it?

There is no #pragma to control this enum in ARM RVCT 2.2 compiler. Is there any hidden pragma available ?


I know that a lot of the windows headers use the following:

enum SOME_ENUM {
    ONE = 1,
    TWO = 2,
    //...
    FORCE_DWORD = 0x7FFFFFFF
};


In C++11:

enum class Scale : uint32_t {
   TimesOne, //0
   TimesTwo, //1
   TimesFour, //2
   TimesEight, //3
};

Visual Studio 2012 and gcc 4.4.6 with the -std=c++0x option both support this (earlier versions may as well.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜