是否可以在C++11中指定枚举的位宽

Is it possible to specify the bit width of an enum in C++11?

本文关键字:枚举 C++11 是否      更新时间:2023-10-16

我正在与嵌入式设备交换数据包,我真的希望能够在数据包定义的子字节部分使用枚举。但我猜不出一个可能有效的语法,我怀疑这是不可能的,因为我不知道如何在C++中声明部分字节子类型:

enum class communication_path_t : uint8_t  { 
    Ethernet = 0, Wifi = 1
};
typedef struct {
    communication_path_t pathByte;  // works, uses one byte
    // ... 
    // single byte split three ways
    uint8_t retryCount : 3;
    communication_path_t path : 3;  // compile error
    uint8_t deviceType : 2;
} packet_t;

这不会编译,因为您无法将8位枚举放入3位字段中。编辑错误:

<anonymous struct>::path’ is too small to hold all values
   of ‘enum class MyNamespace::communication_path_t’ [-Werror]

我想做的是这样的事情:

enum class communication_path_t : uint8_t : 3 { ...

typedef uint8_t:3 three_bit_int_t;
enum class communication_path_t : three_bit_int_t { ...

这两个都没有编译,而且我很难找到同时引用位字段和枚举的文档,这让我怀疑根本没有。在我花几个小时寻找之前,我想做的事情可能吗?


编辑:升级到g++-4.9并不能解决这个问题。它非常无痛,只是:

sudo apt-get install g++-4.9
g++-4.9 --version
g++-4.9 (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
GCC 4.9.2 released [2014-10-30]

然后将我的构建链更改为使用"g++-4.9"而不是"g++"。不幸的是,我得到了相同的错误:

g++-4.9 -Dlinux -std=c++11 -pthread (...) ../common/LogPacketBreakdown.cpp
In file included from ../common/LogPacketBreakdown.cpp:12:0:
../common/PacketInfo.h:104:50: error: ‘Digiflex::<anonymous
    struct>::communicationPath’ is too small to hold all values of 
    ‘enum class Digiflex::communication_path_t’ [-Werror]
    communication_path_t communicationPath : 3;

看起来我需要5.0,而这不在Ubuntu实验工具列表中,所以我需要从源代码构建。我想我现在只能接受变通办法了。谢谢大家的帮助。

您发布的代码应该被最新的编译器接受。您可以在本应进行修复的位置看到此错误报告:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51242

在今天的gcc中,仍然应该发出警告。叮当作响,你什么也看不见。

不,没有办法在C++中typedef一个位字段,即使是枚举类型。

位字段ness是成员变量声明的属性,类型系统根本不支持它。

但是,你的第一个例子非常好。正如Bill所说,这是一个GCC错误,正如GCC开发人员所指出的,自2013年以来,这只是一个警告。解决方法是使用int path : 3;并转换枚举值,或者根本不使用enum

它至少在debian上从g++4.8开始工作,所以我假设它也在ubuntu上工作。

编译器只给出警告,在这种情况下,错误是由-Werr编译器选项引起的。为溢出配置-Werror是个好主意,因为在分配不适合位字段的枚举值时会报告此情况。