问题
While migrating to C++ I require a certain function that seems to have been deprecated.sorry, unimplemented: non-trivial designated initializers not supported
What is the correct way to implement the following data storage system in C++ for memory constraint systems?
typedef union union_t {
float f;
int i;
} arg;
typedef struct type_t {
int a;
arg b;
int d;
} element;
const element list[] = {
{
.a = 1,
.b = { .f = 3.141519f },
.d = 6
},
{
.a = 3,
.b = { .i = 1 },
}
};
Often the use of std:map
or std:vector
is suggested. Which is suitable, however list
is immutable and must be able to compile and link to a specific block of flash. Both seem unfit for that purpose.
The highest I can go is ARM Compiler 6, which is C++14.
回答1:
The way you shown is almost correct compliant with the incoming C++20 standard. Only that .d
also have to be initialized. Is it what I suggest to use.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0329r4.pdf
To handle this in C++14 you have to initilize it explicilty:
typedef union union_t {
float f;
int i;
} arg;
typedef struct type_t {
int a;
arg b;
int d;
} element;
const element list[] = {
{
/*.a = */ 1,
/*.b = */ { /*.f = */ 3.141519f },
/*.d = */ 6
},
{
/* .a = */ 3,
/* .b = */ { /* .i = */ 1 },
0
}
};
来源:https://stackoverflow.com/questions/58559463/embedded-c-static-initialization-of-struct-arrays