【问题标题】:C++ initializer list with complicated structures具有复杂结构的 C++ 初始值设定项列表
【发布时间】:2015-05-22 15:27:09
【问题描述】:

我正在修改这个 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,
    },
},
};

【问题讨论】:

  • C++ 没有指定的初始化器。
  • 我知道,但是如何在不大量调用构造函数的情况下进行初始化。
  • @ArulMoondra 将其设为指针并使用malloc() 而不是new?你为什么想要那个?
  • 任何拥有名为magic 的变量的人都会造成一些麻烦...stackoverflow.com/a/185106/2296458
  • 您是否从任何实际的 c++ 代码中引用此结构?如果不是,请将定义它的源文件重命名为 .c

标签: c++ c structure initializer-list


【解决方案1】:
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...
    }
};

【讨论】:

  • 谢谢,虽然是一个问题,但我必须记住,实际结构的顺序是保留的。其中一些结构不会初始化 10 个变量中的单个变量,而是初始化所有其他变量。有没有办法跳过其中的变量。
  • 恐怕一个跳过的变量需要为零
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 2012-12-24
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
相关资源
最近更新 更多