Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-trivial designated initializers not supported

I have a structure as follows:

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;  }; 

When I try to initialize it thus:

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

I get this error:

sorry, unimplemented: non-trivial designated initializers not supported 

I think it wants the order of initialization exactly as it is declared, and local_connection is missing. I don't need to initialize it though, and setting it to NULL doesn't work.

If I change it to this for g++, still get the same error:

struct app_data data = {     port : 18515,     ib_port : 1,     size : 65536,     tx_depth : 100,     sockfd : -1,     servername : NULL,     remote_connection : NULL,     ib_dev : NULL }; 
like image 598
Ivan Avatar asked Jul 04 '15 01:07

Ivan


People also ask

Does C++ have designated Initializers?

With C++20, we get a handy way of initializing data members. The new feature is called designated initializers and might be familiar to C programmers.

What is designated initializer in C++?

Designated initialization is an extension of aggregate initialization and empowers you to directly initialize the members of a class type using their names. Designated initialization is a special case of aggregate initialization.

What is designated initializer?

A designated initializer, or designator, points out a particular element to be initialized. A designator list is a comma-separated list of one or more designators. A designator list followed by an equal sign constitutes a designation.


2 Answers

the order of initialization needs to be in the exact order of declaration.

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 

I understand that this means that the compiler has no support for name-oriented, out-of-order, member initialization.

Need to initialize the struct in the old fashioned way. I keep the variable names for clarity, but I have to initialize them in order, and not skip a variable.

I can stop the initialization at any variable, but can't initialize variables that come of that.

like image 53
Parallel Universe Avatar answered Sep 23 '22 13:09

Parallel Universe


This does not work with g++. You are essentially using C constructs with C++. Couple of ways to get around it.

1) Remove the "." and change "=" to ":" when initializing.

#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) Use g++ -X c. (Not recommended) or put this code in extern C [Disclaimer, I have not tested this]

like image 38
Anon Avatar answered Sep 21 '22 13:09

Anon