我正在与一个嵌入式设备交换数据包,我真的希望能够在数据包定义的子字节部分使用枚举。但我不能猜测,可能工作语法,我怀疑这是不可能的,因为我不能工作,如何在C++中声明的部分字节的亚型:是否可以在C++ 11中指定枚举的位宽?
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的实验工具列表,所以我需要从源代码来构建。我想我现在只是在解决这个问题。感谢你的帮助。
谁降低了这个数字?我们很少看到更完美的问题。 – Potatoswatter 2015-02-09 02:20:31
...还有,在嵌入式设备上使用现代C++的荣誉。 – Potatoswatter 2015-02-09 02:22:00
谢谢。我习惯于Delphi,所以它看起来很自然,特别是当我们对嵌入式输出(Delphi,C++,ObjectiveC和Java。)的消费者进行多边测试时,显然是出于某种原因。让每个人的代码尽可能相似地读取并使用任何可用的安全类型更容易。 – 2015-02-09 02:29:14