具有复杂结构的c++初始化列表
C++ initializer list with complicated structures
我正在修改这个C代码以与g++编译器一起工作,我被困在这个相当复杂的结构上。g++编译器报错"对不起,未实现:不支持非平凡的指定初始化式"。我知道一个解决方案,包括编写一个构造函数,但对构造函数的调用将是巨大的,太令人困惑。有没有什么方法可以简化和折射它,使它更易于阅读和兼容g++ ?
static const struct {
struct {
__le32 magic;
__le32 length;
#ifndef USE_DEPRECATED_DESC_HEAD
__le32 flags;
#endif
__le32 fs_count;
__le32 hs_count;
} __attribute__((packed)) header;
struct {
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor_no_audio svc_in;
struct usb_endpoint_descriptor_no_audio to_ap;
struct usb_endpoint_descriptor_no_audio from_ap;
} __attribute__((packed)) fs_descs, hs_descs;
} __attribute__((packed)) descriptors = {
.header = {
#ifdef USE_DEPRECATED_DESC_HEAD
.magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC),
#else
.magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
.flags = htole32(FUNCTIONFS_HAS_FS_DESC |
FUNCTIONFS_HAS_HS_DESC),
#endif
.length = htole32(sizeof descriptors),
.fs_count = htole32(4),
.hs_count = htole32(4),
},
.fs_descs = {
.intf = {
.bLength = sizeof descriptors.fs_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
.iInterface = 1,
},
.svc_in = {
.bLength = sizeof descriptors.fs_descs.svc_in,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.bInterval = 10,
.wMaxPacketSize = 64
},
.to_ap = {
.bLength = sizeof descriptors.fs_descs.to_ap,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 64
},
.from_ap = {
.bLength = sizeof descriptors.fs_descs.from_ap,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 64
},
},
.hs_descs = {
.intf = {
.bLength = sizeof descriptors.hs_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
.iInterface = 1,
},
.svc_in = {
.bLength = sizeof descriptors.hs_descs.svc_in,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.bInterval = 10,
.wMaxPacketSize = 512,
},
.to_ap = {
.bLength = sizeof descriptors.hs_descs.to_ap,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 512,
},
.from_ap = {
.bLength = sizeof descriptors.hs_descs.from_ap,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 512,
},
},
};
static const struct {
struct {
__le32 magic;
__le32 length;
#ifndef USE_DEPRECATED_DESC_HEAD
__le32 flags;
#endif
__le32 fs_count;
__le32 hs_count;
} __attribute__((packed)) header;
struct {
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor_no_audio svc_in;
struct usb_endpoint_descriptor_no_audio to_ap;
struct usb_endpoint_descriptor_no_audio from_ap;
} __attribute__((packed)) fs_descs, hs_descs;
} __attribute__((packed))
descriptors =
{
// header
{
// magic
#ifdef USE_DEPRECATED_DESC_HEAD
htole32(FUNCTIONFS_DESCRIPTORS_MAGIC),
#else
htole32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
#endif
// length
htole32(sizeof descriptors),
// flags
#ifdef USE_DEPRECATED_DESC_HEAD
htole32(FUNCTIONFS_HAS_FS_DESC |
FUNCTIONFS_HAS_HS_DESC),
#endif
// fs_count
htole32(4),
htole32(4),
},
// fs_descs
{
// intf
{
sizeof descriptors.fs_descs.intf, // bLength
USB_DT_INTERFACE, // bDescriptorType
3, // bNumEndpoints
USB_CLASS_VENDOR_SPEC,// bInterfaceClass
1, // iInterface
},
// svc_in ...
// and so on...
}
};
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- C++使用整数的压缩数组初始化对象
- C++初始化基类
- 多成员Constexpr结构初始化
- 复制列表初始化的隐式转换的等级是多少
- 内联映射初始化的动态atexit析构函数崩溃
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 没有用于初始化C++中的变量模板的匹配构造函数
- 在未初始化映射的情况下,将值插入到映射的映射中
- C++成员初始化
- 为什么在C++中首先初始化成员类
- 同时具有"聚合初始化"和"模板推导"
- 初始化具有非默认构造函数的std::数组项的更好方法
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 在C和C++中初始化结构中的数组
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 在函数内部的声明中初始化数组,并在外部使用它
- 继承:构造函数,初始化C++11中基类的类C数组成员