不支持非平凡的指定初始值设定项

时间:2015-07-04 01:27:09

标签: c++ initialization

我的结构如下:

struct app_data
{
    int port;
    int ib_port;
    unsigned size;
    int tx_depth;
    int sockfd;
    char *servername;
    struct ib_connection local_connection;
    struct ib_connection *remote_connection;
    struct ibv_device *ib_dev;

};

当我尝试初始化时:

struct app_data data =
{
    .port = 18515,
    .ib_port = 1,
    .size = 65536,
    .tx_depth = 100,
    .sockfd = -1,
    .servername = NULL,
    .remote_connection = NULL,
    .ib_dev = NULL
};

我收到此错误:

sorry, unimplemented: non-trivial designated initializers not supported

我认为它需要完全按照声明的顺序进行初始化,并且缺少local_connection。我不需要初始化它,并将其设置为NULL不起作用。

如果我将其更改为g ++,仍然会收到相同的错误:

struct app_data data =
{
    port : 18515,
    ib_port : 1,
    size : 65536,
    tx_depth : 100,
    sockfd : -1,
    servername : NULL,
    remote_connection : NULL,
    ib_dev : NULL
};

6 个答案:

答案 0 :(得分:37)

初始化的顺序需要按照声明的确切顺序。

typedef struct FOO
{
    int a;
    int b;
    int c;
}FOO;

FOO foo   = {.a = 1, .b = 2}; // OK
FOO foo1  = {.a = 1};         // OK
FOO foo2  = {.b = 2, .a = 1}; // Error sorry, unimplemented: non-trivial designated initializers not supported
FOO foo3  = {.a = 1, .c = 2}; // Error sorry, unimplemented: non-trivial designated initializers not supported

据我所知,这意味着编译器不支持面向名称的无序成员初始化。

需要以旧式方式初始化结构。为了清楚起见,我保留变量名称,但是我必须按顺序初始化它们,而不是跳过变量。

我可以停止任何变量的初始化,但不能初始化由此产生的变量。

答案 1 :(得分:27)

这不适用于g ++。您基本上使用C构造与C ++。有几种方法可以解决它。

1)删除"。"并改变" ="到":"初始化时。

#include <iostream>

using namespace std;
struct ib_connection
   {
    int x;
   };

   struct ibv_device
   {
   int y;
  };

struct app_data
{
    int port;
    int ib_port;
    unsigned size;
    int tx_depth;
    int sockfd;
    char *servername;
    struct ib_connection local_connection;
    struct ib_connection *remote_connection;
    struct ibv_device *ib_dev;

};

int main()
{

    struct app_data data =
    {
        port : 18515,
        ib_port : 1,
        size : 65536,
        tx_depth : 100,
        sockfd : -1,
        servername : NULL,

        local_connection : {5},
        remote_connection : NULL,
        ib_dev : NULL
    };

   cout << "Hello World" << endl; 

   return 0;
}

2)使用g ++ -X c。 (不推荐)或将此代码放在外部C [免责声明,我还没有测试过]

答案 2 :(得分:10)

我注意到我的GCC编译器有一些技巧可以接受.fieldname = value分配,但只有当字段的顺序与它们在结构中声明的顺序相同时才会编译。

我能够以两种方式初始化这个结构。具有名称的那个提高了可读性,并且如果稍后更改结构字段顺序,则降低了分配错误数据的风险。

//Declare struct
typedef struct
{
    uint32_t const * p_start_addr;
    uint32_t const * p_end_addr;
    fs_cb_t  const   callback;    
    uint8_t  const   num_pages;  
    uint8_t  const   priority;
} fs_config_t;

//Assign unnamed
fs_config_t fs_config  
{
    (uint32_t*)0x00030000,  // uint32_t const * p_start_addr;
    (uint32_t*)0x00038000,  // uint32_t const * p_end_addr;         
    fs_evt_handler,         // fs_cb_t  const   callback;
    8,                      // uint8_t  const   num_pages;
    0xFE                    // uint8_t  const   priority;               
};

//Assign to named fields
static fs_config_t fs_config1  
{
    .p_start_addr = (uint32_t*)0x00030000,
    .p_end_addr = (uint32_t*)0x00038000,            
    .callback = fs_evt_handler,
    .num_pages = 8,
    .priority = 0xFE                
};      

经验法则是:

  1. 分配到.name =值字段
  2. 按声明的顺序分配
  3. 包括分配中的所有字段

答案 3 :(得分:3)

由于Arduino IDE没有其他方法适用于我,我决定单独设置每个字段:

struct app_data data;

data.port = 18515;
data.ib_port = 1;
data.size = 65536;
data.tx_depth = 100;
data.sockfd = -1;
data.servername = NULL;
data.remote_connection = NULL;
data.ib_dev = NULL;

答案 4 :(得分:2)

不幸的是,C ++并不支持指定的初始化程序。 GCC仍允许您使用它们(作为扩展名),但您必须按照struct中列出的顺序初始化成员。

另一种解决方法是使用立即调用的lambda:

constexpr fuse_operations fuse_ops = []{
  fuse_operations ops{};
  ops.destroy = wiifs_destroy;
  ops.getattr = wiifs_getattr;
  ops.access = wiifs_access;
  // ...
  return ops;
}();

我个人更喜欢这个解决方案,因为它是完全标准的C ++,它允许您按照您想要的顺序初始化字段,跳过您不需要的字段并默认初始化其余字段。编译器仍然是able to optimise this。请注意,这只适用于C ++ 17或更高版本。

答案 5 :(得分:-1)

另请注意,正如原始问题所述,成员表达的顺序很重要。我注意到如果我只想在前面的例子中初始化“size”,我需要先为.port和.ib_port设置表达式。否则我收到错误“抱歉,未实现:不支持非平凡的指定初始值设定项”不是那么直观......

相关问题