Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否可以在C+中指定枚举的位宽度+;11?_C++_C++11_Gcc - Fatal编程技术网

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

C++ 是否可以在C+中指定枚举的位宽度+;11?,c++,c++11,gcc,C++,C++11,Gcc,我正在与嵌入式设备交换数据包,我真的希望能够在数据包定义的子字节部分使用枚举。但是我猜不出一种语法可能会起作用,而且我怀疑这是不可能的,因为我不知道如何在C++中声明部分字节子类型: enum class communication_path_t : uint8_t { Ethernet = 0, Wifi = 1 }; typedef struct { communication_path_t pathByte; // works, uses one byte

我正在与嵌入式设备交换数据包,我真的希望能够在数据包定义的子字节部分使用枚举。但是我猜不出一种语法可能会起作用,而且我怀疑这是不可能的,因为我不知道如何在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]

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


编辑:升级到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;
g++-4.9-Dlinux-std=c++11-pthread(…)../common/LogPacketBreakdown.cpp
在../common/LogPacketBreakdown.cpp:12:0中包含的文件中:
../common/PacketInfo.h:104:50:错误:“Digiflex:::communicationPath”太小,无法容纳的所有值
“枚举类Digiflex::通信路径”[-Werror]
通信路径通信路径:3;

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

< P>不,没有办法让<代码> TyPulf C++中的位字段,甚至没有枚举类型。

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


但是,您的第一个示例非常好。正如Bill所说,这是一个GCC错误,作为GCC开发人员,这只是2013年以来的一个警告。解决方法是使用
int路径:3
和转换枚举值,或者根本不使用
枚举。

最新的编译器应该接受您发布的代码。您可以在本应进行修复的位置看到此错误报告:


在今天的gcc中,仍应发出警告。在叮当声中,您应该什么也看不到。

它至少在debian上的g++4.8中起作用,所以我假设在ubuntu上也起作用


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

谁否决了此操作?我们很少看到一个更完美的问题……也是在嵌入式设备上使用现代C++的荣誉。谢谢。我习惯了Delphi,所以它看起来是一种自然的方法,尤其是当我们有嵌入式输出的多语言用户(Delphi,C++,ObjectiveC和java)。让每个人的代码以尽可能相似的方式读取并使用任何可用的类型安全性都会更容易。此外:似乎认为即使是基于字节的枚举类也可以正确地设置为字节的任何合法值,而不管是否有具有这些值的枚举成员。是的,
enum:uint8
可以像
uint8
一样使用,但这意味着它也应该像
uint8
一样用作位字段。每个位域都是在其类型值的子集上定义的,这就是位域所做的。@CodeAboutator:第一条注释包括一个与代码基本相同的测试用例。有趣的是,这个bug已经存在一年了,正是我试图做的。该标准甚至说“一个位字段应具有整数或枚举类型(3.9.1)。”不幸的是,它看起来似乎不会很快被修复。另一个错误提供了来自该标准的更多信息。这似乎更有可能奏效:
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 -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;