1

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.

Jeroen3
  • 919
  • 5
  • 20
  • 2
    this is a new feature coming in C++20, it's not "deprecated" (which means an old feature that is going to be removed) – M.M Oct 25 '19 at 13:35
  • 2
    For now, you need to use standard C in order to use designated initializers. Also, creating "variant" type unions is a very bad idea in either language. I can't think of any reason why you would use `union` in C++. – Lundin Oct 25 '19 at 13:38
  • 1
    And no, std::map and std::vector are not suitable since this is an embedded system, not a PC. Use std::array or POD types. "...compile and link to a specific block of flash. Both seem unfit for that purpose." The whole C++ language is unfit for that purpose. – Lundin Oct 25 '19 at 13:40

1 Answers1

2

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
    }
};
majkrzak
  • 1,332
  • 3
  • 14
  • 30