我的结构如下:
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
};
Ivan asked 2020-01-20T01:42:29Z
6个解决方案
45 votes
初始化的顺序必须与声明的确切顺序相同。
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
我知道这意味着编译器不支持面向名称,乱序的成员初始化。
需要以老式的方式初始化结构。 为了清楚起见,我保留了变量名,但是我必须按顺序对其进行初始化,而不要跳过变量。
我可以停止对任何变量的初始化,但是无法初始化随之而来的变量。
Parallel Universe answered 2020-01-20T01:43:34Z
27 votes
这不适用于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中[免责声明,我尚未对此进行测试]
Anon answered 2020-01-20T01:43:00Z
10 votes
我注意到我的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
};
经验法则是:
- 分配给.name = value字段
- 按照声明的顺序分配
- 在作业中包括所有字段
flodis answered 2020-01-20T01:44:16Z
4 votes
由于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;
Falko answered 2020-01-20T01:44:36Z
2 votes
不幸的是,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 ++,它使您可以按需要的顺序初始化字段,跳过不需要的字段并默认初始化其余的字段。 并且编译器仍然能够对此进行优化。 请注意,这仅适用于C ++ 17或更高版本。
Léo Lam answered 2020-01-20T01:45:05Z
-1 votes
还请注意,正如原始问题所述,成员表达式的顺序很重要。 我注意到,如果我只想在前面的示例中初始化“ size”,则需要在.port和.ib_port之前放置表达式。 否则会出现错误“对不起,未实现:不支持非平凡的指定初始值设定项”不是那么直观...
Guz answered 2020-01-20T01:45:26Z
translate from https://stackoverflow.com:/questions/31215971/non-trivial-designated-initializers-not-supported