是否可以在C++11中指定枚举的位宽
Is it possible to specify the bit width of an enum in C++11?
我正在与嵌入式设备交换数据包,我真的希望能够在数据包定义的子字节部分使用枚举。但我猜不出一个可能有效的语法,我怀疑这是不可能的,因为我不知道如何在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是个好主意,因为在分配不适合位字段的枚举值时会报告此情况。
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 连续枚举 C++11
- C ++枚举范围无法使用-std=c ++ 98进行编译,但使用-std=c ++ 11可以
- 安全使用严格枚举 C++ 11
- C++11 类中放置的枚举类的重载运算符
- 初始化枚举类 (C++11) 类型的二维 std::数组
- C++11 枚举前向导致"underlying type mismatch"
- 枚举布尔模板参数在C 11中
- 我可以创建一个可以接受任何枚举的模板(非类型)param类吗?C 11
- 使用 C++11 可变参数模板初始化枚举到字符串映射
- C++11 枚举类:加上 int 编译失败,为什么
- C++11 枚举类命名空间块
- 传递 C++11 枚举类作为模板,同时自动推断其类型
- C 11枚举类实例化
- 类型安全的c++11枚举类标志的模板
- 在C++11中分离枚举声明和定义
- 是否可以在C++11中指定枚举的位宽
- 如何在C++11中输出枚举类的值
- 如何在 C++11 中“使用”弱枚举
- C++11 在开关大小写中混合枚举类和无符号 int 将无法编译