具有复杂结构的 C++ 初始化列表

标签 c++ c structure initializer-list

我正在修改此 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++ 初始化列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400549/

相关文章:

c++ - timeval 未返回预期结果

c++ - 右值引用参数变成左值

c - 作业错误 : Dereferencing pointer to incomplete type

c - 编写编译器时,如何检查标记?

将全局数组复制到另一个全局数组

c++ - 指针(结构引用)作为函数赋值的参数

delphi - Delphi中如何通过ToolsAPI获取模块的结构?

c++ - 在 C 中将 Windows 控制台大小调整为全屏

c++ - 先前初始化内存是否保证在放置新调用后持续存在?

python - Ta-Lib:MAVP(可变周期移动平均线)中的周期概念是什么?