简体   繁体   English

使用pragma或代码更改覆盖枚举基类型

[英]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). 我正在使用一个大的C / C ++代码库,它适用于gcc和visual studio编译器,其中枚举基类型默认为32位(整数类型)。

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. 此代码还有许多内联+嵌入式程序集,它将枚举视为整数类型,在许多情况下,枚举数据用作32位标志。

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. 当使用realview ARM RVCT 2.2编译器编译此代码时,我们开始遇到许多问题,因为realview编译器会根据枚举设置的值自动决定枚举基类型。 http://www.keil.com/support/man/docs/armccref/armccref_Babjddhe.htm 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. 该枚举用作32位标志。 but compiler optimizes it to unsigned char type for this enum. 但是编译器将它优化为此枚举的unsigned char类型。

Using --enum_is_int compiler option is not a good 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. 使用--enum_is_int编译器选项对我们的情况来说不是一个好的解决方案,因为它将所有枚举转换为32位,这将破坏与没有--enum_is_int编译的任何外部代码的交互。

This is warning i found in RVCT compilers & Library guide, 这是我在RVCT编译器和库指南中找到的警告,

The --enum_is_int option is not recommended for general use and is not required for ISO-compatible source. 建议不要将--enum_is_int选项用于一般用途,ISO兼容源不需要.enum_is_int选项。 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. 使用此选项编译的代码不符合ARM体系结构的ABI(基本标准)[BSABI],不正确的使用可能会导致运行时出现故障。 This option is not supported by the C++ libraries. C ++库不支持此选项。

Question

How to convert all enum's base type (by hand-coded changes) to use 32-bit without affecting value ordering? 如何将所有枚举的基本类型(通过手工编码的更改)转换为使用32位而不影响值排序?

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 在.NET中有一些语法

enum Scale: int 枚举比例:int

Is this a ISO C++ standard and ARM compiler lacks it? 这是ISO C ++标准,而ARM编译器缺少它吗?

There is no #pragma to control this enum in ARM RVCT 2.2 compiler. ARM RVCT 2.2编译器中没有#pragma来控制此枚举。 Is there any hidden pragma available ? 有没有隐藏的pragma?

I know that a lot of the windows headers use the following: 我知道很多windows标头使用以下内容:

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

In C++11: 在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.) 带有-std = c ++ 0x选项的Visual Studio 2012和gcc 4.4.6都支持此功能(早期版本也可以。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM